TerraOceanPlugin/GoBot/discord/modules/trust.go

103 lines
2.6 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
2019-10-11 14:29:07 +07:00
import (
"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-13 13:23:57 +07:00
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
2019-10-11 14:29:07 +07:00
)
2019-10-13 13:23:57 +07:00
var changeTrust = enforceDM(memberFilter(true, enforceArgumentCount(
config.ChangeTrustUsage, 2,
2019-10-12 13:49:41 +07:00
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
2019-10-13 13:23:57 +07:00
member, err := backend.GetMemberFromUserFriendlyName(s, command[1])
2019-10-12 13:49:41 +07:00
if err == backend.ErrMemberNotFound {
2019-10-13 13:23:57 +07:00
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustTargetNotFound,
config.ErrorColour,
).Send(s, m.ChannelID)
2019-10-12 13:49:41 +07:00
return
}
if err != nil {
message.AuditError(s, m.ChannelID, err)
2019-10-12 13:49:41 +07:00
return
}
2019-10-13 13:23:57 +07:00
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)
2019-10-12 13:49:41 +07:00
if err != nil {
message.AuditError(s, m.ChannelID, err)
2019-10-12 13:49:41 +07:00
return
}
2019-10-14 10:01:25 +07:00
queueForReact(
msg.ID,
[]string{emojiOne, emojiTwo, emojiThree, emojiFour, emojiFive, emojiCheck},
member.User.ID,
trustCallback,
)
2019-10-12 13:49:41 +07:00
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)
2019-10-12 13:49:41 +07:00
},
2019-10-13 13:23:57 +07:00
)))
2019-10-11 14:29:07 +07:00
2019-10-14 10:01:25 +07:00
func trustCallback(s *discordgo.Session, r *discordgo.MessageReactionAdd, info interface{}) {
target := info.(string)
2019-10-12 06:40:46 +07:00
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:
message.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-13 13:23:57 +07:00
message.InitNewEmbed(
config.ErrorChangeTrustTitle,
config.ErrorChangeTrustRecentlyChanged,
config.ErrorColour,
).Send(s, r.ChannelID)
2019-10-11 14:29:07 +07:00
return
}
if err != nil {
message.AuditError(s, r.ChannelID, err)
2019-10-11 14:29:07 +07:00
return
}
2019-10-13 13:23:57 +07:00
message.InitNewEmbed(
config.ChangeTrustSuccessTitle,
config.ChangeTrustSuccessDescription,
config.SuccessColour,
).Send(s, r.ChannelID)
2019-10-11 14:29:07 +07:00
}