TerraOceanPlugin/GoBot/discord/modules/trust.go

109 lines
2.6 KiB
Go

package modules
import (
"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, enforceArgumentCount(
config.ChangeTrustUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
member, err := backend.GetMemberFromUserFriendlyName(s, command[1])
if err == backend.ErrMemberNotFound {
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustTargetNotFound,
config.ErrorColour,
).Send(s, m.ChannelID)
return
}
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
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
}
trustMessage[msg.ID] = member.User.ID
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiOne)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiTwo)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiThree)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiFour)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiFive)
s.MessageReactionAdd(m.ChannelID, msg.ID, emojiCheck)
},
)))
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:
message.AuditInfo(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 {
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustRecentlyChanged,
config.ErrorColour,
).Send(s, r.ChannelID)
return
}
if err != nil {
message.AuditError(s, r.ChannelID, err)
return
}
message.InitNewEmbed(
config.ChangeTrustSuccessTitle,
config.ChangeTrustSuccessDescription,
config.SuccessColour,
).Send(s, r.ChannelID)
}