forked from chanbakjsd/TerraOceanPlugin
108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
|
package modules
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/bwmarrin/discordgo"
|
||
|
|
||
|
"TerraOceanBot/db"
|
||
|
"TerraOceanBot/discord/backend"
|
||
|
)
|
||
|
|
||
|
var trustMessage = make(map[string]string)
|
||
|
|
||
|
func changeTrust(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
|
||
|
if !enforceDM(s, m) {
|
||
|
return
|
||
|
}
|
||
|
if !membersOnly(s, m) {
|
||
|
return
|
||
|
}
|
||
|
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 {
|
||
|
auditLog(s, fmt.Sprintf("Error while processing change trust request for <@%s>: %s", m.Author.ID, err.Error()))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
message, err := s.ChannelMessageSendEmbed(
|
||
|
m.ChannelID,
|
||
|
createTrustEmbed(member.User.Username+"#"+member.User.Discriminator, member.Nick),
|
||
|
)
|
||
|
if err != nil {
|
||
|
auditLog(s, fmt.Sprintf("Error while sending change trust request for <@%s>: %s", m.Author.ID, err.Error()))
|
||
|
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)
|
||
|
}
|
||
|
|
||
|
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()
|
||
|
}
|
||
|
|
||
|
func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
|
||
|
if r.UserID == s.State.User.ID {
|
||
|
return
|
||
|
}
|
||
|
target, ok := trustMessage[r.MessageID]
|
||
|
|
||
|
if !ok {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
delete(trustMessage, r.MessageID)
|
||
|
|
||
|
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:
|
||
|
value = db.NuclearOptionVote
|
||
|
default:
|
||
|
auditLog(s, "Reaction "+r.Emoji.Name+" was added to trust message ID "+r.MessageID)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
err := backend.UpdateTrust(r.UserID, target, value)
|
||
|
if err == db.ErrRecentlyChanged {
|
||
|
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 {
|
||
|
auditLog(s, fmt.Sprintf("Error while changing the trust value for <@%s> with target <@%s>: %s", r.UserID, target, err.Error()))
|
||
|
return
|
||
|
}
|
||
|
|
||
|
s.ChannelMessageSend(r.ChannelID, "已改变你给这名玩家的分数已被记录。\nThe score for this player has been updated.")
|
||
|
}
|