-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.go
72 lines (58 loc) · 1.5 KB
/
discord.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
package botbooter
import (
"fmt"
"github.com/bwmarrin/discordgo"
)
func (b *Bot) connectDiscord() error {
fmt.Println("Connecting Discord...")
b.DiscordSession.AddHandler(func(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
message := &Message{
UserID: m.Author.ID,
ChannelID: m.ChannelID,
Content: m.Content,
DiscordData: m,
}
b.handleMessageWithCommand(message)
})
err := b.DiscordSession.Open()
if err != nil {
fmt.Println("Error opening Discord session:", err)
return err
}
fmt.Println("Discord bot is now running. Press CTRL-C to exit.")
return nil
}
func (b *Bot) disconnectDiscord() error {
fmt.Println("Disconnecting Discord...")
return b.DiscordSession.Close()
}
func InitAsDiscordBot(token string) *Bot {
dg, err := discordgo.New("Bot " + token)
if err != nil {
fmt.Println("Error creating Discord session:", err)
return nil
}
return &Bot{
BotType: DiscordBotType,
DiscordSession: dg,
Commands: []Command{},
UnknownCommandHandler: nil,
SlackClient: nil,
SlackSocketClient: nil,
}
}
func getAttachmentsFromDiscordMessage(m *discordgo.Message) []Attachment {
var attachments []Attachment
for _, attachment := range m.Attachments {
isImage := attachment.Width > 0 && attachment.Height > 0
attachments = append(attachments, Attachment{
IsImage: isImage,
URL: attachment.URL,
ExtraData: attachment,
})
}
return attachments
}