go: Implement '!setmcusernae' command

master
Luther Wen Xu 2019-10-13 17:59:35 +07:00
parent e9d5730300
commit f3c69655e2
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
7 changed files with 151 additions and 5 deletions

@ -35,7 +35,7 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
_, err = db.Exec("CREATE TABLE IF NOT EXISTS minecraftUsername (discordId INTEGER, minecraftUsername TEXT, UNIQUE(discordId), UNIQUE(minecraftUsername))") _, err = db.Exec("CREATE TABLE IF NOT EXISTS minecraftUsername (discordId TEXT, minecraftUsername TEXT, UNIQUE(discordId), UNIQUE(minecraftUsername))")
if err != nil { if err != nil {
panic(err) panic(err)
} }

@ -0,0 +1,44 @@
package db
func GetMinecraftUsername(discordID string) (string, error) {
rows, err := db.Query("SELECT minecraftUsername FROM minecraftUsername WHERE discordId=?", discordID)
if err != nil {
return "", err
}
defer rows.Close()
if rows.Next() {
var username string
err := rows.Scan(&username)
if err != nil {
return "", err
}
return username, nil
}
return "", ErrNotFound
}
func GetDiscordID(username string) (string, error) {
rows, err := db.Query("SELECT discordId FROM minecraftUsername WHERE minecraftUsername=?", username)
if err != nil {
return "", err
}
defer rows.Close()
if rows.Next() {
var id string
err := rows.Scan(&id)
if err != nil {
return "", err
}
return id, nil
}
return "", ErrNotFound
}
func SetMinecraftUsername(discordID, username string) error {
_, err := GetDiscordID(username)
if err != ErrNotFound {
return ErrAlreadyExists
}
_, err = db.Exec("REPLACE INTO minecraftUsername(discordId, minecraftUsername) VALUES(?,?)", discordID, username)
return err
}

@ -97,5 +97,19 @@ const ValidateSuccess = "已向验证码制造者发送了请求。\nSent a requ
const ErrorValidateTitle = "验证错误Validation Error" const ErrorValidateTitle = "验证错误Validation Error"
const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one from its creator." const ErrorValidationCodeReuse = "该验证码已被使用过。请向验证码制造者要求新的验证码。\nThis validation code has been used before. Please request a new one from its creator."
const ErrorValidationCodeNotExist = "该验证码不存在。请使用一位会员给你的验证码。\nThis validation code doesn't exist. Please use one given by a member." const ErrorValidationCodeNotExist = "该验证码不存在。请使用一位会员给你的验证码。\nThis validation code doesn't exist. Please use one given by a member."
const ErrorValidationCodeNotOwnedByMember = "验证码制造者不是会员。\nvalidation code creator is no longer a member." const ErrorValidationCodeNotOwnedByMember = "验证码制造者不是会员。\nValidation code creator is no longer a member."
const ErrorValidationCodeCreatorDisabledPM = "验证码制造者关闭了私信。\nValidation code creator did not enable DMs." const ErrorValidationCodeCreatorDisabledPM = "验证码制造者关闭了私信。\nValidation code creator did not enable DMs."
const SetMCUsernameUsage = "!setmcusername <MC用户名>\n!setmcusername <MC Username>"
const SetMCUsernameConfirmTitle = "设置MC用户名确认 Set MC Username Confirmation"
const SetMCUsernameConfirmDescription = "你在改变了MC用户名以后就不能再以原有用户名登录了请确保你已把你所要的东西放进箱子里。\nYou will not be able to login with your old username once you've changed your username. Please make sure you've placed everything in chests or similar."
const SetMCUsernameSuccessTitle = "设置用户名成功 Set MC Username Successful"
const SetMCUsernameSuccessDescription = "设置用户名成功!请使用新的用户名登录。\nMC username has been set successfully! Please use your new username to login."
const SetMCUsernameConfirmColour = 0xF0F000
const SetMCUsernameConfirmOldUsername = "旧用户名Old Username"
const SetMCUsernameConfirmNewUsername = "新用户名New Username"
const SetMCUsernameConfirmDeniedTitle = "成功取消设置用户名 Cancelled Set MC Username"
const SetMCUsernameConfirmDeniedDescription = "你的用户名没有被改变。请继续使用旧用户名。\nYour username was not changed. Please continue using the old username."
const SetMCUsernameErrorTitle = "设定MC用户名错误 Set MC Username Error"
const ErrorSetMCUsernameDuplicate = "你要设置的用户名已经是你目前记录了的用户名了。\nThe username you want to set is equal to the username in the record."
const ErrorSetMCUsernameAlreadyOwned = "你要设置的用户名已被其他人使用了。\nThe username you requested is already owned by another player."

