Made requested changes

This commit is contained in:
Allen Lydiard 2018-07-10 04:51:34 -03:00
parent 75bd736c73
commit 8b9c2a8e99

View File

@ -10,6 +10,14 @@ import (
"github.com/bwmarrin/discordgo" "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. // OnMessageCreate is called every time a new message is created on any channel.
func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) { func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself // Ignore all messages created by the bot itself
@ -84,19 +92,14 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
// Set the specific region role for the user // Set the specific region role for the user
if strings.HasPrefix(msg.Content, "!region ") { if strings.HasPrefix(msg.Content, "!region ") {
region := strings.ToLower(msg.Content[len("!region "):])
regions := map[string]string{
"africa": "465876853236826112",
"america": "465876808311635979",
"asia": "465876834031108096",
"australia": "465876893036707840",
"europe": "465876773029019659",
}
region := msg.Content[len("!region "):]
// check to make sure the region is in the region map // check to make sure the region is in the region map
if _, ok := regions[region]; ok { 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 // Get the channel, this is used to get the guild ID
c, _ := s.Channel(msg.ChannelID) c, _ := s.Channel(msg.ChannelID)
@ -105,38 +108,32 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
for _, role := range user.Roles { for _, role := range user.Roles {
match := false match := false
// we also need to loop through our map because discord doesn't return roles as names
// We also need to loop through our map because discord doesn't return roles as names
// but rather IDs. // but rather IDs.
for _, id := range regions { for _, id := range regions {
if role == id { if role == id {
// remove the role and set match to true // Remove the role and set match to true.
s.GuildMemberRoleRemove(c.GuildID, msg.Author.ID, id) s.GuildMemberRoleRemove(c.GuildID, msg.Author.ID, id)
match = true match = true
break break
} }
} }
if match { if match {
break break
} }
} }
// try to set the role // Try to set the role.
err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region]) err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region])
if err != nil { if err != nil {
s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!") s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!")
return return
} }
s.ChannelMessageSend(msg.ChannelID, "The "+region+" role has been set on your user!") s.ChannelMessageSend(msg.ChannelID, "The "+region+" role has been set on your user!")
} else {
s.ChannelMessageSend(msg.ChannelID, "This is not a region!")
} }
return return
}
} }