108 lines
2.9 KiB
Go
108 lines
2.9 KiB
Go
package modules
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
"TerraOceanBot/discord/config"
|
|
)
|
|
|
|
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
|
|
}
|
|
passOn(s, m, command)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
|
|
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).
|
|
Send(s, m.ChannelID)
|
|
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 += "..."
|
|
}
|
|
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)
|
|
}
|