70 lines
1.6 KiB
Go
Raw Normal View History

2018-07-10 15:46:17 +00:00
package commands
import (
"strings"
2018-07-10 16:18:44 +00:00
"github.com/animenotifier/arn/stringutils"
2018-07-10 15:46:17 +00:00
"github.com/bwmarrin/discordgo"
)
var regions = map[string]string{
2018-07-10 16:04:58 +00:00
"africa": "465387147629953034",
"america": "465386843706359840",
"asia": "465386826006528001",
"australia": "465387169888862230",
"europe": "465386794914152448",
2018-07-10 15:46:17 +00:00
}
// Region sets the specific region role for the user.
func Region(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
2018-07-10 16:04:58 +00:00
if !strings.HasPrefix(msg.Content, "!region ") {
2018-07-10 15:46:17 +00:00
return false
}
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 true
}
// Check to see if user already has a region role
2018-07-10 16:14:08 +00:00
user, err := s.GuildMember(guildID, msg.Author.ID)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, "Error: User not found")
return true
}
2018-07-10 15:46:17 +00:00
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
2018-07-10 16:14:08 +00:00
s.GuildMemberRoleRemove(guildID, msg.Author.ID, id)
2018-07-10 15:46:17 +00:00
match = true
break
}
}
if match {
break
}
}
// Try to set the role
2018-07-10 16:14:08 +00:00
err = s.GuildMemberRoleAdd(guildID, msg.Author.ID, regions[region])
2018-07-10 15:46:17 +00:00
if err != nil {
s.ChannelMessageSend(msg.ChannelID, "The region role could not be set!")
return true
}
2018-07-10 16:18:44 +00:00
s.ChannelMessageSend(msg.ChannelID, "Set region "+stringutils.Capitalize(region)+" for your account!")
2018-07-10 15:46:17 +00:00
return true
}