-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
165 lines (139 loc) · 5.03 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"errors"
"fmt"
"strings"
log "github.com/sirupsen/logrus"
"github.com/bwmarrin/discordgo"
"github.com/jamestjw/lyrical/utils"
"github.com/jamestjw/lyrical/voice"
)
// DiscordEvent will be passed into handler functions and contains all
// that is necessary to respond accordingly.
type DiscordEvent struct {
session *discordgo.Session
message *discordgo.MessageCreate
}
// SendMessage sends a message to the channel within
// the guild that invoked this event.
func (e DiscordEvent) SendMessage(message string) *discordgo.Message {
m, err := e.session.ChannelMessageSend(e.message.ChannelID, message)
if err != nil {
log.Fatal(err)
}
return m
}
// SendMessageWithMentions sends a message to the channel within the guild that invoked
// the event while mentioning a list of users present.
// message: Message to send
// userIDs: List of users to mention
func (e DiscordEvent) SendMessageWithMentions(message string, userIDs []string) *discordgo.Message {
if len(userIDs) > 0 {
joinedUserIDs := strings.Join(utils.StringArrayMap(userIDs, utils.Mentionify), " ")
// TODO: Support mentioning users from the front
// TODO: Support taking delimiter between user mentions and message as a parameter
message = fmt.Sprintf("%s\n%s", message, joinedUserIDs)
}
m, err := e.session.ChannelMessageSendComplex(e.message.ChannelID, &discordgo.MessageSend{
Content: message,
AllowedMentions: &discordgo.MessageAllowedMentions{
Parse: []discordgo.AllowedMentionType{discordgo.AllowedMentionTypeUsers},
},
})
if err != nil {
log.Fatal(err)
}
return m
}
// SendQuotedMessage sends a message to the channel within
// the guild that invoked this event with an added quote.
func (e DiscordEvent) SendQuotedMessage(quote string, message string) *discordgo.Message {
return e.SendQuotedMessageWithMentions(quote, message, make([]string, 0))
}
// SendQuotedMessageWithMentions sends a message to the channel within
// the guild that invoked this event with an added quote while mentioning
// list of users.
func (e DiscordEvent) SendQuotedMessageWithMentions(quote string, message string, userIDs []string) *discordgo.Message {
quotedMessage := fmt.Sprintf(`>>> %s`, quote)
e.SendMessage(quotedMessage)
return e.SendMessageWithMentions(message, userIDs)
}
// React will add a reaction from the bot to the message that triggered
// this event.
func (e DiscordEvent) React(emoji string) {
e.ReactToMessage(emoji, e.message.ID)
}
// ReactToMessage will react to a particular message in the channel that triggered this event
func (e DiscordEvent) ReactToMessage(emoji string, messageID string) {
err := e.session.MessageReactionAdd(e.message.ChannelID, messageID, emoji)
if err != nil {
log.Fatal(err)
}
}
// FindVoiceChannel tries to find a voice channel with this channel name
// within the guild.
func (e DiscordEvent) FindVoiceChannel(channelName string) (channelID string, err error) {
channels, err := e.session.GuildChannels(e.message.GuildID)
if err != nil {
return
}
for _, channel := range channels {
if channel.Bitrate == 0 || channel.ParentID == "" {
continue
}
if channel.Name == channelName {
channelID = channel.ID
return
}
}
err = errors.New("unable to find channel with this name")
return
}
// GetSession returns a Connectable belonging to the guild of this event
func (e DiscordEvent) GetSession() voice.Connectable {
return botSession{e.session}
}
// GetGuildID returns the guild ID of the guild in which this event
// was invoked.
func (e DiscordEvent) GetGuildID() string {
return e.message.GuildID
}
// GetVoiceConnection returns a Connection that the bot is connected to
// in the guild in which this event was fired.
func (e DiscordEvent) GetVoiceConnection() (voice.Connection, bool) {
vc, connected := e.session.VoiceConnections[e.GetGuildID()]
return voice.DGVoiceConnection{Connection: vc}, connected
}
func (e DiscordEvent) GetChannelID() string {
return e.message.ChannelID
}
func (e DiscordEvent) GetMessageByMessageID(messageID string) (*discordgo.Message, error) {
m, err := e.session.ChannelMessage(e.GetChannelID(), messageID)
return m, err
}
// GetReactionsFromMessage will fetch a list of IDs of users that reacted
// to a particular message.
func (e DiscordEvent) GetReactionsFromMessage(messageID string) (map[string][]string, error) {
reactions := make(map[string][]string)
m, err := e.GetMessageByMessageID(messageID)
if err != nil {
return make(map[string][]string), err
}
for _, reaction := range m.Reactions {
var userIDs []string
users, err := e.session.MessageReactions(e.message.ChannelID, messageID, reaction.Emoji.Name, 100, "", "")
if err != nil {
return make(map[string][]string), fmt.Errorf("unable to fetch users that reacted to the message")
}
for _, user := range users {
userIDs = append(userIDs, user.ID)
}
reactions[reaction.Emoji.Name] = userIDs
}
return reactions, nil
}
// GetUserForBot fetches the User that corresponds to the current bot
func (e DiscordEvent) GetUserForBot() (*discordgo.User, error) {
user, err := e.session.User("@me")
return user, err
}