TerraOceanPlugin/GoBot/discord/modules/trust.go

103 lines
2.9 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
2019-10-11 14:29:07 +07:00
import (
"strings"
"github.com/bwmarrin/discordgo"
2019-10-12 08:07:10 +07:00
"TerraOceanBot/db"
2019-10-12 09:45:43 +07:00
"TerraOceanBot/discord/backend"
2019-10-11 14:29:07 +07:00
)
var trustMessage = make(map[string]string)
2019-10-12 13:49:41 +07:00
var changeTrust = enforceDM(memberFilter(true,
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])
if err == backend.ErrMemberNotFound {
s.ChannelMessageSend(m.ChannelID, "无法找到您所要求的玩家。\nCannot find the requested player.")
return
}
if err != nil {
auditError(s, m.ChannelID, err)
return
}
message, err := s.ChannelMessageSendEmbed(
m.ChannelID,
createTrustEmbed(member.User.Username+"#"+member.User.Discriminator, member.Nick),
)
if err != nil {
auditError(s, m.ChannelID, err)
return
}
trustMessage[message.ID] = member.User.ID
s.MessageReactionAdd(m.ChannelID, message.ID, emojiOne)
s.MessageReactionAdd(m.ChannelID, message.ID, emojiTwo)
s.MessageReactionAdd(m.ChannelID, message.ID, emojiThree)
s.MessageReactionAdd(m.ChannelID, message.ID, emojiFour)
s.MessageReactionAdd(m.ChannelID, message.ID, emojiFive)
s.MessageReactionAdd(m.ChannelID, message.ID, emojiCheck)
},
))
2019-10-11 14:29:07 +07:00
func createTrustEmbed(username, nick string) *discordgo.MessageEmbed {
embed := 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()
}
2019-10-12 09:45:43 +07:00
func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
2019-10-11 14:29:07 +07:00
if r.UserID == s.State.User.ID {
return
}
target, ok := trustMessage[r.MessageID]
if !ok {
return
}
2019-10-12 06:40:46 +07:00
delete(trustMessage, r.MessageID)
2019-10-11 14:29:07 +07:00
var value int
switch r.Emoji.Name {
case emojiOne:
value = 1
case emojiTwo:
value = 2
case emojiThree:
value = 3
case emojiFour:
value = 4
case emojiFive:
value = 5
case emojiCheck:
2019-10-12 08:07:10 +07:00
value = db.NuclearOptionVote
2019-10-11 14:29:07 +07:00
default:
2019-10-12 13:49:41 +07:00
auditInfo(s, "Reaction "+r.Emoji.Name+" was added to trust message ID "+r.MessageID)
2019-10-11 14:29:07 +07:00
return
}
2019-10-12 09:45:43 +07:00
err := backend.UpdateTrust(r.UserID, target, value)
2019-10-12 08:07:10 +07:00
if err == db.ErrRecentlyChanged {
2019-10-11 14:29:07 +07:00
s.ChannelMessageSend(r.ChannelID, "你在一个月内有设定过该名玩家的分数。你这次的更动没有被记录。\nYou have changed your score for this player within the last month. Your change was not recorded.")
return
}
if err != nil {
2019-10-12 13:49:41 +07:00
auditError(s, r.ChannelID, err)
2019-10-11 14:29:07 +07:00
return
}
s.ChannelMessageSend(r.ChannelID, "已改变你给这名玩家的分数已被记录。\nThe score for this player has been updated.")
}