Compare commits

...

3 Commits

Author SHA1 Message Date
6c0000d409
Tidy up main 2022-08-24 08:52:30 -05:00
547063de2e
Move handlers to their own package 2022-08-23 13:25:44 -05:00
435884b61a
Move config to bot package 2022-08-23 11:52:36 -05:00
10 changed files with 144 additions and 104 deletions

View File

@ -1,4 +1,4 @@
package command package bot
const ( const (
defaultPrefix = "!" defaultPrefix = "!"
@ -25,6 +25,10 @@ type (
Emojis []string Emojis []string
Channels []string Channels []string
} }
WeatherConfig struct {
Token string `mapstructure:"token"`
}
) )
func NewConfig() Config { func NewConfig() Config {

10
bot/handlers.go Normal file
View File

@ -0,0 +1,10 @@
package bot
import (
"github.com/bwmarrin/discordgo"
)
type MessageCreateHandler interface {
Handle(s *discordgo.Session, m *discordgo.MessageCreate)
SetConfig(config Config)
}

66
bot/handlers/reaction.go Normal file
View File

@ -0,0 +1,66 @@
package handler
import (
"math/rand"
"strings"
"git.kill0.net/chill9/beepboop/bot"
"git.kill0.net/chill9/beepboop/command"
"git.kill0.net/chill9/beepboop/lib"
"github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus"
)
type (
ReactionHandler struct {
config bot.Config
}
)
func NewReactionHandler() *ReactionHandler {
return new(ReactionHandler)
}
func (h *ReactionHandler) SetConfig(config bot.Config) {
h.config = config
}
func (h *ReactionHandler) Handle(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
emojis := h.config.Handler.Reaction.Emojis
channels := h.config.Handler.Reaction.Channels
if len(emojis) == 0 {
log.Warning("emoji list is empty")
return
}
channel, err := s.Channel(m.ChannelID)
if err != nil {
log.Fatalf("unable to get channel name: %v", err)
}
if len(channels) > 0 && !lib.Contains(channels, channel.Name) {
return
}
for _, a := range m.Attachments {
if strings.HasPrefix(a.ContentType, "image/") {
for i := 1; i <= command.RandInt(1, len(emojis)); i++ {
r := emojis[rand.Intn(len(emojis))]
s.MessageReactionAdd(m.ChannelID, m.ID, r)
}
}
}
for range m.Embeds {
for i := 1; i <= command.RandInt(1, len(emojis)); i++ {
r := emojis[rand.Intn(len(emojis))]
s.MessageReactionAdd(m.ChannelID, m.ID, r)
}
}
}

View File

@ -6,12 +6,12 @@ import (
//"log" //"log"
"math/rand"
"os" "os"
"os/signal" "os/signal"
"strings"
"syscall" "syscall"
"git.kill0.net/chill9/beepboop/bot"
handler "git.kill0.net/chill9/beepboop/bot/handlers"
"git.kill0.net/chill9/beepboop/command" "git.kill0.net/chill9/beepboop/command"
"git.kill0.net/chill9/beepboop/lib" "git.kill0.net/chill9/beepboop/lib"
@ -22,9 +22,7 @@ import (
) )
var ( var (
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"} C bot.Config
C command.Config
handlers []command.CommandHandler = []command.CommandHandler{ handlers []command.CommandHandler = []command.CommandHandler{
command.NewCoinHandler(), command.NewCoinHandler(),
@ -34,44 +32,15 @@ var (
command.NewTimeHandler(), command.NewTimeHandler(),
command.NewVersionHandler("version"), command.NewVersionHandler("version"),
command.NewWeatherHandler(), command.NewWeatherHandler(),
handler.NewReactionHandler(),
} }
) )
func main() { func main() {
var ( setupConfig()
err error
)
flag.Bool("debug", false, "enable debug logging")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
}
lib.SeedMathRand() lib.SeedMathRand()
viper.SetDefault("handler.reaction.emojis", defaultReactions)
viper.SetEnvPrefix("BEEPBOOP")
viper.AutomaticEnv()
viper.SetConfigName("config")
viper.SetConfigType("toml")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err)
}
err = viper.Unmarshal(&C)
if err != nil {
log.Fatalf("unable to decode into struct: %v", err)
}
if C.DiscordToken == "" { if C.DiscordToken == "" {
log.Fatalf("Discord token is not set") log.Fatalf("Discord token is not set")
} }
@ -86,9 +55,6 @@ func main() {
dg.AddHandler(h.Handle) dg.AddHandler(h.Handle)
} }
dg.AddHandler(reactionHandler)
dg.AddHandler(praiseHandler)
dg.Identify.Intents = discordgo.IntentsGuildMessages dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open() err = dg.Open()
@ -107,61 +73,38 @@ func main() {
log.Info("Shutting down") log.Info("Shutting down")
} }
func praiseHandler(s *discordgo.Session, m *discordgo.MessageCreate) { func setupConfig() {
if m.Author.ID == s.State.User.ID { var err error
return
C = bot.NewConfig()
flag.Bool("debug", false, "enable debug logging")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetBool("debug") {
log.SetLevel(log.DebugLevel)
} }
if strings.Contains(m.Content, "good bot") { viper.SetEnvPrefix("BEEPBOOP")
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s> Thank you, daddy.", m.Author.ID)) viper.AutomaticEnv()
}
}
func reactionHandler(s *discordgo.Session, m *discordgo.MessageCreate) { viper.SetConfigName("config")
if m.Author.ID == s.State.User.ID { viper.SetConfigType("toml")
return viper.AddConfigPath(".")
err = viper.ReadInConfig()
viper.BindEnv("DISCORD_TOKEN")
viper.BindEnv("OPEN_WEATHER_MAP_TOKEN")
if _, ok := err.(viper.ConfigFileNotFoundError); !ok {
log.Fatalf("fatal error config file: %v", err)
} }
emojis := C.Handler.Reaction.Emojis err = viper.Unmarshal(&C)
channels := C.Handler.Reaction.Channels
if len(emojis) == 0 {
log.Warning("emoji list is empty")
return
}
channel, err := s.Channel(m.ChannelID)
if err != nil { if err != nil {
log.Fatalf("unable to get channel name: %v", err) log.Fatalf("unable to decode into struct: %v", err)
} }
if len(channels) > 0 && !contains(channels, channel.Name) {
return
}
for _, a := range m.Attachments {
if strings.HasPrefix(a.ContentType, "image/") {
for i := 1; i <= command.RandInt(1, len(emojis)); i++ {
r := emojis[rand.Intn(len(emojis))]
s.MessageReactionAdd(m.ChannelID, m.ID, r)
}
}
}
for range m.Embeds {
for i := 1; i <= command.RandInt(1, len(emojis)); i++ {
r := emojis[rand.Intn(len(emojis))]
s.MessageReactionAdd(m.ChannelID, m.ID, r)
}
}
}
func contains[T comparable](s []T, v T) bool {
for _, x := range s {
if x == v {
return true
}
}
return false
} }

