156 lines
4.2 KiB
Go
156 lines
4.2 KiB
Go
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 <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)
|
|
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 <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)
|
|
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, command[1])
|
|
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
|
|
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 {
|
|
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,
|
|
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)
|
|
},
|
|
)))
|