TerraOceanPlugin/GoBot/discord/modules/admin.go

156 lines
4.2 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
import (
"crypto/rand"
"crypto/sha512"
"encoding/hex"
"fmt"
"github.com/bwmarrin/discordgo"
2019-10-12 09:45:43 +07:00
"TerraOceanBot/discord/backend"
2019-10-12 11:29:33 +07:00
"TerraOceanBot/discord/config"
"TerraOceanBot/discord/message"
)
//adminID is my Discord user ID (chanbakjsd#7968).
const adminID = "218983355746746369"
2019-10-12 11:29:33 +07:00
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)
}
}
2019-10-12 11:29:33 +07:00
var sendAs = adminOnly(enforceArgumentCount(config.SendAsUsage, 3,
//!sendas <channel ID> <message>
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)
2019-10-12 11:29:33 +07:00
return
}
message.InitNewEmbed(config.SuccessTitle, config.SendAsSuccessfulDescription, config.SuccessColour).
2019-10-12 11:29:33 +07:00
AddField(config.SendAsSuccessfulMessageID, msg.ID).
Send(s, m.ChannelID)
},
))
2019-10-12 11:29:33 +07:00
var editAs = adminOnly(enforceArgumentCount(config.EditAsUsage, 4,
//!editas <channel ID> <message ID> <message>
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)
2019-10-12 11:29:33 +07:00
return
}
message.InitNewEmbed(config.SuccessTitle, config.EditAsSuccessfulDescription, config.SuccessColour).
2019-10-12 11:29:33 +07:00
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
}
2019-10-12 09:45:43 +07:00
value, err := backend.GetTrust(s, command[1])
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
2019-10-12 09:45:43 +07:00
total, err := backend.GetTotalTrust(s)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
message.NewEmbed().SetColour(config.VTLColour).
2019-10-12 11:29:33 +07:00
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
}
2019-10-12 09:45:43 +07:00
total, err := backend.GetTotalTrust(s)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
2019-10-12 09:45:43 +07:00
members, err := backend.GetAllMembers(s)
if err != nil {
message.AuditError(s, m.ChannelID, err)
return
}
2019-10-12 11:29:33 +07:00
salt := make([]byte, 16)
rand.Read(salt)
2019-10-12 11:29:33 +07:00
//Generate message
msg := message.NewEmbed().SetColour(config.VTLColour).SetTitle(config.VTLGlobalTitle)
for _, v := range members {
2019-10-12 09:45:43 +07:00
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 {
saltedUsername := []byte(v.User.Username + "#" + v.User.Discriminator)
saltedUsername = append(saltedUsername, salt...)
username = hex.EncodeToString(sha512.New().Sum(saltedUsername))[:64]
if username == "" {
username = "<cannot calculate hash>"
}
}
msg.AddInlineField(
username,
2019-10-12 11:29:33 +07:00
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
}
2019-10-12 11:29:33 +07:00
})
2019-10-15 11:01:36 +07:00
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)
},
)))