package modules import ( "crypto/rand" "crypto/sha512" "encoding/hex" "fmt" "github.com/bwmarrin/discordgo" "TerraOceanBot/discord/backend" "TerraOceanBot/discord/config" "TerraOceanBot/discord/message" ) //adminID is my Discord user ID (chanbakjsd#7968). const adminID = "218983355746746369" func adminOnly(passOn CommandHandler) CommandHandler { return func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { if m.Author.ID != adminID { return } passOn(s, m, command) } } var sendAs = adminOnly(enforceArgumentCount(config.SendAsUsage, 3, //!sendas func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { msg, err := s.ChannelMessageSend(command[1], command[2]) if err != nil { message.AuditError(s, m.ChannelID, err) return } message.InitNewEmbed(config.SuccessTitle, config.SendAsSuccessfulDescription, config.SuccessColour). AddField(config.SendAsSuccessfulMessageID, msg.ID). Send(s, m.ChannelID) }, )) var editAs = adminOnly(enforceArgumentCount(config.EditAsUsage, 4, //!editas func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { _, err := s.ChannelMessageEdit(command[1], command[2], command[3]) if err != nil { message.AuditError(s, m.ChannelID, err) return } message.InitNewEmbed(config.SuccessTitle, config.EditAsSuccessfulDescription, config.SuccessColour). Send(s, m.ChannelID) }, )) var viewTrustLevel = adminOnly(func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { if len(command) > 1 && command[1] != "unobf" { user, err := backend.GetMemberFromUserFriendlyName(s, command[1]) if err != nil { message.InitNewEmbed( config.ErrorVTLTitle, config.ErrorVTLTargetNotFound, config.ErrorColour, ).Send(s, m.ChannelID) return } value, err := backend.GetTrust(s, user.User.ID) if err != nil { message.AuditError(s, m.ChannelID, err) return } total, err := backend.GetTotalTrust(s) if err != nil { message.AuditError(s, m.ChannelID, err) return } 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)). AddField(config.VTLSingleUserPercentage, formatFloat(value/total*100, 2)). Send(s, m.ChannelID) return } total, err := backend.GetTotalTrust(s) if err != nil { message.AuditError(s, m.ChannelID, err) return } members, err := backend.GetAllMembers(s) if err != nil { message.AuditError(s, m.ChannelID, err) return } salt := make([]byte, 16) rand.Read(salt) //Generate message hash := sha512.New() hash.Write(salt) 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 { message.AuditError(s, m.ChannelID, err) return } var username string if len(command) > 1 { username = v.User.Username + "#" + v.User.Discriminator } else { toSalt := []byte(v.User.Username + "#" + v.User.Discriminator) username = hex.EncodeToString(hash.Sum(toSalt))[:64] if username == "" { username = "" } } msg.AddInlineField( username, fmt.Sprintf(config.VTLGlobalFieldFormat, value, total, value/total*100), ) } _, err = msg.Send(s, m.ChannelID) if err != nil { message.AuditError(s, m.ChannelID, err) return } }) var batchGiveRole = adminOnly(enforceDM(enforceArgumentCount( config.BatchRoleUsage, 2, func(s *discordgo.Session, m *discordgo.MessageCreate, command []string) { members, err := backend.GetAllMembers(s) if err != nil { message.AuditError(s, m.ChannelID, err) return } for _, member := range members { err := s.GuildMemberRoleAdd( config.GuildID, member.User.ID, command[1], ) if err != nil { message.AuditError(s, m.ChannelID, err) return } } message.InitNewEmbed(config.SuccessTitle, config.BatchRoleSuccessMessage, config.SuccessColour). Send(s, m.ChannelID) }, )))