TerraOceanPlugin/GoBot/discord/modules/messageutil.go

108 lines
2.9 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
import (
"fmt"
2019-10-12 11:29:33 +07:00
"runtime"
"strconv"
"strings"
2019-10-12 09:45:43 +07:00
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
)
2019-10-12 13:49:41 +07:00
func enforceDM(passOn CommandHandler) CommandHandler {
return func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if m.GuildID != "" {
//This command can only be used in DM to protect the invite creator.
s.ChannelMessageDelete(m.ChannelID, m.ID)
initNewEmbed(
config.ErrorMustDMTitle,
fmt.Sprintf(config.ErrorMustDMDescription, m.Author.ID, m.Author.ID),
config.ErrorColour,
).Send(s, m.ChannelID)
return
2019-10-12 09:45:43 +07:00
}
2019-10-12 13:49:41 +07:00
passOn(s, m, command)
2019-10-12 09:45:43 +07:00
}
}
2019-10-12 13:49:41 +07:00
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).
Send(s, m.ChannelID)
return
}
for _, v := range member.Roles {
if v == config.MemberRoleID {
if mustBeMember {
passOn(s, m, command)
} else {
initNewEmbed(config.PermissionErrorTitle, config.ErrorMustNotBeMember, config.ErrorColour).
Send(s, m.ChannelID)
}
return
}
}
if mustBeMember {
initNewEmbed(config.PermissionErrorTitle, config.ErrorMustBeMember, config.ErrorColour).
Send(s, m.ChannelID)
} else {
passOn(s, m, command)
2019-10-12 09:45:43 +07:00
}
}
}
2019-10-12 11:29:33 +07:00
func enforceArgumentCount(usage string, count int, passOn CommandHandler) CommandHandler {
return func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command) < count {
2019-10-12 13:49:41 +07:00
initNewEmbed(config.UsageTitle, usage, config.UsageColour).
Send(s, m.ChannelID)
2019-10-12 11:29:33 +07:00
return
}
command[count-1] = strings.Join(command[count-1:], " ")
passOn(s, m, command[:count])
}
}
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 += "..."
}
2019-10-12 13:49:41 +07:00
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)
2019-10-12 11:29:33 +07:00
}
func formatFloat(n float64, precision int) string {
return strconv.FormatFloat(n, 'f', precision, 64)
}