Compare commits
3 Commits
cf3fece52c
...
6c0000d409
Author | SHA1 | Date | |
---|---|---|---|
6c0000d409 | |||
547063de2e | |||
435884b61a |
@ -1,4 +1,4 @@
|
||||
package command
|
||||
package bot
|
||||
|
||||
const (
|
||||
defaultPrefix = "!"
|
||||
@ -25,6 +25,10 @@ type (
|
||||
Emojis []string
|
||||
Channels []string
|
||||
}
|
||||
|
||||
WeatherConfig struct {
|
||||
Token string `mapstructure:"token"`
|
||||
}
|
||||
)
|
||||
|
||||
func NewConfig() Config {
|
10
bot/handlers.go
Normal file
10
bot/handlers.go
Normal 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
66
bot/handlers/reaction.go
Normal 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)
|
||||
}
|
||||
}
|
||||
}
|
121
cmd/bb/main.go
121
cmd/bb/main.go
@ -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,9 +22,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
defaultReactions []string = []string{"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩", "🔥", "🍒", "🎉", "🥳", "🎊"}
|
||||
|
||||
C command.Config
|
||||
C bot.Config
|
||||
|
||||
handlers []command.CommandHandler = []command.CommandHandler{
|
||||
command.NewCoinHandler(),
|
||||
@ -34,44 +32,15 @@ var (
|
||||
command.NewTimeHandler(),
|
||||
command.NewVersionHandler("version"),
|
||||
command.NewWeatherHandler(),
|
||||
handler.NewReactionHandler(),
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
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)
|
||||
}
|
||||
setupConfig()
|
||||
|
||||
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")
|
||||
}
|
||||
@ -86,9 +55,6 @@ func main() {
|
||||
dg.AddHandler(h.Handle)
|
||||
}
|
||||
|
||||
dg.AddHandler(reactionHandler)
|
||||
dg.AddHandler(praiseHandler)
|
||||
|
||||
dg.Identify.Intents = discordgo.IntentsGuildMessages
|
||||
|
||||
err = dg.Open()
|
||||
@ -107,61 +73,38 @@ func main() {
|
||||
log.Info("Shutting down")
|
||||
}
|
||||
|
||||
func praiseHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
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)
|
||||
}
|
||||
|
||||
if strings.Contains(m.Content, "good bot") {
|
||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("<@%s> Thank you, daddy.", m.Author.ID))
|
||||
}
|
||||
}
|
||||
viper.SetEnvPrefix("BEEPBOOP")
|
||||
viper.AutomaticEnv()
|
||||
|
||||
func reactionHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
err = viper.Unmarshal(&C)
|
||||
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
|
||||
}
|
||||
|
@ -1,8 +1,11 @@
|
||||
package command
|
||||
|
||||
import "github.com/bwmarrin/discordgo"
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type CommandHandler interface {
|
||||
Handle(s *discordgo.Session, m *discordgo.MessageCreate)
|
||||
SetConfig(config Config)
|
||||
SetConfig(config bot.Config)
|
||||
}
|
||||
|
@ -3,13 +3,14 @@ package command
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type (
|
||||
PingHandler struct {
|
||||
config Config
|
||||
config bot.Config
|
||||
}
|
||||
)
|
||||
|
||||
@ -17,7 +18,7 @@ func NewPingHandler() *PingHandler {
|
||||
return new(PingHandler)
|
||||
}
|
||||
|
||||
func (h *PingHandler) SetConfig(config Config) {
|
||||
func (h *PingHandler) SetConfig(config bot.Config) {
|
||||
h.config = config
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
@ -35,14 +36,14 @@ type (
|
||||
}
|
||||
|
||||
CoinHandler struct {
|
||||
config Config
|
||||
config bot.Config
|
||||
}
|
||||
RollHandler struct {
|
||||
config Config
|
||||
config bot.Config
|
||||
}
|
||||
|
||||
RouletteHandler struct {
|
||||
config Config
|
||||
config bot.Config
|
||||
}
|
||||
)
|
||||
|
||||
@ -153,7 +154,7 @@ func NewRollHandler() *RollHandler {
|
||||
return new(RollHandler)
|
||||
}
|
||||
|
||||
func (h *RollHandler) SetConfig(config Config) {
|
||||
func (h *RollHandler) SetConfig(config bot.Config) {
|
||||
h.config = config
|
||||
}
|
||||
|
||||
@ -199,7 +200,7 @@ func NewRouletteHandler() *RouletteHandler {
|
||||
return new(RouletteHandler)
|
||||
}
|
||||
|
||||
func (h *RouletteHandler) SetConfig(config Config) {
|
||||
func (h *RouletteHandler) SetConfig(config bot.Config) {
|
||||
h.config = config
|
||||
}
|
||||
|
||||
@ -229,7 +230,7 @@ func NewCoinHandler() *CoinHandler {
|
||||
return new(CoinHandler)
|
||||
}
|
||||
|
||||
func (h *CoinHandler) SetConfig(config Config) {
|
||||
func (h *CoinHandler) SetConfig(config bot.Config) {
|
||||
h.config = config
|
||||
}
|
||||
|
||||
|
@ -5,13 +5,14 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type (
|
||||
TimeHandler struct {
|
||||
config Config
|
||||
config bot.Config
|
||||
}
|
||||
)
|
||||
|
||||
@ -19,7 +20,7 @@ func NewTimeHandler() *TimeHandler {
|
||||
return new(TimeHandler)
|
||||
}
|
||||
|
||||
func (h *TimeHandler) SetConfig(config Config) {
|
||||
func (h *TimeHandler) SetConfig(config bot.Config) {
|
||||
h.config = config
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
@ -13,7 +14,7 @@ const (
|
||||
|
||||
type (
|
||||
VersionHandler struct {
|
||||
config Config
|
||||
config bot.Config
|
||||
Name string
|
||||
}
|
||||
)
|
||||
@ -24,7 +25,7 @@ func NewVersionHandler(s string) *VersionHandler {
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *VersionHandler) SetConfig(config Config) {
|
||||
func (h *VersionHandler) SetConfig(config bot.Config) {
|
||||
h.config = config
|
||||
}
|
||||
|
||||
|
10
lib/common.go
Normal file
10
lib/common.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user