-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
175 lines (158 loc) · 4.78 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
169
170
171
172
173
174
175
// Discordrole is a simple discord bot written in Go. It assigns a role to a user when they add a reaction to a specific message.
package main
import (
"flag"
"log"
"strings"
"regexp"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
)
var (
token string
ownerID = "0"
activeChannel = "roles"
verbose = false
emoji = "🍆"
)
func init() {
flag.StringVar(&token, "t", "", "Bot `token` (required)")
flag.StringVar(&ownerID, "o", "", "Owner user `id` (only this owner ID and server owner can use the register command)")
flag.StringVar(&activeChannel, "c", "roles", "Channel `name` to use")
flag.StringVar(&emoji, "e", "🍆", "Emoji to use as reaction button")
flag.BoolVar(&verbose, "v", false, "Verbose logging")
flag.Parse()
if token == "" {
flag.Usage()
os.Exit(1)
}
}
func debug(v ...interface{}) {
if verbose {
fa := "Debug: "
v = append([]interface{}{fa}, v...)
log.Print(v...)
}
}
func main() {
discord, err := discordgo.New("Bot " + token)
if err != nil {
log.Fatal("error creating Discord session,", err)
return
}
discord.AddHandler(messageCreate)
discord.AddHandler(messageReactionAdd)
discord.AddHandler(messageReactionRemove)
// discord.AddHandler(ready)
err = discord.Open()
if err != nil {
log.Fatal("error opening connection,", err)
return
}
guilds, err := discord.UserGuilds(100, "", "")
log.Print("Running on servers:")
if len(guilds) == 0 {
log.Print("\t(none)")
}
for index := range guilds {
guild := guilds[index]
log.Print("\t", guild.Name, " (", guild.ID, ")")
}
log.Print("channel name: ", activeChannel)
log.Print("Join URL:")
log.Print("https://discordapp.com/api/oauth2/authorize?scope=bot&permissions=268446720&client_id=", discord.State.User.ID)
user, err := discord.User("@me")
if err != nil {
log.Print("Bot running. CTRL-C to exit.")
} else {
log.Print("Bot running as ", user.Username, "#", user.Discriminator, ". CTRL-C to exit.")
}
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
discord.Close()
}
func reactionUpdate(s *discordgo.Session, MessageID *string, ChannelID *string) (bool, string, string) {
channel, _ := s.Channel(*ChannelID)
if channel.Name != "roles" {
return false, "", ""
}
message, _ := s.ChannelMessage(*ChannelID, *MessageID)
if message.Author.ID != s.State.User.ID {
return false, "", ""
}
getRole := regexp.MustCompile(`<@&([0-9]+)>`)
roleID := getRole.FindStringSubmatch(message.Content)[1]
return true, channel.GuildID, roleID
}
func messageReactionAdd(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
shouldRun, GuildID, roleID := reactionUpdate(s, &r.MessageID, &r.ChannelID)
if !shouldRun || r.UserID == s.State.User.ID {
return
}
debug("Giving ", roleID, " to ", r.UserID)
err := s.GuildMemberRoleAdd(GuildID, r.UserID, roleID)
if err != nil {
log.Print(err)
debug("try moving the bot's role up the role list")
}
}
func messageReactionRemove(s *discordgo.Session, r *discordgo.MessageReactionRemove) {
shouldRun, GuildID, roleID := reactionUpdate(s, &r.MessageID, &r.ChannelID)
if !shouldRun || r.UserID == s.State.User.ID {
return
}
debug("Removing ", roleID, " from ", r.UserID)
err := s.GuildMemberRoleRemove(GuildID, r.UserID, roleID)
if err != nil {
log.Print(err)
}
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
channel, err := s.Channel(m.ChannelID)
if err != nil {
log.Print("Error getting channel:")
log.Print(err)
return
}
guild, err := s.Guild(channel.GuildID)
if err != nil {
log.Print("Error getting guild:")
log.Print(err)
return
}
if m.Author.ID != ownerID && m.Author.ID != guild.OwnerID {
return
}
if strings.HasPrefix(m.Content, "register") {
if channel.Name != activeChannel {
debug("register command only works in channels with name: ", activeChannel)
return
}
getRole := regexp.MustCompile(`<@&([0-9]+)> ?(.*)?`)
regexout := getRole.FindAllStringSubmatch(m.Content, -1)
if regexout != nil {
roleID := regexout[0][1]
description := ""
text := []string{}
if regexout[0][2] != "" {
description = regexout[0][2]
log.Print("registering ", roleID, ": ", description)
text = []string{"<@&", roleID, ">\n", description}
} else {
log.Print("registering ", roleID, ": ", description)
text = []string{"<@&", roleID, ">\n", description}
}
newm, err := s.ChannelMessageSend(m.ChannelID, strings.Join(text, ""))
if err == nil {
s.MessageReactionAdd(newm.ChannelID, newm.ID, emoji)
s.ChannelMessageDelete(channel.ID, m.ID)
}
}
}
}