go: Move embed creation and audit functions into its own package

master
Luther Wen Xu 2019-10-13 20:38:00 +07:00
parent d58e32f2b4
commit 6211334d91
Signed by untrusted user: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
14 changed files with 178 additions and 155 deletions

@ -1,12 +1,11 @@
package backend
import (
"fmt"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"TerraOceanBot/server"
)
@ -27,8 +26,7 @@ func UpdateVoiceChannelState(s *discordgo.Session) {
continue
}
if err != nil {
//TODO Use auditError() once it's moved to its own package.
fmt.Println(err)
message.AuditError(s, "", err)
continue
}
server.ConnectUser(username)
@ -36,14 +34,13 @@ func UpdateVoiceChannelState(s *discordgo.Session) {
}
}
func VoiceStateUpdate(vs *discordgo.VoiceState) {
func VoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceState) {
username, err := db.GetMinecraftUsername(vs.UserID)
if err == db.ErrNotFound {
return
}
if err != nil {
//TODO Use auditError() once it's moved to its own package.
fmt.Println(err)
message.AuditError(s, "", err)
return
}
if vs.GuildID != config.GuildID {

@ -32,5 +32,5 @@ func ProcessVote(s *discordgo.Session, m *discordgo.MessageCreate) {
}
func ProcessVoiceStateUpdate(s *discordgo.Session, vs *discordgo.VoiceStateUpdate) {
backend.VoiceStateUpdate(vs.VoiceState)
backend.VoiceStateUpdate(s, vs.VoiceState)
}

@ -0,0 +1,42 @@
package message
import (
"runtime/debug"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
)
func AuditInfo(s *discordgo.Session, message string) {
InitNewEmbed(config.AuditInfoTitle, message, config.AuditInfoColour).
Send(s, config.AuditChannel)
}
func AuditError(s *discordgo.Session, sourceChannel string, err error) {
if sourceChannel != "" {
InitNewErrorEmbed(config.InternalErrorDescription).
Send(s, sourceChannel)
}
sendError(s, config.AuditChannel, err)
}
func AuditErrorPM(s *discordgo.Session, sourceUser string, err error) {
channel, channelErr := s.UserChannelCreate(sourceUser)
if channelErr != nil {
AuditError(s, "", channelErr)
AuditError(s, "", err)
return
}
AuditError(s, channel.ID, err)
}
func sendError(s *discordgo.Session, channelID string, err error) {
stack := string(debug.Stack())
if len(stack) > 1000 {
stack = stack[:1000] + "..."
}
InitNewErrorEmbed(err.Error()).
AddField(config.ErrorStack, stack).
Send(s, channelID)
}

@ -1,4 +1,4 @@
package modules
package message
import (
"github.com/bwmarrin/discordgo"
@ -6,42 +6,42 @@ import (
"TerraOceanBot/discord/config"
)
type embed struct {
type Embed struct {
*discordgo.MessageEmbed
}
func newEmbed() *embed {
return &embed{&discordgo.MessageEmbed{}}
func NewEmbed() *Embed {
return &Embed{&discordgo.MessageEmbed{}}
}
func initNewEmbed(title, description string, colour int) *embed {
return newEmbed().SetTitle(title).SetDescription(description).SetColour(colour)
func InitNewEmbed(title, description string, colour int) *Embed {
return NewEmbed().SetTitle(title).SetDescription(description).SetColour(colour)
}
func initNewErrorEmbed(description string) *embed {
e := newEmbed()
func InitNewErrorEmbed(description string) *Embed {
e := NewEmbed()
e.Color = 0xE00000
e.Title = "发生错误An error has occurred!"
e.Description = description
return e
}
func (e *embed) SetTitle(title string) *embed {
func (e *Embed) SetTitle(title string) *Embed {
e.Title = title
return e
}
func (e *embed) SetColour(colour int) *embed {
func (e *Embed) SetColour(colour int) *Embed {
e.Color = colour
return e
}
func (e *embed) SetDescription(description string) *embed {
func (e *Embed) SetDescription(description string) *Embed {
e.Description = description
return e
}
func (e *embed) AddField(name, value string) *embed {
func (e *Embed) AddField(name, value string) *Embed {
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: name,
Value: value,
@ -49,7 +49,7 @@ func (e *embed) AddField(name, value string) *embed {
return e
}
func (e *embed) AddInlineField(name, value string) *embed {
func (e *Embed) AddInlineField(name, value string) *Embed {
e.Fields = append(e.Fields, &discordgo.MessageEmbedField{
Name: name,
Value: value,
@ -58,30 +58,30 @@ func (e *embed) AddInlineField(name, value string) *embed {
return e
}
func (e *embed) UpdateVoteStatus(isPositive bool) *embed {
func (e *Embed) UpdateVoteStatus(isPositive bool) *Embed {
if isPositive {
return e.SetColour(config.VotePassColour).SetTitle(config.VotePassTitle)
}
return e.SetColour(config.VoteRejectColour).SetTitle(config.VoteRejectTitle)
}
func (e *embed) Build() *discordgo.MessageEmbed {
func (e *Embed) Build() *discordgo.MessageEmbed {
return e.MessageEmbed
}
func (e *embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message, error) {
func (e *Embed) Send(s *discordgo.Session, channelID string) (*discordgo.Message, error) {
return s.ChannelMessageSendEmbed(channelID, e.Build())
}
func (e *embed) SendPM(s *discordgo.Session, userID string) (*discordgo.Message, error) {
func (e *Embed) SendPM(s *discordgo.Session, userID string) (*discordgo.Message, error) {
channel, err := s.UserChannelCreate(userID)
if err != nil {
auditError(s, "", err)
AuditError(s, "", err)
return nil, err
}
return e.Send(s, channel.ID)
}
func (e *embed) Edit(s *discordgo.Session, channelID, messageID string) {
func (e *Embed) Edit(s *discordgo.Session, channelID, messageID string) {
s.ChannelMessageEditEmbed(channelID, messageID, e.Build())
}

@ -7,6 +7,7 @@ import (
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
//adminID is my Discord user ID (chanbakjsd#7968).
@ -26,10 +27,10 @@ var sendAs = adminOnly(enforceArgumentCount(config.SendAsUsage, 3,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
msg, err := s.ChannelMessageSend(command[1], command[2])
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
initNewEmbed(config.SuccessTitle, config.SendAsSuccessfulDescription, config.SuccessColour).
message.InitNewEmbed(config.SuccessTitle, config.SendAsSuccessfulDescription, config.SuccessColour).
AddField(config.SendAsSuccessfulMessageID, msg.ID).
Send(s, m.ChannelID)
},
@ -40,10 +41,10 @@ var editAs = adminOnly(enforceArgumentCount(config.EditAsUsage, 4,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
_, err := s.ChannelMessageEdit(command[1], command[2], command[3])
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
initNewEmbed(config.SuccessTitle, config.EditAsSuccessfulDescription, config.SuccessColour).
message.InitNewEmbed(config.SuccessTitle, config.EditAsSuccessfulDescription, config.SuccessColour).
Send(s, m.ChannelID)
},
))
@ -52,15 +53,15 @@ var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCr
if len(command) > 1 {
value, err := backend.GetTrust(s, command[1])
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
total, err := backend.GetTotalTrust(s)
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
newEmbed().SetColour(config.VTLColour).
message.NewEmbed().SetColour(config.VTLColour).
SetTitle(fmt.Sprintf(config.VTLSingleUserTitle, command[1], command[1])).
AddField(config.VTLSingleUserValue, formatFloat(value, 1)).
AddField(config.VTLSingleUserServerTotal, formatFloat(total, 1)).
@ -71,30 +72,30 @@ var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCr
total, err := backend.GetTotalTrust(s)
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
members, err := backend.GetAllMembers(s)
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
//Generate message
message := newEmbed().SetColour(config.VTLColour).SetTitle(config.VTLGlobalTitle)
msg := message.NewEmbed().SetColour(config.VTLColour).SetTitle(config.VTLGlobalTitle)
for _, v := range members {
value, err := backend.GetTrust(s, v.User.ID)
if err == backend.ErrNotMember {
continue
}
if err != nil {
sendError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
message.AddInlineField(
msg.AddInlineField(
v.User.Username+"#"+v.User.Discriminator,
fmt.Sprintf(config.VTLGlobalFieldFormat, value, total, value/total*100),
)
}
message.Send(s, m.ChannelID)
msg.Send(s, m.ChannelID)
})

@ -8,6 +8,7 @@ import (
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"TerraOceanBot/server"
)
@ -22,7 +23,7 @@ var createInvite = enforceDM(memberFilter(true, enforceArgumentCount(
config.InviteUsage, 2,
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if strings.ContainsRune(command[1], ' ') {
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteCodeContainsSpace, config.ErrorColour).
Send(s, m.ChannelID)
return
}
@ -32,20 +33,20 @@ var createInvite = enforceDM(memberFilter(true, enforceArgumentCount(
case db.ErrAlreadyExists:
owner, _ := db.GetInviteOwner(command[1])
if owner == m.Author.ID {
initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
message.InitNewEmbed(config.InviteSuccessTitle, config.InviteSuccessAlreadyOwn, config.SuccessColour).
Send(s, m.ChannelID)
return
}
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteOwnedByOthers, config.ErrorColour).
Send(s, m.ChannelID)
case db.ErrInviteUsed:
initNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
message.InitNewEmbed(config.ErrorInviteTitle, config.ErrorInviteExistAndUsed, config.ErrorColour).
Send(s, m.ChannelID)
case nil:
initNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
message.InitNewEmbed(config.InviteSuccessTitle, config.InviteSuccessNew, config.SuccessColour).
Send(s, m.ChannelID)
default:
auditErrorPM(s, m.Author.ID, err)
message.AuditErrorPM(s, m.Author.ID, err)
}
},
)))
@ -55,24 +56,24 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
inviter, err := db.GetInviteOwner(command[1])
if err == db.ErrInviteUsed {
initNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeReuse, config.ErrorColour).
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeReuse, config.ErrorColour).
Send(s, m.ChannelID)
return
}
if err == db.ErrNotFound {
initNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotExist, config.ErrorColour).
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotExist, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
if err != nil {
auditError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
db.UseInvite(command[1])
member, err := s.GuildMember(config.GuildID, inviter)
if err != nil {
auditErrorPM(s, m.ChannelID, err)
message.AuditErrorPM(s, m.ChannelID, err)
return
}
@ -83,18 +84,18 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
}
}
if !isMember {
initNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotOwnedByMember, config.ErrorColour).
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeNotOwnedByMember, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
channel, err := s.UserChannelCreate(inviter)
if err != nil {
initNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour).
message.InitNewEmbed(config.ErrorValidateTitle, config.ErrorValidationCodeCreatorDisabledPM, config.ErrorColour).
SendPM(s, m.ChannelID)
return
}
msg, err := initNewEmbed(
msg, err := message.InitNewEmbed(
config.ValidateConfirmationTitle,
config.ValidateConfirmationDescription,
config.ValidateConfirmationColour,
@ -102,7 +103,7 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
AddField(config.ValidateConfirmationInviteCode, command[1]).
Send(s, channel.ID)
if err != nil {
auditErrorPM(s, m.ChannelID, err)
message.AuditErrorPM(s, m.ChannelID, err)
return
}
pendingInviteConfirmation[msg.ID] = invite{
@ -111,7 +112,7 @@ var checkUseInvite = enforceDM(memberFilter(false, enforceArgumentCount(
}
s.MessageReactionAdd(channel.ID, msg.ID, emojiCheck)
s.MessageReactionAdd(channel.ID, msg.ID, emojiX)
initNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
message.InitNewEmbed(config.SuccessTitle, config.ValidateSuccess, config.SuccessColour).
SendPM(s, m.ChannelID)
},
)))
@ -129,12 +130,12 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
switch r.Emoji.Name {
case emojiX:
initNewEmbed(
message.InitNewEmbed(
config.ValidateConfirmationRejectCreatorTitle,
config.ValidateConfirmationRejectCreatorDescription,
config.ErrorColour,
).SendPM(s, r.UserID)
initNewEmbed(
message.InitNewEmbed(
config.ValidateConfirmationRejectRejecteeTitle,
config.ValidateConfirmationRejectRejecteeDescription,
config.ErrorColour,
@ -145,12 +146,12 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
return
}
initNewEmbed(
message.InitNewEmbed(
config.ValidateConfirmationAcceptCreatorTitle,
config.ValidateConfirmationAcceptCreatorDescription,
config.ErrorColour,
).SendPM(s, r.UserID)
initNewEmbed(
message.InitNewEmbed(
config.ValidateConfirmationAcceptAccepteeTitle,
config.ValidateConfirmationAcceptAccepteeDescription,
config.ErrorColour,
@ -158,15 +159,15 @@ func CheckForInvite(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
if err != nil {
auditErrorPM(s, r.UserID, err)
message.AuditErrorPM(s, r.UserID, err)
return
}
id, err := db.CreateInviteVote(msg.ID, invite.User, invite.Reason)
if err != nil {
auditErrorPM(s, r.UserID, err)
message.AuditErrorPM(s, r.UserID, err)
return
}
auditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, r.UserID))
message.AuditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, r.UserID))
s.ChannelMessageEdit(config.VoteChannel, msg.ID, "")
createInviteEmbed(id, invite.User+":"+invite.Reason).Edit(s, config.VoteChannel, msg.ID)
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiX)

@ -2,13 +2,13 @@ package modules
import (
"fmt"
"runtime"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
func enforceDM(passOn CommandHandler) CommandHandler {
@ -16,7 +16,7 @@ func enforceDM(passOn CommandHandler) CommandHandler {
if m.GuildID != "" {
//This command can only be used in DM to protect the invite creator.
s.ChannelMessageDelete(m.ChannelID, m.ID)
initNewEmbed(
message.InitNewEmbed(
config.ErrorMustDMTitle,
fmt.Sprintf(config.ErrorMustDMDescription, m.Author.ID, m.Author.ID),
config.ErrorColour,
@ -31,7 +31,7 @@ func memberFilter(mustBeMember bool, passOn CommandHandler) CommandHandler {
return func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
member, err := s.GuildMember(config.GuildID, m.Author.ID)
if err != nil {
initNewEmbed(config.PermissionErrorTitle, config.ErrorCannotFindMember, config.ErrorColour).
message.InitNewEmbed(config.PermissionErrorTitle, config.ErrorCannotFindMember, config.ErrorColour).
Send(s, m.ChannelID)
return
}
@ -40,14 +40,14 @@ func memberFilter(mustBeMember bool, passOn CommandHandler) CommandHandler {
if mustBeMember {
passOn(s, m, command)
} else {
initNewEmbed(config.PermissionErrorTitle, config.ErrorMustNotBeMember, config.ErrorColour).
message.InitNewEmbed(config.PermissionErrorTitle, config.ErrorMustNotBeMember, config.ErrorColour).
Send(s, m.ChannelID)
}
return
}
}
if mustBeMember {
initNewEmbed(config.PermissionErrorTitle, config.ErrorMustBeMember, config.ErrorColour).
message.InitNewEmbed(config.PermissionErrorTitle, config.ErrorMustBeMember, config.ErrorColour).
Send(s, m.ChannelID)
} else {
passOn(s, m, command)
@ -58,7 +58,7 @@ func memberFilter(mustBeMember bool, passOn CommandHandler) CommandHandler {
func enforceArgumentCount(usage string, count int, passOn CommandHandler) CommandHandler {
return func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command) < count {
initNewEmbed(config.UsageTitle, usage, config.UsageColour).
message.InitNewEmbed(config.UsageTitle, usage, config.UsageColour).
Send(s, m.ChannelID)
return
}
@ -67,41 +67,6 @@ func enforceArgumentCount(usage string, count int, passOn CommandHandler) Comman
}
}
func sendError(s *discordgo.Session, channelID string, err error) {
stackCache := make([]byte, 512)
count := runtime.Stack(stackCache, false)
stack := string(stackCache[:count])
if count == 512 {
stack += "..."
}
initNewErrorEmbed(err.Error()).
AddField(config.ErrorStack, stack).
Send(s, channelID)
}
func auditInfo(s *discordgo.Session, message string) {
initNewEmbed(config.AuditInfoTitle, message, config.AuditInfoColour).
Send(s, config.AuditChannel)
}
func auditError(s *discordgo.Session, sourceChannel string, err error) {
if sourceChannel != "" {
initNewErrorEmbed(config.InternalErrorDescription).
Send(s, sourceChannel)
}
sendError(s, config.AuditChannel, err)
}
func auditErrorPM(s *discordgo.Session, sourceUser string, err error) {
channel, channelErr := s.UserChannelCreate(sourceUser)
if channelErr != nil {
auditError(s, "", channelErr)
auditError(s, "", err)
return
}
auditError(s, channel.ID, err)
}
func formatFloat(n float64, precision int) string {
return strconv.FormatFloat(n, 'f', precision, 64)
}

@ -6,6 +6,7 @@ import (
"TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"TerraOceanBot/server"
)
@ -20,15 +21,15 @@ var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
processUsernameUpdate(s, m.Author.ID, m.ChannelID, command[1])
return
}
auditError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
if oldUsername == command[1] {
initNewEmbed(config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameDuplicate, config.ErrorColour).
message.InitNewEmbed(config.SetMCUsernameErrorTitle, config.ErrorSetMCUsernameDuplicate, config.ErrorColour).
Send(s, m.ChannelID)
return
}
msg, err := initNewEmbed(
msg, err := message.InitNewEmbed(
config.SetMCUsernameConfirmTitle,
config.SetMCUsernameConfirmDescription,
config.SetMCUsernameConfirmColour,
@ -36,7 +37,7 @@ var updateMinecraftUsername = memberFilter(true, enforceArgumentCount(
AddField(config.SetMCUsernameConfirmNewUsername, command[1]).
SendPM(s, m.Author.ID)
if err != nil {
auditError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
}
minecraftConfirmReact[msg.ID] = command[1]
s.MessageReactionAdd(msg.ChannelID, msg.ID, emojiCheck)
@ -59,8 +60,11 @@ func ConfirmMinecraftUsername(s *discordgo.Session, r *discordgo.MessageReaction
switch r.Emoji.Name {
case emojiCheck:
case emojiX:
initNewEmbed(config.SetMCUsernameConfirmDeniedTitle, config.SetMCUsernameConfirmDeniedDescription, config.SuccessColour).
Send(s, r.ChannelID)
message.InitNewEmbed(
config.SetMCUsernameConfirmDeniedTitle,
config.SetMCUsernameConfirmDeniedDescription,
config.SuccessColour,
).Send(s, r.ChannelID)
return
}
@ -71,7 +75,7 @@ func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUserna
oldUsername, oldErr := db.GetMinecraftUsername(discordID)
err := db.SetMinecraftUsername(discordID, newUsername)
if err == db.ErrAlreadyExists {
initNewEmbed(
message.InitNewEmbed(
config.SetMCUsernameErrorTitle,
config.ErrorSetMCUsernameAlreadyOwned,
config.ErrorColour,
@ -84,7 +88,7 @@ func processUsernameUpdate(s *discordgo.Session, discordID, channelID, newUserna
server.WhitelistRemove(oldUsername)
}
initNewEmbed(
message.InitNewEmbed(
config.SetMCUsernameSuccessTitle,
config.SetMCUsernameSuccessDescription,
config.SuccessColour,

@ -7,6 +7,7 @@ import (
"TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/message"
)
var trustMessage = make(map[string]string)
@ -24,32 +25,35 @@ var changeTrust = enforceDM(memberFilter(true,
return
}
if err != nil {
auditError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
message, err := s.ChannelMessageSendEmbed(
msg, err := s.ChannelMessageSendEmbed(
m.ChannelID,
createTrustEmbed(member.User.Username+"#"+member.User.Discriminator, member.Nick),
)
if err != nil {
auditError(s, m.ChannelID, err)
message.AuditError(s, m.ChannelID, err)
return
}
trustMessage[message.ID] = member.User.ID
trustMessage[msg.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)
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 createTrustEmbed(username, nick string) *discordgo.MessageEmbed {
embed := newEmbed().SetColour(0x00FFFF).SetTitle("更新玩家评分Update Player Score").AddField("ID", username)
embed := message.NewEmbed().
SetColour(0x00FFFF).
SetTitle("更新玩家评分Update Player Score").
AddField("ID", username)
if nick != "" {
embed.AddField("昵称Nickname", nick)
}
@ -84,17 +88,18 @@ func CheckForTrustUpdate(s *discordgo.Session, r *discordgo.MessageReactionAdd)
case emojiCheck:
value = db.NuclearOptionVote
default:
auditInfo(s, "Reaction "+r.Emoji.Name+" was added to trust message ID "+r.MessageID)
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 {
//TODO Move to embed.
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 {
auditError(s, r.ChannelID, err)
message.AuditError(s, r.ChannelID, err)
return
}

@ -1,10 +1,14 @@
package modules
import "github.com/bwmarrin/discordgo"
import (
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/message"
)
type voteType struct {
//TODO Add FormatName(name string) to clean up vote name
EmbedBuilder func(id int, name string) *embed
EmbedBuilder func(id int, name string) *message.Embed
ResultHandler func(s *discordgo.Session, id int, name string, isPositive bool)
}

@ -6,10 +6,11 @@ import (
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
func createCustomEmbed(id int, name string) *embed {
return initNewEmbed(config.CustomVoteTitle, "", config.CustomVoteColour).
func createCustomEmbed(id int, name string) *message.Embed {
return message.InitNewEmbed(config.CustomVoteTitle, "", config.CustomVoteColour).
AddField(config.CustomVoteVoteID, strconv.Itoa(id)).
AddField(config.CustomVoteContent, name)
}

@ -10,11 +10,12 @@ import (
"TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
func createInviteEmbed(id int, name string) *embed {
func createInviteEmbed(id int, name string) *message.Embed {
list := strings.SplitN(name, ":", 2)
return initNewEmbed(config.EntryVoteTitle, "", config.EntryVoteColour).
return message.InitNewEmbed(config.EntryVoteTitle, "", config.EntryVoteColour).
AddField(config.EntryVoteVoteID, strconv.Itoa(id)).
AddField(config.EntryVoteApplicant, "<@"+list[0]+">").
AddField(config.EntryVoteReason, list[1])
@ -23,28 +24,28 @@ func createInviteEmbed(id int, name string) *embed {
func handleInviteResult(s *discordgo.Session, id int, name string, isPositive bool) {
list := strings.SplitN(name, ":", 2)
if !isPositive {
initNewEmbed(config.EntryVoteRejectNotificationTitle, config.EntryVoteRejectNotificationContent, config.ErrorColour).
message.InitNewEmbed(config.EntryVoteRejectNotificationTitle, config.EntryVoteRejectNotificationContent, config.ErrorColour).
SendPM(s, list[0])
return
}
member, err := backend.GetMemberFromUserFriendlyName(s, list[0])
if err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
return
}
err = s.GuildMemberRoleAdd(config.GuildID, member.User.ID, config.MemberRoleID)
if err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
return
}
initNewEmbed(
message.InitNewEmbed(
config.EntryVoteSuccessWelcomeTitle,
fmt.Sprintf(config.EntryVoteSuccessWelcomeDescription, member.User.ID, member.User.ID),
config.SuccessColour,
).Send(s, config.AnnounceInviteChannel)
choices, err := db.GetAllVoteChoices(id)
if err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
return
}
for _, choice := range choices {

@ -9,6 +9,7 @@ import (
"TerraOceanBot/db"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
"TerraOceanBot/server"
)
@ -31,7 +32,7 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
return
}
if err != nil {
auditError(s, r.ChannelID, err)
message.AuditError(s, r.ChannelID, err)
return
}
@ -54,19 +55,19 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
case emojiCheck:
value = db.NuclearOptionVote
default:
auditInfo(s, "Reaction "+r.Emoji.Name+" was added to vote #"+strconv.Itoa(voteID))
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 {
initNewEmbed(config.VoteErrorTitle, config.ErrorForceRejectionReuse, config.ErrorColour).
message.InitNewEmbed(config.VoteErrorTitle, config.ErrorForceRejectionReuse, config.ErrorColour).
SendPM(s, r.UserID)
return
}
if err == db.ErrVoteIsOver {
initNewEmbed(config.VoteErrorTitle, config.ErrorVoteHasEnded, config.ErrorColour).
message.InitNewEmbed(config.VoteErrorTitle, config.ErrorVoteHasEnded, config.ErrorColour).
SendPM(s, r.UserID)
return
}
@ -77,16 +78,16 @@ func CheckForVote(s *discordgo.Session, r *discordgo.MessageReactionAdd) {
}
if err != nil {
auditErrorPM(s, r.UserID, err)
message.AuditErrorPM(s, r.UserID, err)
return
}
initNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour).
message.InitNewEmbed(config.VoteSuccessfulTitle, "", config.SuccessColour).
AddField(config.VoteSuccessfulVoteID, strconv.Itoa(voteID)).
AddField(config.VoteSuccessfulVoteName, voteName).
AddField(config.VoteSuccessfulDetectedVote, r.Emoji.Name).
SendPM(s, r.UserID)
auditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))
message.AuditInfo(s, fmt.Sprintf("<@%s> has chosen %s for vote ID %d.", r.UserID, r.Emoji.Name, voteID))
checkForVoteResult(s, voteID)
}
@ -97,7 +98,7 @@ func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
args := strings.SplitN(m.Content, " ", 2)
if len(args) == 1 {
initNewEmbed(
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionNotEnoughInfo+config.VoteSuggestionNote,
config.ErrorColour,
@ -105,7 +106,7 @@ func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
return
}
if len(args[1]) > 500 {
initNewEmbed(
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
config.ErrorVoteSuggestionTooLong+config.VoteSuggestionNote,
config.ErrorColour,
@ -117,15 +118,15 @@ func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
case "custom":
msg, err := s.ChannelMessageSend(config.VoteChannel, "正在准备新的一个投票…… Preparing for the next vote...")
if err != nil {
auditErrorPM(s, m.Author.ID, err)
message.AuditErrorPM(s, m.Author.ID, err)
return
}
id, err := db.CreateCustomVote(msg.ID, args[1])
if err != nil {
auditErrorPM(s, m.Author.ID, err)
message.AuditErrorPM(s, m.Author.ID, err)
return
}
auditInfo(s, fmt.Sprintf("Vote ID %d has been created by <@%s>.", id, m.Author.ID))
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, args[1]).Edit(s, config.VoteChannel, msg.ID)
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiOne)
@ -135,7 +136,7 @@ func VoteSuggestion(s *discordgo.Session, m *discordgo.MessageCreate) {
s.MessageReactionAdd(config.VoteChannel, msg.ID, emojiFive)
server.VoteNotification()
default:
initNewEmbed(
message.InitNewEmbed(
config.VoteSuggestionErrorTitle,
fmt.Sprintf(config.ErrorVoteSuggestionUnknownType, args[1])+config.VoteSuggestionNote,
config.ErrorColour,

@ -10,6 +10,7 @@ import (
"TerraOceanBot/db"
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
type confirmedResult struct {
@ -24,9 +25,9 @@ func ListenToVoteFinishes(s *discordgo.Session) {
for {
voteMutex.Lock()
for _, v := range toAnnounceResultList {
auditInfo(s, fmt.Sprintf("Announcing the result of vote %d.", v.VoteID))
message.AuditInfo(s, fmt.Sprintf("Announcing the result of vote %d.", v.VoteID))
if err := db.FinishVote(v.VoteID); err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
continue
}
voteType, _ := db.GetVoteType(v.VoteID)
@ -44,7 +45,7 @@ func ListenToVoteFinishes(s *discordgo.Session) {
func checkForVoteResult(s *discordgo.Session, id int) {
votes, err := db.GetAllVoteChoices(id)
if err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
return
}
var currentScore, totalTrust float64
@ -55,7 +56,7 @@ func checkForVoteResult(s *discordgo.Session, id int) {
}
trust, err := backend.GetTrust(s, vote.UserID)
if err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
return
}
currentScore += float64(vote.Value) * trust
@ -64,7 +65,7 @@ func checkForVoteResult(s *discordgo.Session, id int) {
totalGlobalTrust, err := backend.GetTotalTrust(s)
if err != nil {
auditError(s, "", err)
message.AuditError(s, "", err)
return
}
remainingTrust := totalGlobalTrust - totalTrust
@ -81,7 +82,7 @@ func checkForVoteResult(s *discordgo.Session, id int) {
return
}
}
auditInfo(s, fmt.Sprintf("Rejection of vote ID %d has been confirmed.", id))
message.AuditInfo(s, fmt.Sprintf("Rejection of vote ID %d has been confirmed.", id))
toAnnounceResultList = append(toAnnounceResultList, confirmedResult{
VoteID: id,
IsPositive: false,
@ -98,7 +99,7 @@ func checkForVoteResult(s *discordgo.Session, id int) {
return
}
}
auditInfo(s, fmt.Sprintf("Acceptance of vote ID %d has been confirmed.", id))
message.AuditInfo(s, fmt.Sprintf("Acceptance of vote ID %d has been confirmed.", id))
toAnnounceResultList = append(toAnnounceResultList, confirmedResult{
VoteID: id,
IsPositive: true,