Compare commits

..

No commits in common. "6c0000d409d79887353039c2dd7c2d3617c707bc" and "cf3fece52c7dd30d2aa04b0a33b4092e2472a8b4" have entirely different histories.

10 changed files with 108 additions and 148 deletions

View File

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

View File

@ -1,66 +0,0 @@
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"
"math/rand"
"os"
"os/signal"
"strings"
"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/lib"
@ -22,7 +22,9 @@ import (
)
var (
C bot.Config
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
C command.Config
handlers []command.CommandHandler = []command.CommandHandler{
command.NewCoinHandler(),
@ -32,15 +34,44 @@ var (
command.NewTimeHandler(),
command.NewVersionHandler("version"),
command.NewWeatherHandler(),
handler.NewReactionHandler(),
}
)
func main() {
setupConfig()
var (
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()
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 == "" {
log.Fatalf("Discord token is not set")
}
@ -55,6 +86,9 @@ func main() {
dg.AddHandler(h.Handle)
}
dg.AddHandler(reactionHandler)
dg.AddHandler(praiseHandler)
dg.Identify.Intents = discordgo.IntentsGuildMessages
err = dg.Open()
@ -73,38 +107,61 @@ func main() {
log.Info("Shutting down")
}
func setupConfig() {
var err error
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)
func praiseHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
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)
if strings.Contains(m.Content, "good bot") {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s> Thank you, daddy.", m.Author.ID))
}
}
err = viper.Unmarshal(&C)
func reactionHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == s.State.User.ID {
return
}
emojis := C.Handler.Reaction.Emojis
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 {
log.Fatalf("unable to decode into struct: %v", err)
log.Fatalf("unable to get channel name: %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,11 +1,8 @@
package command
import (
"git.kill0.net/chill9/beepboop/bot"
"github.com/bwmarrin/discordgo"
)
import "github.com/bwmarrin/discordgo"
type CommandHandler interface {
Handle(s *discordgo.Session, m *discordgo.MessageCreate)
SetConfig(config bot.Config)
SetConfig(config Config)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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