143 lines
3.7 KiB
Go
143 lines
3.7 KiB
Go
package modules
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"TerraOceanBot/db"
|
|
"TerraOceanBot/discord/config"
|
|
"TerraOceanBot/discord/message"
|
|
"TerraOceanBot/server"
|
|
)
|
|
|
|
const (
|
|
emojiOne = "1⃣"
|
|
emojiTwo = "2⃣"
|
|
emojiThree = "3⃣"
|
|
emojiFour = "4⃣"
|
|
emojiFive = "5⃣"
|
|
emojiCheck = "✅"
|
|
emojiX = "❌"
|
|
)
|
|
|
|
func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
|
|
if r.UserID == s.State.User.ID {
|
|
return
|
|
}
|
|
voteID, err := db.GetVoteFromMessageID(r.MessageID)
|
|
if err == db.ErrNotFound {
|
|
return
|
|
}
|
|
if err != nil {
|
|
message.AuditError(s, r.ChannelID, err)
|
|
return
|
|
}
|
|
|
|
s.MessageReactionRemove(r.ChannelID, r.MessageID, r.Emoji.Name, r.UserID)
|
|
|
|
var value int
|
|
switch r.Emoji.Name {
|
|
case emojiX:
|
|
value = db.ForceRejectionVote
|
|
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 vote #"+strconv.Itoa(voteID))
|
|
return
|
|
}
|
|
|
|
err = db.UpdateVote(voteID, r.UserID, value)
|
|
|
|
if err == db.ErrForceRejectionVoteReuse {
|
|
message.InitNewEmbed(config.VoteErrorTitle, config.ErrorForceRejectionReuse, config.ErrorColour).
|
|
SendPM(s, r.UserID)
|
|
return
|
|
}
|
|
if err == db.ErrVoteIsOver {
|
|
message.InitNewEmbed(config.VoteErrorTitle, config.ErrorVoteHasEnded, config.ErrorColour).
|
|
SendPM(s, r.UserID)
|
|
return
|
|
}
|
|
|
|
var voteName, voteType string
|
|
if err == nil {
|
|
voteName, err = db.GetVoteName(voteID)
|
|
}
|
|
if err == nil {
|
|
voteType, err = db.GetVoteType(voteID)
|
|
}
|
|
|
|
if err != nil {
|
|
message.AuditErrorPM(s, r.UserID, err)
|
|
return
|
|
}
|
|
|
|
message.InitNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour).
|
|
AddField(config.VoteSuccessfulVoteID, strconv.Itoa(voteID)).
|
|
AddField(config.VoteSuccessfulVoteName, voteTypes[voteType].FormatName(voteName)).
|
|
AddField(config.VoteSuccessfulDetectedVote, r.Emoji.Name).
|
|
SendPM(s, r.UserID)
|
|
message.AuditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))
|
|
|
|
checkForVoteResult(s, voteID)
|
|
}
|
|
|
|
var voteSuggestion = enforceDM(enforceArgumentCount(
|
|
config.VoteSuggestionUsage, 3,
|
|
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
|
|
if len(command[2]) > 500 {
|
|
message.InitNewEmbed(
|
|
config.VoteSuggestionErrorTitle,
|
|
config.ErrorVoteSuggestionTooLong,
|
|
config.ErrorColour,
|
|
).SendPM(s, m.Author.ID)
|
|
return
|
|
}
|
|
|
|
switch command[1] {
|
|
case "custom":
|
|
msg, err := message.InitNewEmbed(
|
|
config.VoteSuggestionUpcomingTitle,
|
|
config.VoteSuggestionUpcomingDescription,
|
|
config.VoteSuggestionUpcomingColour,
|
|
).Send(s, config.VoteChannel)
|
|
if err != nil {
|
|
message.AuditErrorPM(s, m.Author.ID, err)
|
|
return
|
|
}
|
|
id, err := db.CreateCustomVote(msg.ID, command[2])
|
|
if err != nil {
|
|
message.AuditErrorPM(s, m.Author.ID, err)
|
|
return
|
|
}
|
|
message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
|
|
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
|
|
createCustomEmbed(id, command[2]).Edit(s, config.VoteChannel, msg.ID)
|
|
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiOne)
|
|
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiTwo)
|
|
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiThree)
|
|
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiFour)
|
|
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiFive)
|
|
server.VoteNotification()
|
|
default:
|
|
message.InitNewEmbed(
|
|
config.VoteSuggestionErrorTitle,
|
|
fmt.Sprintf(config.ErrorVoteSuggestionUnknownType, command[1]),
|
|
config.ErrorColour,
|
|
).SendPM(s, m.Author.ID)
|
|
}
|
|
},
|
|
))
|