|
1
|
|
|
package main |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"fmt" |
|
5
|
|
|
"gopkg.in/robfig/cron.v2" |
|
6
|
|
|
"os" |
|
7
|
|
|
"os/signal" |
|
8
|
|
|
"strings" |
|
9
|
|
|
"syscall" |
|
10
|
|
|
|
|
11
|
|
|
"github.com/FlameInTheDark/dtbot/bot" |
|
12
|
|
|
"github.com/FlameInTheDark/dtbot/cmd" |
|
13
|
|
|
"github.com/bwmarrin/discordgo" |
|
14
|
|
|
) |
|
15
|
|
|
|
|
16
|
|
|
var ( |
|
17
|
|
|
conf *bot.Config |
|
18
|
|
|
// CmdHandler bot command handler |
|
19
|
|
|
CmdHandler *bot.CommandHandler |
|
20
|
|
|
// Sessions bot session manager |
|
21
|
|
|
Sessions *bot.SessionManager |
|
22
|
|
|
botId string |
|
|
|
|
|
|
23
|
|
|
youtube *bot.Youtube |
|
24
|
|
|
botMsg *bot.BotMessages |
|
25
|
|
|
dataType *bot.DataType |
|
26
|
|
|
dbWorker *bot.DBWorker |
|
27
|
|
|
guilds bot.GuildsMap |
|
28
|
|
|
botCron *cron.Cron |
|
29
|
|
|
) |
|
30
|
|
|
|
|
31
|
|
|
func main() { |
|
32
|
|
|
botCron = cron.New() |
|
33
|
|
|
conf = bot.LoadConfig() |
|
34
|
|
|
CmdHandler = bot.NewCommandHandler() |
|
35
|
|
|
registerCommands() |
|
36
|
|
|
Sessions = bot.NewSessionManager() |
|
37
|
|
|
youtube = &bot.Youtube{Conf: conf} |
|
38
|
|
|
botMsg = bot.NewMessagesMap() |
|
39
|
|
|
dataType = bot.NewDataType() |
|
40
|
|
|
discord, err := discordgo.New("Bot " + os.Getenv("BOT_TOKEN")) |
|
41
|
|
|
if err != nil { |
|
42
|
|
|
fmt.Println("Create session error, ", err) |
|
43
|
|
|
return |
|
44
|
|
|
} |
|
45
|
|
|
usr, err := discord.User("@me") |
|
46
|
|
|
if err != nil { |
|
47
|
|
|
fmt.Println("Error obtaining account details,", err) |
|
48
|
|
|
return |
|
49
|
|
|
} |
|
50
|
|
|
botId = usr.ID |
|
51
|
|
|
discord.AddHandler(commandHandler) |
|
52
|
|
|
discord.AddHandler(func(discord *discordgo.Session, ready *discordgo.Ready) { |
|
53
|
|
|
discord.UpdateStatus(0, conf.General.Game) |
|
54
|
|
|
guilds := discord.State.Guilds |
|
55
|
|
|
fmt.Println("Ready with", len(guilds), "guilds.") |
|
56
|
|
|
}) |
|
57
|
|
|
|
|
58
|
|
|
err = discord.Open() |
|
59
|
|
|
if err != nil { |
|
60
|
|
|
fmt.Printf("Connection open error: %v", err) |
|
61
|
|
|
return |
|
62
|
|
|
} |
|
63
|
|
|
defer discord.Close() |
|
64
|
|
|
fmt.Println("Bot is now running.") |
|
65
|
|
|
|
|
66
|
|
|
sc := make(chan os.Signal, 1) |
|
67
|
|
|
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill) |
|
68
|
|
|
dbWorker = bot.NewDBSession(conf.General.DatabaseName) |
|
69
|
|
|
guilds = dbWorker.InitGuilds(discord, conf) |
|
70
|
|
|
botCron.Start() |
|
71
|
|
|
defer botCron.Stop() |
|
72
|
|
|
defer dbWorker.DBSession.Close() |
|
73
|
|
|
<-sc |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
// Handle discord messages |
|
77
|
|
|
func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) { |
|
78
|
|
|
user := message.Author |
|
79
|
|
|
if user.ID == botId || user.Bot { |
|
80
|
|
|
return |
|
81
|
|
|
} |
|
82
|
|
|
args := strings.Split(message.Content, " ") |
|
83
|
|
|
name := strings.ToLower(args[0]) |
|
84
|
|
|
command, found := CmdHandler.Get(name) |
|
85
|
|
|
if !found { |
|
86
|
|
|
return |
|
87
|
|
|
} |
|
88
|
|
|
channel, err := discord.State.Channel(message.ChannelID) |
|
89
|
|
|
if err != nil { |
|
90
|
|
|
fmt.Println("Error getting channel,", err) |
|
91
|
|
|
return |
|
92
|
|
|
} |
|
93
|
|
|
guild, err := discord.State.Guild(channel.GuildID) |
|
94
|
|
|
if err != nil { |
|
95
|
|
|
fmt.Println("Error getting guild,", err) |
|
96
|
|
|
return |
|
97
|
|
|
} |
|
98
|
|
|
// Checking permissions |
|
99
|
|
|
perm, err := discord.State.UserChannelPermissions(botId, message.ChannelID) |
|
100
|
|
|
if err != nil { |
|
101
|
|
|
fmt.Printf("Error whilst getting bot permissions in guild \"%v\", %v\n", guild.ID ,err) |
|
102
|
|
|
return |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
if perm&discordgo.PermissionSendMessages != discordgo.PermissionSendMessages || |
|
106
|
|
|
perm&discordgo.PermissionAttachFiles != discordgo.PermissionAttachFiles{ |
|
107
|
|
|
fmt.Printf("Permissions denied on guild \"%v\"\n", guild.ID) |
|
108
|
|
|
return |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
ctx := bot.NewContext( |
|
112
|
|
|
botId, |
|
113
|
|
|
discord, |
|
114
|
|
|
guild, |
|
115
|
|
|
channel, |
|
116
|
|
|
user, |
|
117
|
|
|
message, |
|
118
|
|
|
conf, |
|
119
|
|
|
CmdHandler, |
|
120
|
|
|
Sessions, |
|
121
|
|
|
youtube, |
|
122
|
|
|
botMsg, |
|
123
|
|
|
dataType, |
|
124
|
|
|
dbWorker, |
|
125
|
|
|
guilds, |
|
126
|
|
|
botCron) |
|
127
|
|
|
ctx.Args = args[1:] |
|
128
|
|
|
c := *command |
|
129
|
|
|
c(*ctx) |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// Adds bot commands |
|
133
|
|
|
func registerCommands() { |
|
134
|
|
|
CmdHandler.Register("!r", cmd.PlayerCommand) |
|
135
|
|
|
CmdHandler.Register("!w", cmd.WeatherCommand) |
|
136
|
|
|
CmdHandler.Register("!t", cmd.TranslateCommand) |
|
137
|
|
|
CmdHandler.Register("!n", cmd.NewsCommand) |
|
138
|
|
|
CmdHandler.Register("!c", cmd.CurrencyCommand) |
|
139
|
|
|
CmdHandler.Register("!y", cmd.YoutubeCommand) |
|
140
|
|
|
CmdHandler.Register("!v", cmd.VoiceCommand) |
|
141
|
|
|
CmdHandler.Register("!b", cmd.BotCommand) |
|
142
|
|
|
CmdHandler.Register("!play", cmd.YoutubeShortCommand) |
|
143
|
|
|
CmdHandler.Register("!d", cmd.DebugCommand) |
|
144
|
|
|
CmdHandler.Register("!p", cmd.PollCommand) |
|
145
|
|
|
CmdHandler.Register("!dice", cmd.DiceCommand) |
|
146
|
|
|
CmdHandler.Register("!help", cmd.HelpCommand) |
|
147
|
|
|
CmdHandler.Register("!cron", cmd.CronCommand) |
|
148
|
|
|
} |
|
149
|
|
|
|