go: Refactor trust.go

pull/12/head
Luther Wen Xu 2019-10-13 21:23:57 +07:00
parent 6211334d91
commit 4be6408655
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
2 changed files with 44 additions and 30 deletions

@ -113,3 +113,16 @@ const SetMCUsernameConfirmDeniedDescription = "你的用户名没有被改变。
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."
const ChangeTrustUsage = "!trust <更改对象>\n!trust <target>"
const ChangeTrustPromptTitle = "更新玩家评分 Update Player Score"
const ChangeTrustPromptColour = 0xF0F000
const ChangeTrustPromptID = "Discord编号 Discord ID"
const ChangeTrustPromptNick = "昵称 Nickname"
const ChangeTrustPromptReminderTitle = "提醒 Reminder"
const ChangeTrustPromptReminderDescription = "你一个月只能更换对一个玩家的评分一次。\nYou can only change your trust towards a player once every month."
const ChangeTrustSuccessTitle = "成功更新玩家评分 Update Player Score Success"
const ChangeTrustSuccessDescription = "已改变你给这名玩家的分数已被记录。\nThe score for this player has been updated."
const ErrorChangeTrustTitle = "调整信誉分错误 Change Trust Error"
const ErrorChangeTrustTargetNotFound = "无法寻找到改变目标。\nCannot find the target user."
const ErrorChangeTrustRecentlyChanged = "你在一个月内有设定过该名玩家的分数。你这次的更动没有被记录。\nYou have changed your score for this player within the last month. Your change was not recorded."

@ -1,27 +1,26 @@
package modules
import (
"strings"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
var trustMessage = make(map[string]string)
var changeTrust = enforceDM(memberFilter(true,
var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
config.ChangeTrustUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command) < 2 {
s.ChannelMessageSend(m.ChannelID, "指令的使用方法是`!trust <你要改信任度的人>`.\nUsage: `!trust <the user>`")
return
}
member, err := backend.GetMemberFromUserFriendlyName(s, strings.SplitN(m.Content, " ", 2)[1])
member, err := backend.GetMemberFromUserFriendlyName(s, command[1])
if err == backend.ErrMemberNotFound {
s.ChannelMessageSend(m.ChannelID, "无法找到您所要求的玩家。\nCannot find the requested player.")
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustTargetNotFound,
config.ErrorColour,
).Send(s, m.ChannelID)
return
}
if err != nil {
@ -29,10 +28,17 @@ var changeTrust = enforceDM(memberFilter(true,
return
}
msg, err := s.ChannelMessageSendEmbed(
m.ChannelID,
createTrustEmbed(member.User.Username+"#"+member.User.Discriminator, member.Nick),
)
embed := message.NewEmbed().
SetTitle(config.ChangeTrustPromptTitle).
SetColour(config.ChangeTrustPromptColour).
AddField(config.ChangeTrustPromptID, member.User.Username+"#"+member.User.Discriminator)
if member.Nick != "" {
embed.AddField(config.ChangeTrustPromptNick, member.Nick)
}
embed.AddField(config.ChangeTrustPromptReminderTitle, config.ChangeTrustPromptReminderDescription)
msg, err := embed.Send(s, m.ChannelID)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
@ -47,19 +53,7 @@ var changeTrust = enforceDM(memberFilter(true,
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiFive)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiCheck)
},
))
func createTrustEmbed(username, nick string) *discordgo.MessageEmbed {
embed := message.NewEmbed().
SetColour(0x00FFFF).
SetTitle("更新玩家评分Update Player Score").
AddField("ID", username)
if nick != "" {
embed.AddField("昵称Nickname", nick)
}
embed.AddField("提醒Reminder", "你一个月只能更换对一个玩家的评分一次。You can only change your trust towards a player once every month.")
return embed.Build()
}
)))
func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
if r.UserID == s.State.User.ID {
@ -94,8 +88,11 @@ func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd)
err := backend.UpdateTrust(r.UserID, target, value)
if err == db.ErrRecentlyChanged {
//TODO Move to embed.
s.ChannelMessageSend(r.ChannelID, "你在一个月内有设定过该名玩家的分数。你这次的更动没有被记录。\nYou have changed your score for this player within the last month. Your change was not recorded.")
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustRecentlyChanged,
config.ErrorColour,
).Send(s, r.ChannelID)
return
}
if err != nil {
@ -103,5 +100,9 @@ func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd)
return
}
s.ChannelMessageSend(r.ChannelID, "已改变你给这名玩家的分数已被记录。\nThe score for this player has been updated.")
message.InitNewEmbed(
config.ChangeTrustSuccessTitle,
config.ChangeTrustSuccessDescription,
config.SuccessColour,
).Send(s, r.ChannelID)
}