@ -20,6 +20,7 @@ func StartBot(token string, kill chan bool) {
dg.AddHandler(modules.CheckForVote) dg.AddHandler(modules.CheckForVote)
dg.AddHandler(modules.CheckForTrustUpdate) dg.AddHandler(modules.CheckForTrustUpdate)
dg.AddHandler(modules.CheckForInvite) dg.AddHandler(modules.CheckForInvite)
dg.AddHandler(modules.ConfirmMinecraftUsername)
if err := dg.Open(); err != nil { if err := dg.Open(); err != nil {
panic(err) panic(err)

@ -33,4 +33,8 @@ var Commands = []Command{
Name: "!trust", Name: "!trust",
Handler: changeTrust, Handler: changeTrust,
}, },
Command{
Name: "!setmcusername",
Handler: updateMinecraftUsername,
},
} }

@ -73,13 +73,13 @@ func (e *embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message
return s.ChannelMessageSendEmbed(channelID, e.Build()) return s.ChannelMessageSendEmbed(channelID, e.Build())
} }
func (e *embed) SendPM(s *discordgo.Session, userID string) { func (e *embed) SendPM(s *discordgo.Session, userID string) (*discordgo.Message, error) {
channel, err := s.UserChannelCreate(userID) channel, err := s.UserChannelCreate(userID)
if err != nil { if err != nil {
auditError(s, "", err) auditError(s, "", err)
return return nil, err
} }
e.Send(s, channel.ID) return e.Send(s, channel.ID)
} }
func (e *embed) Edit(s *discordgo.Session, channelID, messageID string) { func (e *embed) Edit(s *discordgo.Session, channelID, messageID string) {

@ -0,0 +1,83 @@
package modules
import (
"github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
)
var minecraftConfirmReact = make(map[string]string)
var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
config.SetMCUsernameUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
oldUsername, err := db.GetMinecraftUsername(m.Author.ID)
if err != nil {
if err == db.ErrNotFound {
processUsernameUpdate(s, m.Author.ID, m.ChannelID, command[1])
return
}
auditError(s, m.ChannelID, err)
return
}
if oldUsername == command[1] {
initNewEmbed(config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameDuplicate, config.ErrorColour).
Send(s, m.ChannelID)
return
}
msg, err := initNewEmbed(
config.SetMCUsernameConfirmTitle,
config.SetMCUsernameConfirmDescription,
config.SetMCUsernameConfirmColour,
).AddField(config.SetMCUsernameConfirmOldUsername, oldUsername).
AddField(config.SetMCUsernameConfirmNewUsername, command[1]).
SendPM(s, m.Author.ID)
if err != nil {
auditError(s, m.ChannelID, err)
}
minecraftConfirmReact[msg.ID] = command[1]
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiX)
},
))
func ConfirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
if r.UserID == s.State.User.ID {
return
}
newUsername, ok := minecraftConfirmReact[r.MessageID]
if !ok {
return
}
delete(minecraftConfirmReact, r.MessageID)
switch r.Emoji.Name {
case emojiCheck:
case emojiX:
initNewEmbed(config.SetMCUsernameConfirmDeniedTitle, config.SetMCUsernameConfirmDeniedDescription, config.SuccessColour).
Send(s, r.ChannelID)
return
}
processUsernameUpdate(s, r.UserID, r.ChannelID, newUsername)
}
func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUsername string) {
err := db.SetMinecraftUsername(discordID, newUsername)
if err == db.ErrAlreadyExists {
initNewEmbed(
config.SetMCUsernameErrorTitle,
config.ErrorSetMCUsernameAlreadyOwned,
config.ErrorColour,
).Send(s, channelID)
return
}
initNewEmbed(
config.SetMCUsernameSuccessTitle,
config.SetMCUsernameSuccessDescription,
config.SuccessColour,
).Send(s, channelID)
}