From e80e7952a66aa222596210a6e244f2243ddecfba Mon Sep 17 00:00:00 2001 From: Allen Lydiard Date: Mon, 9 Jul 2018 07:38:44 -0300 Subject: [PATCH 1/4] Starting work on the discord bot --- bots/discord/OnMessageCreate.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bots/discord/OnMessageCreate.go b/bots/discord/OnMessageCreate.go index 55a0efc3..c0ba93f9 100644 --- a/bots/discord/OnMessageCreate.go +++ b/bots/discord/OnMessageCreate.go @@ -23,7 +23,8 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) { **!animelist** [username] **!play** [status text] **!randomquote** -**!source**`) +**!source** +**!region** [region]`) } // Has the bot been mentioned? From 816a44692d7c9a3687e6cd7928ad6ba026da1de0 Mon Sep 17 00:00:00 2001 From: Allen Lydiard Date: Mon, 9 Jul 2018 08:40:48 -0300 Subject: [PATCH 2/4] Some more work on the discord bot --- bots/discord/OnMessageCreate.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/bots/discord/OnMessageCreate.go b/bots/discord/OnMessageCreate.go index c0ba93f9..7a3a69f9 100644 --- a/bots/discord/OnMessageCreate.go +++ b/bots/discord/OnMessageCreate.go @@ -81,4 +81,37 @@ 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 ") { + + 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 + if _, ok := regions[region]; ok { + // try to set the role + c, _ := s.Channel(msg.ChannelID) + 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!") + + } else { + s.ChannelMessageSend(msg.ChannelID, "This is not a region!") + } + + return + } } From 75bd736c7326ec73a0959f48d2512cfb5033369d Mon Sep 17 00:00:00 2001 From: Allen Lydiard Date: Mon, 9 Jul 2018 09:01:40 -0300 Subject: [PATCH 3/4] Basic region command done --- bots/discord/OnMessageCreate.go | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/bots/discord/OnMessageCreate.go b/bots/discord/OnMessageCreate.go index 7a3a69f9..c6a176b4 100644 --- a/bots/discord/OnMessageCreate.go +++ b/bots/discord/OnMessageCreate.go @@ -97,10 +97,35 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) { // check to make sure the region is in the region map if _, ok := regions[region]; ok { - // try to set the role + // Get the channel, this is used to get the guild ID c, _ := s.Channel(msg.ChannelID) - err := s.GuildMemberRoleAdd(c.GuildID, msg.Author.ID, regions[region]) + // 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 From 8b9c2a8e99ee19f595a97d0720c0f1f5d2e3c3e7 Mon Sep 17 00:00:00 2001 From: Allen Lydiard Date: Tue, 10 Jul 2018 04:51:34 -0300 Subject: [PATCH 4/4] Made requested changes --- bots/discord/OnMessageCreate.go | 95 ++++++++++++++++----------------- 1 file changed, 46 insertions(+), 49 deletions(-) diff --git a/bots/discord/OnMessageCreate.go b/bots/discord/OnMessageCreate.go index c6a176b4..01193197 100644 --- a/bots/discord/OnMessageCreate.go +++ b/bots/discord/OnMessageCreate.go @@ -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 @@ -84,59 +92,48 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) { // Set the specific region role for the user if strings.HasPrefix(msg.Content, "!region ") { - - regions := map[string]string{ - "africa": "465876853236826112", - "america": "465876808311635979", - "asia": "465876834031108096", - "australia": "465876893036707840", - "europe": "465876773029019659", - } - - region := msg.Content[len("!region "):] + region := strings.ToLower(msg.Content[len("!region "):]) // check to make sure the region is in the region map - if _, ok := regions[region]; ok { - // 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!") - - } else { + if _, ok := regions[region]; !ok { s.ChannelMessageSend(msg.ChannelID, "This is not a region!") + return } - 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 }