-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmain.go
84 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
82
83
84
package main
import(
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/bwmarrin/discordgo"
)
// The "StrList" custom type allows us to add our own functions to it
type StrList []string
// Globals
var (
GuildID string
DeveloperMode bool
DeveloperList StrList
startTime time.Time
)
// Main entry point
func main() {
var bot *discordgo.Session
var err error
startTime = time.Now()
botToken := getConfigPropertyAsStr("discord", "token")
if bot, err = discordgo.New("Bot " + botToken); err != nil {
fmt.Println("[ERROR] Critical error creating Discord session, ", err)
return
}
// Handle messageCreate events sent from Discord
bot.AddHandler(messageCreate)
if err = bot.Open(); err != nil {
fmt.Println("[ERROR] Critical error connecting to Discord, ", err)
return
}
GuildID = ""
DeveloperMode = false
DeveloperList = []string{"165177089035599873"} // List of discord user ID's that can access developer commands
// Build the alias maps for commands and dictionary definitions
buildDictionaryMap()
buildCommandMap()
fmt.Println("[INFO] Bot is now running! Press CTRL-C to stop!")
// Listen for kill signals
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Close the client and free resources
_ = bot.Close()
}
// Handler for message events received from Discord
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Don't process commands sent from the bot
if m.Author.ID == s.State.User.ID {
return
}
// Don't handle nil messages
if len(m.Content) <= 0 {
return
}
// When the first character is the command character, parse the command and pass it off to the generic command handler
if m.Content[0] == '!' {
cmd := strings.Replace(m.Content, "!", "", 1)
cmdParts := strings.Split(cmd, " ")
command(s, m, cmdParts, cmdParts[0])
}
}