forked from Minecraft/canopeas
135 lines
2.9 KiB
Go
135 lines
2.9 KiB
Go
package discord
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"go.etztech.xyz/sedbot/config"
|
|
"go.etztech.xyz/sedbot/database"
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
"go.jolheiser.com/beaver"
|
|
)
|
|
|
|
// Register commands to this map
|
|
var commands = make(map[string]command)
|
|
|
|
type commandInit struct {
|
|
session *discordgo.Session
|
|
message *discordgo.Message
|
|
config *config.Config
|
|
database *database.Database
|
|
}
|
|
|
|
type command struct {
|
|
validate func(cmd commandInit) bool
|
|
run func(cmd commandInit) error
|
|
help string
|
|
}
|
|
|
|
func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error) {
|
|
bot, err := discordgo.New("Bot " + cfg.Token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
Links(cfg)
|
|
|
|
bot.AddHandler(commandHandler(cfg, db))
|
|
bot.AddHandler(messageHandler(cfg, db))
|
|
|
|
beaver.Info("https://discord.com/api/oauth2/authorize?client_id=718905104643784825&permissions=0&redirect_uri=https%3A%2F%2Fbirbmc.com&scope=bot")
|
|
|
|
return bot, nil
|
|
}
|
|
|
|
func sendTyping(s *discordgo.Session, channelID string) {
|
|
if err := s.ChannelTyping(channelID); err != nil {
|
|
beaver.Errorf("could not send typing status: %v", err)
|
|
}
|
|
}
|
|
|
|
func sendMessage(s *discordgo.Session, channelID, content string) *discordgo.Message {
|
|
msg, err := s.ChannelMessageSend(channelID, content)
|
|
if err != nil {
|
|
beaver.Errorf("could not send message: %v", err)
|
|
return nil
|
|
}
|
|
return msg
|
|
}
|
|
|
|
func isStaff(authorRoleIDs, staffRoleIDs []string) bool {
|
|
for _, aRole := range authorRoleIDs {
|
|
for _, sRole := range staffRoleIDs {
|
|
if aRole == sRole {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
func commandHandler(cfg *config.Config, db *database.Database) func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
// Ignore bots
|
|
if m.Author.Bot {
|
|
return
|
|
}
|
|
|
|
// Check prefix
|
|
if !strings.HasPrefix(m.Content, cfg.Prefix) {
|
|
return
|
|
}
|
|
|
|
content := m.Content[1:]
|
|
args := strings.Fields(content)
|
|
if len(args) == 0 {
|
|
return
|
|
}
|
|
|
|
cmdArg := strings.ToLower(args[0])
|
|
isHelp := strings.EqualFold(cmdArg, "help")
|
|
|
|
cmd, ok := commands[cmdArg]
|
|
if !ok && !isHelp {
|
|
return
|
|
}
|
|
|
|
if isHelp {
|
|
sendMessage(s, m.ChannelID, cmd.help)
|
|
return
|
|
}
|
|
|
|
cmdInit := commandInit{
|
|
session: s,
|
|
message: m.Message,
|
|
config: cfg,
|
|
database: db,
|
|
}
|
|
if !cmd.validate(cmdInit) {
|
|
sendMessage(s, m.ChannelID, "You cannot run this command.")
|
|
return
|
|
}
|
|
if err := cmd.run(cmdInit); err != nil {
|
|
sendMessage(s, m.ChannelID, err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
func messageHandler(cfg *config.Config, db *database.Database) func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
// Ignore bots
|
|
if m.Author.Bot {
|
|
return
|
|
}
|
|
|
|
// [FIRED] increment
|
|
for _, role := range m.MentionRoles {
|
|
if cfg.FiredRole == role {
|
|
if err := db.IncrementPing(cfg.FiredRole); err != nil {
|
|
beaver.Errorf("could not increment ping for %s: %v", cfg.FiredRole, err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|