TerraOceanPlugin/GoBot/discord/modules/messageutil.go

73 lines
2.0 KiB
Go

package modules
import (
"fmt"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
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)
message.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 {
message.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 {
message.InitNewEmbed(config.PermissionErrorTitle, config.ErrorMustNotBeMember, config.ErrorColour).
Send(s, m.ChannelID)
}
return
}
}
if mustBeMember {
message.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 {
message.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 formatFloat(n float64, precision int) string {
return strconv.FormatFloat(n, 'f', precision, 64)
}