Merge pull request #182 from FM1337/go

Added region role command
This commit is contained in:
Eduard Urbach 2018-07-10 23:47:43 +09:00 committed by GitHub
commit 957de3856b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,14 @@ import (
"github.com/bwmarrin/discordgo"
)
var regions = map[string]string{
"africa": "465876853236826112",
"america": "465876808311635979",
"asia": "465876834031108096",
"australia": "465876893036707840",
"europe": "465876773029019659",
}
// OnMessageCreate is called every time a new message is created on any channel.
func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
@ -23,7 +31,8 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
**!animelist** [username]
**!play** [status text]
**!randomquote**
**!source**`)
**!source**
**!region** [region]`)
}
// Has the bot been mentioned?
@ -80,4 +89,51 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
s.ChannelMessageSend(msg.ChannelID, msg.Author.Mention()+" B-baaaaaaaka! Y..you...you want to...TOUCH MY CODE?!\n\nhttps://github.com/animenotifier/notify.moe/tree/go/bots/discord")
return
}
// Set the specific region role for the user
if strings.HasPrefix(msg.Content, "!region ") {
region := strings.ToLower(msg.Content[len("!region "):])
// check to make sure the region is in the region map
if _, ok := regions[region]; !ok {
s.ChannelMessageSend(msg.ChannelID, "This is not a region!")
return
}
// Get the channel, this is used to get the guild ID
c, _ := s.Channel(msg.ChannelID)
// Check to see if user already has a region role
user, _ := s.GuildMember(c.GuildID, msg.Author.ID)
for _, role := range user.Roles {
match := false
// We also need to loop through our map because discord doesn't return roles as names
// but rather IDs.
for _, id := range regions {
if role == id {
// Remove the role and set match to true.
s.GuildMemberRoleRemove(c.GuildID, msg.Author.ID, id)
match = true
break
}
}
if match {
break
}
}
// Try to set the role.
err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region])
if err != nil {
s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!")
return
}
s.ChannelMessageSend(msg.ChannelID, "The "+region+" role has been set on your user!")
}
return
}