Added Discord account verification

This commit is contained in:
Eduard Urbach 2018-11-14 10:33:46 +09:00
parent c34a4b763b
commit 2b303d93c1
3 changed files with 107 additions and 71 deletions

View File

@ -14,9 +14,9 @@ var allCommands = []Command{
commands.AnimeSearch,
commands.Play,
commands.RandomQuote,
commands.Region,
commands.Roles,
commands.Source,
commands.Verify,
}
// OnMessageCreate is called every time a new message is created on any channel.
@ -33,7 +33,7 @@ func OnMessageCreate(s *discordgo.Session, msg *discordgo.MessageCreate) {
**!play** [status text]
**!randomquote**
**!source**
**!region** [region]`)
**!verify** [username]`)
}
// Has the bot been mentioned?

View File

@ -1,69 +0,0 @@
package commands
import (
"strings"
"github.com/animenotifier/arn/stringutils"
"github.com/bwmarrin/discordgo"
)
var regions = map[string]string{
"africa": "465387147629953034",
"america": "465386843706359840",
"asia": "465386826006528001",
"australia": "465387169888862230",
"europe": "465386794914152448",
}
// Region sets the specific region role for the user.
func Region(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
if !strings.HasPrefix(msg.Content, "!region ") {
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
user, err := s.GuildMember(guildID, msg.Author.ID)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, "Error: User not found")
return true
}
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(guildID, msg.Author.ID, id)
match = true
break
}
}
if match {
break
}
}
// Try to set the role
err = s.GuildMemberRoleAdd(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 "+stringutils.Capitalize(region)+" for your account!")
return true
}

View File

@ -0,0 +1,105 @@
package commands
import (
"fmt"
"strings"
"github.com/animenotifier/arn"
"github.com/fatih/color"
"github.com/pariz/gountries"
"github.com/bwmarrin/discordgo"
)
var query = gountries.New()
var regions = map[string]string{
"africa": "465387147629953034",
"americas": "465386843706359840",
"asia": "465386826006528001",
"oceania": "465387169888862230",
"europe": "465386794914152448",
}
const (
verifiedRole = "512044929195704330"
editorRole = "141849207404363776"
staffRole = "218221363918274560"
)
// Verify verifies that the given user has an account on notify.moe.
func Verify(s *discordgo.Session, msg *discordgo.MessageCreate) bool {
discordTag := msg.Author.Username + "#" + msg.Author.Discriminator
if msg.Content == "!verify" {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("To verify your account, first add `%s` as your Discord account on https://notify.moe/settings/accounts, then type `!verify` followed by your username on notify.moe, e.g. `!verify MyName`", discordTag))
return true
}
if !strings.HasPrefix(msg.Content, "!verify ") {
return false
}
arnUserName := strings.TrimSpace(msg.Content[len("!verify "):])
user, err := arn.GetUserByNick(arnUserName)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("User `%s` doesn't seem to exist on notify.moe", arnUserName))
return true
}
if user.Accounts.Discord.Nick == "" {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("You haven't set up your Discord account `%s` on https://notify.moe/settings/accounts", discordTag))
return true
}
if user.Accounts.Discord.Nick != discordTag {
s.ChannelMessageSend(msg.ChannelID, fmt.Sprintf("Mismatching Discord accounts: `%s` and `%s`", user.Accounts.Discord.Nick, discordTag))
return true
}
// Try to set the verified role
err = s.GuildMemberRoleAdd(guildID, msg.Author.ID, verifiedRole)
if err != nil {
s.ChannelMessageSend(msg.ChannelID, "There was an error adding the Verified role to your account!")
return true
}
// Give editor role
if user.Role == "editor" {
s.GuildMemberRoleAdd(guildID, msg.Author.ID, editorRole)
s.GuildMemberRoleAdd(guildID, msg.Author.ID, staffRole)
}
// Give region role
if user.Location.CountryCode != "" {
country, err := query.FindCountryByAlpha(user.Location.CountryCode)
if err != nil {
color.Red("Error querying country code: %s", err.Error())
} else {
regionRole, exists := regions[strings.ToLower(country.Region)]
if !exists {
color.Red("Error getting region role for: %s", country.Region)
} else {
// Remove old region role
for _, roleID := range regions {
s.GuildMemberRoleRemove(guildID, msg.Author.ID, roleID)
}
// Add new region role
s.GuildMemberRoleAdd(guildID, msg.Author.ID, regionRole)
}
}
}
// Update notify.moe user account
user.Accounts.Discord.Verified = true
user.Save()
// Send success message
s.ChannelMessageSend(msg.ChannelID, "Thank you, you are now a verified member of the notify.moe community!")
return true
}