-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
168 lines (148 loc) · 4.12 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
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"os"
"os/signal"
"syscall"
)
// Variables used for command line parameters
var (
TokenFile string
Token string
Version string
OwnName string
Debug bool
DevSrvID string
)
// TODO move stuff to proper config file
func init() {
flag.StringVar(&TokenFile, "t", "", "Bot Token file")
flag.Parse()
// Read the token from supplied file
Token = read(TokenFile)
DevSrvID = read("config") // TODO sloppy..
Version = "0.2.8"
Debug = false
if Debug {
OwnName = "Ragequitter - Test"
} else {
OwnName = "Ragequitter"
}
// set error logging file
if !Debug {
logFile, _ := os.OpenFile("./ragebot.err", os.O_WRONLY|os.O_CREATE|os.O_SYNC, 0755)
syscall.Dup2(int(logFile.Fd()), 1)
syscall.Dup2(int(logFile.Fd()), 2)
}
}
var buffer = make([][]byte, 0)
func main() {
err := loadSound(&buffer)
if err != nil {
fmt.Println("Error loading media: ", err)
return
}
discord, err := discordgo.New("Bot " + Token)
if err != nil {
fmt.Println("Error creating the Bot.")
return
}
// Register ready as a callback for the ready events.
discord.AddHandler(ready)
discord.AddHandler(messageCreate)
// Register guildCreate as a callback for the guildCreate events.
discord.AddHandler(guildCreate)
discord.AddHandler(messageReactionAdd)
// Open a websocket connection to Discord and begin listening
err = discord.Open()
if err != nil {
fmt.Println("Error opening connection, ", err)
return
}
// Wait here until CTRL-C or other term signal is received.
fmt.Println("Bot is now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the discord session.
discord.Close()
}
// This function will be called when the bot receives
// the "ready" event from Discord.
func ready(s *discordgo.Session, event *discordgo.Ready) {
// Set the playing status
s.UpdateStatus(0, "Σας παρακολουθώ")
}
// This function will be called (since we register it as a Handler)
// every time a new message is created on any channel that the authenticated bot
// has access to.
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
// This isn't required but it's a good practice.
if m.Author.ID == s.State.User.ID {
return
}
c, err := s.Channel(m.ChannelID)
if err != nil {
return
}
if !isOnDevelopmentServer(c.GuildID) && Debug {
// do not send messages when in development mode
return
}
simpleReplyText := ParseCommand(m)
if simpleReplyText != "" && ShouldSend(c.GuildID) {
s.ChannelMessageSend(m.ChannelID, simpleReplyText)
}
}
//case func(*Session, *Disconnect):
//return disconnectEventHandler(v)
func messageReactionAdd(s *discordgo.Session, d *discordgo.MessageReactionAdd) {
if d.Emoji.Name == "🖕" {
message, err := s.ChannelMessage(d.ChannelID, d.MessageID)
if err != nil {
return
}
if message.Author.ID != s.State.User.ID {
// not for me!
return
}
user, usrerr := s.User(d.UserID)
// TODO refactor to shared code for channel cheking
// TODO add functionality for private messages
c, err := s.Channel(d.ChannelID)
if err != nil || usrerr != nil {
return
}
if !isOnDevelopmentServer(c.GuildID) && Debug {
// do not send messages when in development mode
return
}
if ShouldSend(c.GuildID) {
msg := "Χμ όχι και πολύ ευγενικό αυτό " + user.Mention() + " ε; :poop:"
s.ChannelMessageSend(d.ChannelID, msg)
}
}
}
// This function will be called every time a new guild is joined
func guildCreate(s *discordgo.Session, event *discordgo.GuildCreate) {
if event.Guild.Unavailable {
return
}
if !isOnDevelopmentServer(event.Guild.ID) && Debug {
// Avoid spamming other servers when testing
return
}
for _, channel := range event.Guild.Channels {
if channel.ID == event.Guild.ID {
_, _ = s.ChannelMessageSend(channel.ID, "Χαίρετε :smile:")
s.GuildMemberNickname(event.Guild.ID, "@me", OwnName)
return
}
}
}
func isOnDevelopmentServer(guildId string) bool {
return DevSrvID == guildId
}