Add welcome handler

Signed-off-by: jolheiser <john.olheiser@gmail.com>
welcome
jolheiser 2022-01-07 14:48:22 -06:00
parent 520cc5968c
commit e37945c5d5
Signed by: jolheiser
GPG Key ID: B853ADA5DA7BBF7A
1 changed files with 31 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package discord
import (
"fmt"
"os"
"strings"
"time"
@ -100,6 +101,7 @@ func Bot(cfg *config.Config, db *database.Database) (*discordgo.Session, error)
}
bot.AddHandler(readyHandler())
bot.AddHandler(joinHandler(cfg))
bot.AddHandler(leaveHandler(cfg))
bot.AddHandler(commandHandler(cfg, db, sapi, twitterClient))
bot.AddHandler(messageHandler(cfg, db))
@ -294,3 +296,32 @@ func updateStatus(s *discordgo.Session) {
<-ticker.C
}
}
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)
}
}
}