-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
74 lines (64 loc) · 1.85 KB
/
main.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
package main
import (
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/rmwiesenberg/ode/commands"
"github.com/rmwiesenberg/ode/mentions"
"log"
"os"
"os/signal"
"strings"
)
// Bot parameters
var (
BotID = flag.String("id", "1140718447018377417", "BotID")
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers tools globally")
)
func messageHandler(session *discordgo.Session, message *discordgo.MessageCreate) {
if message.Author.ID == *BotID || message.Author.Bot {
// Do nothing because the bot is talking
return
} else if strings.HasPrefix(message.Content, fmt.Sprintf("<@!%s> ", *BotID)) || strings.HasPrefix(message.Content, fmt.Sprintf("<@%s> ", *BotID)) {
mentions.MentionHandler(session, message)
}
}
func main() {
flag.Parse()
session, err := discordgo.New("Bot " + os.Getenv("DISCORD_BOT_TOKEN"))
if err != nil {
panic(err)
}
session.AddHandler(messageHandler)
session.AddHandler(func(session *discordgo.Session, i *discordgo.InteractionCreate) {
if handler, ok := commands.CommandHandlers[i.ApplicationCommandData().Name]; ok {
handler(session, i)
}
})
session.AddHandler(func(session *discordgo.Session, r *discordgo.Ready) {
log.Printf("Ode has started on %d servers", len(session.State.Guilds))
})
err = session.Open()
if err != nil {
panic(err)
}
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
for _, v := range commands.Commands {
_, err := session.ApplicationCommandCreate(session.State.User.ID, *GuildID, v)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.Name, err)
}
}
defer func(session *discordgo.Session) {
err := session.Close()
if err != nil {
log.Fatalf("Cannot close the session: %v", err)
}
}(session)
stop := make(chan os.Signal)
signal.Notify(stop, os.Interrupt)
<-stop
log.Println("Gracefully shutdowning")
}