-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (66 loc) · 1.87 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
75
76
77
78
79
80
81
package main
import (
"fmt"
"log"
"os"
"os/signal"
"strings"
"syscall"
"github.com/LucasSnatiago/gomusic-bot/commands"
"github.com/LucasSnatiago/gomusic-bot/config"
"github.com/LucasSnatiago/gomusic-bot/music"
"github.com/bwmarrin/discordgo"
)
var BotConfig *config.Config
func init() {
BotConfig = config.ReadConfig()
if BotConfig == nil {
log.Fatalf("Could not read config file")
}
}
func main() {
dg, err := discordgo.New("Bot " + BotConfig.Token)
if err != nil {
panic(err)
}
dg.AddHandler(HandleCommands)
dg.AddHandler(ready)
// In this example, we only care about receiving message events.
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsGuildVoiceStates
err = dg.Open()
if err != nil {
panic(err)
}
// Cleanly close down the Discord session.
defer dg.Close()
defer fmt.Println("\nPowering off bot.")
// Wait here until CTRL-C or other term signal is received.
fmt.Println("GOmusic is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-sc
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
// Set the playing status.
s.UpdateGameStatus(0, fmt.Sprintf("%shelp", BotConfig.BotPrefix))
}
func HandleCommands(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages sent by the bot itself
if m.Author.ID == s.State.User.ID {
return
} else if !strings.HasPrefix(m.Content, BotConfig.BotPrefix) { // Ignore messages that are not sent for this bot
return
}
// Split message in the command and its arguments
message := strings.Replace(m.Content, BotConfig.BotPrefix, "", 1)
cmd_and_args := strings.Split(message, " ")
// All available commands
switch strings.ToLower(cmd_and_args[0]) {
case "ping":
commands.Ping(s, m)
case "play":
music.ConnectVoiceChannel(s, m)
case "echo":
// commands.Echo()
}
}