2018-07-10 15:46:17 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 true
|
|
|
|
}
|
|
|
|
|
|
|
|
s.ChannelMessageSend(msg.ChannelID, "Set region "+region+" for your account!")
|
|
|
|
return true
|
|
|
|
}
|