View File

@ -1,8 +1,11 @@
package command package command
import "github.com/bwmarrin/discordgo" import (
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo"
)
type CommandHandler interface { type CommandHandler interface {
Handle(s *discordgo.Session, m *discordgo.MessageCreate) Handle(s *discordgo.Session, m *discordgo.MessageCreate)
SetConfig(config Config) SetConfig(config bot.Config)
} }

View File

@ -3,13 +3,14 @@ package command
import ( import (
"strings" "strings"
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type ( type (
PingHandler struct { PingHandler struct {
config Config config bot.Config
} }
) )
@ -17,7 +18,7 @@ func NewPingHandler() *PingHandler {
return new(PingHandler) return new(PingHandler)
} }
func (h *PingHandler) SetConfig(config Config) { func (h *PingHandler) SetConfig(config bot.Config) {
h.config = config h.config = config
} }

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@ -35,14 +36,14 @@ type (
} }
CoinHandler struct { CoinHandler struct {
config Config config bot.Config
} }
RollHandler struct { RollHandler struct {
config Config config bot.Config
} }
RouletteHandler struct { RouletteHandler struct {
config Config config bot.Config
} }
) )
@ -153,7 +154,7 @@ func NewRollHandler() *RollHandler {
return new(RollHandler) return new(RollHandler)
} }
func (h *RollHandler) SetConfig(config Config) { func (h *RollHandler) SetConfig(config bot.Config) {
h.config = config h.config = config
} }
@ -199,7 +200,7 @@ func NewRouletteHandler() *RouletteHandler {
return new(RouletteHandler) return new(RouletteHandler)
} }
func (h *RouletteHandler) SetConfig(config Config) { func (h *RouletteHandler) SetConfig(config bot.Config) {
h.config = config h.config = config
} }
@ -229,7 +230,7 @@ func NewCoinHandler() *CoinHandler {
return new(CoinHandler) return new(CoinHandler)
} }
func (h *CoinHandler) SetConfig(config Config) { func (h *CoinHandler) SetConfig(config bot.Config) {
h.config = config h.config = config
} }

View File

@ -5,13 +5,14 @@ import (
"strings" "strings"
"time" "time"
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
type ( type (
TimeHandler struct { TimeHandler struct {
config Config config bot.Config
} }
) )
@ -19,7 +20,7 @@ func NewTimeHandler() *TimeHandler {
return new(TimeHandler) return new(TimeHandler)
} }
func (h *TimeHandler) SetConfig(config Config) { func (h *TimeHandler) SetConfig(config bot.Config) {
h.config = config h.config = config
} }

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"runtime" "runtime"
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo" "github.com/bwmarrin/discordgo"
) )
@ -13,7 +14,7 @@ const (
type ( type (
VersionHandler struct { VersionHandler struct {
config Config config bot.Config
Name string Name string
} }
) )
@ -24,7 +25,7 @@ func NewVersionHandler(s string) *VersionHandler {
return h return h
} }
func (h *VersionHandler) SetConfig(config Config) { func (h *VersionHandler) SetConfig(config bot.Config) {
h.config = config h.config = config
} }

10
lib/common.go Normal file
View File

@ -0,0 +1,10 @@
package lib
func Contains[T comparable](s []T, v T) bool {
for _, x := range s {
if x == v {
return true
}
}
return false
}