2020-06-09 13:04:44 +00:00
|
|
|
package discord
|
|
|
|
|
|
|
|
import (
|
2020-10-25 21:17:40 +00:00
|
|
|
"fmt"
|
2022-01-07 20:48:22 +00:00
|
|
|
"os"
|
2020-06-09 13:04:44 +00:00
|
|
|
"strings"
|
2020-07-02 20:00:22 +00:00
|
|
|
"time"
|
2020-06-09 13:04:44 +00:00
|
|
|
|
2022-01-07 20:47:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
|
|
|
"git.jojodev.com/Minecraft/go-serverapi"
|
|
|
|
|
|
|
|
"git.jojodev.com/Minecraft/canopeas/config"
|
|
|
|
"git.jojodev.com/Minecraft/canopeas/database"
|
2020-06-09 13:04:44 +00:00
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
2021-02-22 18:27:49 +00:00
|
|
|
"github.com/dghubble/go-twitter/twitter"
|
|
|
|
"github.com/dghubble/oauth1"
|
2020-06-09 13:04:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Register commands to this map
|
2020-06-12 05:17:58 +00:00
|
|
|
var (
|
2021-02-23 05:53:55 +00:00
|
|
|
commands = make([]*command, 0)
|
|
|
|
commandMap = make(map[string]*command)
|
2020-06-12 05:17:58 +00:00
|
|
|
messageRoleMap = make(map[string]map[string]string)
|
2020-07-02 20:00:22 +00:00
|
|
|
|
|
|
|
memeRateLimit *rateLimit
|
2021-02-23 05:53:55 +00:00
|
|
|
embedColor = 0x007D96
|
2020-06-12 05:17:58 +00:00
|
|
|
)
|
2020-06-09 13:04:44 +00:00
|
|
|
|
|
|
|
type commandInit struct {
|
2021-02-22 18:27:49 +00:00
|
|
|
session *discordgo.Session
|
|
|
|
message *discordgo.Message
|
|
|
|
config *config.Config
|
|
|
|
database *database.Database
|
|
|
|
sapiClient *serverapi.Client
|
|
|
|
twitterClient *twitter.Client
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type command struct {
|
2021-02-23 05:53:55 +00:00
|
|
|
staffOnly bool
|
|
|
|
deleteInvocation bool
|
2021-05-14 03:10:02 +00:00
|
|
|
child bool
|
2021-02-22 18:27:49 +00:00
|
|
|
// TODO Does this really need to exist separately?
|
2020-06-09 13:04:44 +00:00
|
|
|
validate func(cmd commandInit) bool
|
2020-06-12 05:17:58 +00:00
|
|
|
run func(cmd commandInit) (string, error)
|
2021-02-23 05:53:55 +00:00
|
|
|
name string
|
|
|
|
aliases []string
|
2020-06-09 13:04:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-05-14 03:10:02 +00:00
|
|
|
// Init rand
|
2021-01-17 02:45:06 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
|
2021-02-22 18:27:49 +00:00
|
|
|
// Init ServerAPI
|
|
|
|
sapi := serverapi.NewClient(cfg.ServerAPI.Endpoint, serverapi.WithToken(cfg.ServerAPI.Token))
|
|
|
|
|
|
|
|
// Init Twitter
|
|
|
|
twitterConfig := oauth1.NewConfig(cfg.Twitter.ConsumerKey, cfg.Twitter.ConsumerSecret)
|
|
|
|
twitterToken := oauth1.NewToken(cfg.Twitter.AccessToken, cfg.Twitter.AccessSecret)
|
|
|
|
twitterHttpClient := twitterConfig.Client(oauth1.NoContext, twitterToken)
|
|
|
|
twitterClient := twitter.NewClient(twitterHttpClient)
|
|
|
|
|
|
|
|
// Init Unban Schedule
|
|
|
|
sched := &unbanSchedule{
|
|
|
|
db: db,
|
|
|
|
sapi: sapi,
|
|
|
|
}
|
|
|
|
go sched.Run()
|
|
|
|
|
2020-06-12 05:17:58 +00:00
|
|
|
for _, messageRole := range cfg.MessageRoles {
|
|
|
|
if messageRoleMap[messageRole.MessageID] == nil {
|
|
|
|
messageRoleMap[messageRole.MessageID] = make(map[string]string)
|
|
|
|
}
|
2021-05-05 02:45:49 +00:00
|
|
|
for _, reaction := range messageRole.Reactions {
|
|
|
|
_ = bot.MessageReactionAdd(messageRole.ChannelID, messageRole.MessageID, reaction.Emoji)
|
|
|
|
messageRoleMap[messageRole.MessageID][reaction.Emoji] = reaction.RoleID
|
|
|
|
}
|
2020-06-12 05:17:58 +00:00
|
|
|
}
|
2020-06-09 13:04:44 +00:00
|
|
|
|
2021-02-23 05:53:55 +00:00
|
|
|
// Init commandMap
|
2021-05-14 03:10:02 +00:00
|
|
|
if err := Album(cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-12 16:38:18 +00:00
|
|
|
Echo(cfg)
|
2021-02-23 05:53:55 +00:00
|
|
|
for _, c := range commands {
|
|
|
|
if c.name == "" {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("command is missing a name: %s", c.help)
|
2021-02-23 05:53:55 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
commandMap[c.name] = c
|
|
|
|
for _, a := range c.aliases {
|
|
|
|
commandMap[a] = c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:01:17 +00:00
|
|
|
bot.AddHandler(readyHandler())
|
2022-01-07 20:48:22 +00:00
|
|
|
bot.AddHandler(joinHandler(cfg))
|
2020-10-25 21:17:40 +00:00
|
|
|
bot.AddHandler(leaveHandler(cfg))
|
2021-02-22 18:27:49 +00:00
|
|
|
bot.AddHandler(commandHandler(cfg, db, sapi, twitterClient))
|
2020-06-09 13:04:44 +00:00
|
|
|
bot.AddHandler(messageHandler(cfg, db))
|
2020-06-12 05:17:58 +00:00
|
|
|
bot.AddHandler(reactionAddHandler())
|
|
|
|
bot.AddHandler(reactionRemoveHandler())
|
2020-06-09 13:04:44 +00:00
|
|
|
|
2020-07-02 20:00:22 +00:00
|
|
|
// Rate limits
|
|
|
|
d, err := time.ParseDuration(cfg.MemeRate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
memeRateLimit = NewRateLimit(d)
|
|
|
|
|
2020-10-25 21:17:40 +00:00
|
|
|
// Intents
|
|
|
|
bot.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAll)
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
return bot, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func sendTyping(s *discordgo.Session, channelID string) {
|
|
|
|
if err := s.ChannelTyping(channelID); err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("could not send typing status: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:43:43 +00:00
|
|
|
func sendMessage(s *discordgo.Session, channelID, content string, scrub bool) *discordgo.Message {
|
|
|
|
var msg *discordgo.Message
|
|
|
|
var err error
|
|
|
|
if scrub {
|
|
|
|
msg, err = s.ChannelMessageSendComplex(channelID, &discordgo.MessageSend{
|
2020-07-02 21:52:20 +00:00
|
|
|
Content: content,
|
2020-07-02 20:43:43 +00:00
|
|
|
AllowedMentions: &discordgo.MessageAllowedMentions{},
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
msg, err = s.ChannelMessageSend(channelID, content)
|
|
|
|
}
|
2020-06-09 13:04:44 +00:00
|
|
|
if err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("could not send message: %v", err)
|
2020-06-09 13:04:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-07-02 20:00:22 +00:00
|
|
|
func sendEmbed(s *discordgo.Session, channelID string, embed *discordgo.MessageEmbed) *discordgo.Message {
|
2021-02-23 05:53:55 +00:00
|
|
|
embed.Color = embedColor
|
2020-07-02 20:00:22 +00:00
|
|
|
msg, err := s.ChannelMessageSendEmbed(channelID, embed)
|
|
|
|
if err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("could not send embed: %v", err)
|
2020-07-02 20:00:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
func isStaff(authorRoleIDs, staffRoleIDs []string) bool {
|
|
|
|
for _, aRole := range authorRoleIDs {
|
|
|
|
for _, sRole := range staffRoleIDs {
|
|
|
|
if aRole == sRole {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-06-12 17:01:17 +00:00
|
|
|
func readyHandler() func(s *discordgo.Session, m *discordgo.Ready) {
|
|
|
|
return func(s *discordgo.Session, r *discordgo.Ready) {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Info().Msgf("https://discord.com/api/oauth2/authorize?client_id=%s&permissions=0&redirect_uri=https://birbmc.com&scope=bot", r.User.ID)
|
2021-05-14 03:27:39 +00:00
|
|
|
|
|
|
|
// Init status changer
|
|
|
|
go updateStatus(s)
|
2020-06-12 17:01:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-22 18:27:49 +00:00
|
|
|
func commandHandler(cfg *config.Config, db *database.Database, sapi *serverapi.Client, twitterClient *twitter.Client) func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
2020-06-09 13:04:44 +00:00
|
|
|
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])
|
|
|
|
|
2021-02-23 05:53:55 +00:00
|
|
|
cmd, ok := commandMap[cmdArg]
|
2020-06-12 05:17:58 +00:00
|
|
|
if !ok {
|
2020-06-09 13:04:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-02-22 18:27:49 +00:00
|
|
|
if cmd.staffOnly && !isStaff(m.Member.Roles, cfg.StaffRoles) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-09 13:04:44 +00:00
|
|
|
cmdInit := commandInit{
|
2021-02-22 18:27:49 +00:00
|
|
|
session: s,
|
|
|
|
message: m.Message,
|
|
|
|
config: cfg,
|
|
|
|
database: db,
|
|
|
|
sapiClient: sapi,
|
|
|
|
twitterClient: twitterClient,
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
if !cmd.validate(cmdInit) {
|
2020-07-02 20:43:43 +00:00
|
|
|
sendMessage(s, m.ChannelID, "You cannot run this command.", false)
|
2020-06-09 13:04:44 +00:00
|
|
|
return
|
|
|
|
}
|
2021-02-23 05:53:55 +00:00
|
|
|
if cmd.deleteInvocation {
|
|
|
|
if err := s.ChannelMessageDelete(m.Message.ChannelID, m.Message.ID); err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Warn().Msgf("could not remove invocation for %s: %v", m.Content, err)
|
2021-02-23 05:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-12 05:17:58 +00:00
|
|
|
feedback, err := cmd.run(cmdInit)
|
|
|
|
if err != nil {
|
|
|
|
feedback = "Internal error"
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("error while running %s: %v", cmdArg, err)
|
2020-06-12 05:17:58 +00:00
|
|
|
}
|
|
|
|
if len(feedback) > 0 {
|
2020-07-02 20:43:43 +00:00
|
|
|
sendMessage(s, m.ChannelID, feedback, false)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("could not increment ping for %s: %v", cfg.FiredRole, err)
|
2020-06-09 13:04:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 05:17:58 +00:00
|
|
|
|
|
|
|
func reactionAddHandler() func(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
|
|
|
return func(s *discordgo.Session, m *discordgo.MessageReactionAdd) {
|
|
|
|
reactionHandler(true, s, m.MessageReaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reactionRemoveHandler() func(s *discordgo.Session, m *discordgo.MessageReactionRemove) {
|
|
|
|
return func(s *discordgo.Session, m *discordgo.MessageReactionRemove) {
|
|
|
|
reactionHandler(false, s, m.MessageReaction)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func reactionHandler(add bool, s *discordgo.Session, m *discordgo.MessageReaction) {
|
|
|
|
if _, ok := messageRoleMap[m.MessageID]; ok {
|
2020-06-12 14:57:01 +00:00
|
|
|
if r, ok := messageRoleMap[m.MessageID][m.Emoji.APIName()]; ok {
|
2020-06-12 05:17:58 +00:00
|
|
|
var roleCmd func(guildID, userID, roleID string) (err error)
|
|
|
|
if add {
|
|
|
|
roleCmd = s.GuildMemberRoleAdd
|
|
|
|
} else {
|
|
|
|
roleCmd = s.GuildMemberRoleRemove
|
|
|
|
}
|
|
|
|
if err := roleCmd(m.GuildID, m.UserID, r); err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Error().Msgf("could not modify role %s for user %s", r, m.UserID)
|
2020-06-12 05:17:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-25 21:17:40 +00:00
|
|
|
|
|
|
|
func leaveHandler(cfg *config.Config) func(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
|
|
|
|
return func(s *discordgo.Session, m *discordgo.GuildMemberRemove) {
|
2020-10-30 20:34:11 +00:00
|
|
|
sendMessage(s, cfg.LeaveChannel, fmt.Sprintf("%s (%s) left the server. :sob:", m.Mention(), m.User.String()), true)
|
2020-10-25 21:17:40 +00:00
|
|
|
}
|
|
|
|
}
|
2021-05-14 03:27:39 +00:00
|
|
|
|
|
|
|
func updateStatus(s *discordgo.Session) {
|
|
|
|
ticker := time.NewTicker(time.Minute * 30)
|
|
|
|
for {
|
|
|
|
dj, err := newDadJoke()
|
|
|
|
if err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Warn().Msgf("could not get new dad joke: %v", err)
|
2021-05-14 03:27:39 +00:00
|
|
|
} else if err := s.UpdateStatus(1, dj.Joke); err != nil {
|
2022-01-07 20:47:35 +00:00
|
|
|
log.Warn().Msgf("could not update status: %v", err)
|
2021-05-14 03:27:39 +00:00
|
|
|
}
|
|
|
|
<-ticker.C
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 20:48:22 +00:00
|
|
|
|
|
|
|
func joinHandler(cfg *config.Config) func(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
|
|
|
return func(s *discordgo.Session, m *discordgo.GuildMemberAdd) {
|
|
|
|
sendMessage(s, cfg.Welcome.Channel, os.Expand(cfg.Welcome.Message, func(s string) string {
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case "user":
|
|
|
|
return m.Mention()
|
|
|
|
default:
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}), false)
|
|
|
|
|
|
|
|
if cfg.Welcome.DM != "" {
|
|
|
|
dm, err := s.UserChannelCreate(m.User.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msgf("could not create DM with %s", m.User.Username)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
sendMessage(s, dm.ID, os.Expand(cfg.Welcome.DM, func(s string) string {
|
|
|
|
switch strings.ToLower(s) {
|
|
|
|
case "user":
|
|
|
|
return m.Mention()
|
|
|
|
default:
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}), false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|