TerraOceanPlugin/GoBot/discord/modules/messageutil.go

110 lines
2.9 KiB
Go

package modules
import (
"fmt"
"runtime"
"strconv"
"strings"
"github.com/bwmarrin/discordgo"
"TerraOceanBot/discord/config"
)
func enforceDM(s *discordgo.Session, m *discordgo.MessageCreate) bool {
if m.GuildID != "" {
//This command can only be used in DM to protect the invite creator.
s.ChannelMessageDelete(m.ChannelID, m.ID)
s.ChannelMessageSend(
m.ChannelID,
m.Author.Mention()+",这个指令只能在私信中使用。\n"+m.Author.Mention()+", this command can only be used in DM.",
)
return false
}
return true
}
func membersOnly(s *discordgo.Session, m *discordgo.MessageCreate) bool {
member, err := s.GuildMember(config.GuildID, m.Author.ID)
if err != nil {
s.ChannelMessageSend(
m.ChannelID,
m.Author.Mention()+",请在成为会员后才使用这个指令。\n"+m.Author.Mention()+", please use this command after you become a member.",
)
return false
}
for _, v := range member.Roles {
if v == config.MemberRoleID {
return true
}
}
s.ChannelMessageSend(
m.ChannelID,
m.Author.Mention()+",请在成为会员后才使用这个指令。\n"+m.Author.Mention()+", please use this command after you become a member.",
)
return false
}
func nonMembersOnly(s *discordgo.Session, m *discordgo.MessageCreate) bool {
member, err := s.GuildMember(config.GuildID, m.Author.ID)
if err != nil {
return false
}
for _, v := range member.Roles {
if v == config.MemberRoleID {
s.ChannelMessageSend(
m.ChannelID,
m.Author.Mention()+",该指令只能被非会员使用。\n"+m.Author.Mention()+", this command can only be used by non-member.",
)
return false
}
}
return true
}
func sendPrivateMessage(s *discordgo.Session, recipient, message string) error {
channel, err := s.UserChannelCreate(recipient)
if err != nil {
return err
}
_, err = s.ChannelMessageSend(channel.ID, message)
return err
}
func auditLog(s *discordgo.Session, message string) {
_, err := s.ChannelMessageSend(config.AuditChannel, message)
if err != nil {
fmt.Println("==Audit Log==")
fmt.Println(message)
fmt.Println(err)
}
}
func enforceArgumentCount(usage string, count int, passOn CommandHandler) CommandHandler {
return func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) {
if len(command) < count {
s.ChannelMessageSendEmbed(
m.ChannelID,
newEmbed().SetTitle(config.UsageTitle).SetDescription(usage).SetColour(config.UsageColour).Build(),
)
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 formatFloat(n float64, precision int) string {
return strconv.FormatFloat(n, 'f', precision, 64)
}