This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
155 lines (136 loc) · 3.8 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
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
package main
import (
"errors"
"fmt"
"os"
"os/signal"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
/* Bot owner */
owner string
/* The list of the sounds found in the sound path */
soundList = make([]string, 0)
/* The list of connected server */
serverList = make(map[string]*Server, 0)
/* A Chan to close properly the bot via ""!musicbot shutdwon" */
shutdownChan = make(chan bool)
)
/* List all sounds in the sound path and put them in the soundlist */
func populateSoundCollection() {
//Create a temporary soundlist (in order that other routines always access to a full populated sound list)
var tempSoundList []string
//tempSoundList := make([]string, 0)
//Open the sound path
folder, err := os.Open(soundPath)
//Read the name of all files in the directory
files, err := folder.Readdirnames(0)
if err != nil {
fmt.Println("Error Reading directory :", err)
return
}
// For each file verify it has the dca suffix (only dca files are accepted by the bot) and add them to the list without the ".dca" suffix (user don't need to type it)
for _, file := range files {
if strings.HasSuffix(file, ".dca") {
name := strings.Replace(file, ".dca", "", -1)
tempSoundList = append(tempSoundList, name)
}
}
//Put the full populated sound list to soundList var
soundList = tempSoundList
}
/* Main function of the program */
func main() {
token := os.Getenv("TOKEN")
if token == "" {
fmt.Println("No token provided")
return
}
// Create a new Discord session using the provided token.
dg, err := discordgo.New(token)
if err != nil {
fmt.Println("error creating Discord session,", err)
return
}
//Get the bot owner
app, err := dg.Application("@me")
if err != nil {
fmt.Println(err)
return
}
owner = app.Owner.ID
// Fill the sound list
populateSoundCollection()
// Register messageCreate as a callback for the messageCreate events.
dg.AddHandler(messageCreate)
dg.AddHandler(guildCreate)
//Prevent forced stop
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Open the websocket and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("error opening connection,", err)
return
}
fmt.Println("Bot is now running. Press CTRL-C to exit.")
select {
case <-c:
case <-shutdownChan:
break
}
// Simple way to keep program running until CTRL-C is pressed or shutdown command entered
for _, server := range serverList {
server.Disconnect()
}
dg.Logout()
dg.Close()
fmt.Println("Goodbye ^^")
return
}
func guildCreate(s *discordgo.Session, g *discordgo.GuildCreate) {
serverList[g.ID] = NewServer(g.ID)
}
func guildDelete(s *discordgo.Session, g *discordgo.GuildDelete) {
delete(serverList, g.ID)
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if strings.HasPrefix(m.Content, prefix) {
message := strings.Split(m.Content, " ")
//Get the channel object from the channelID
c, err := s.State.Channel(m.ChannelID)
if err != nil {
// Could not find channel.
return
}
server, exist := serverList[c.GuildID]
if exist == false {
// Could not find guild
return
}
//Verify there is a command after the suffix
if len(message) > 1 {
msg := command(message, server, m.Author.ID, s)
if msg != "" {
_, _ = s.ChannelMessageSend(m.ChannelID, msg)
}
} else {
_, _ = s.ChannelMessageSend(m.ChannelID, help)
}
}
}
func getAuthorChannel(s *discordgo.Session, authorID, guildID string) (string, error) {
g, err := s.State.Guild(guildID)
if err != nil {
return "", errors.New("Error getting guild information")
}
for _, vs := range g.VoiceStates {
if vs.UserID == authorID {
return vs.ChannelID, nil
}
}
return "", errors.New("User not found")
}