Compare commits
No commits in common. "446ac616bf15c454191422696d3b10677ec47b03" and "8606ed0200682f40fde4960fe55066319ee19fdc" have entirely different histories.
446ac616bf
...
8606ed0200
@ -1,4 +0,0 @@
|
||||
---
|
||||
linters:
|
||||
disable:
|
||||
- errcheck
|
147
bot/bot.go
147
bot/bot.go
@ -1,147 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var C Config
|
||||
|
||||
type (
|
||||
Bot struct {
|
||||
Session *discordgo.Session
|
||||
Config Config
|
||||
}
|
||||
|
||||
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
|
||||
)
|
||||
|
||||
func NewBot(s *discordgo.Session, config Config) *Bot {
|
||||
return &Bot{Session: s, Config: config}
|
||||
}
|
||||
|
||||
func (b *Bot) RegisterCommands() {
|
||||
AddCommand(&Command{
|
||||
Name: "coin",
|
||||
Func: b.CoinCommand(),
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "deal",
|
||||
Func: b.DealCommand(),
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "ping",
|
||||
Func: b.PingCommand(),
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "roll",
|
||||
Func: b.RollCommand(),
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "time",
|
||||
Func: b.TimeCommand(),
|
||||
NArgs: 1,
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "version",
|
||||
Func: b.VersionCommand(),
|
||||
})
|
||||
AddCommand(&Command{
|
||||
Name: "weather",
|
||||
Func: b.WeatherCommand(),
|
||||
NArgs: 1,
|
||||
})
|
||||
}
|
||||
|
||||
func (b *Bot) RegisterHandlers() {
|
||||
b.Session.AddHandler(b.CommandHandler())
|
||||
b.Session.AddHandler(b.ReactionHandler())
|
||||
}
|
||||
|
||||
func Run() error {
|
||||
setupConfig()
|
||||
|
||||
if err := lib.SeedMathRand(); err != nil {
|
||||
log.Warn(err)
|
||||
}
|
||||
|
||||
if C.DiscordToken == "" {
|
||||
log.Fatalf("Discord token is not set")
|
||||
}
|
||||
|
||||
dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken))
|
||||
if err != nil {
|
||||
log.Fatalf("error creating Discord session: %v\n", err)
|
||||
}
|
||||
|
||||
b := NewBot(dg, C)
|
||||
b.RegisterHandlers()
|
||||
b.RegisterCommands()
|
||||
|
||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||
|
||||
err = dg.Open()
|
||||
if err != nil {
|
||||
log.Fatalf("error opening connection: %v\n", err)
|
||||
}
|
||||
|
||||
log.Info("The bot is now running. Press CTRL-C to exit.")
|
||||
|
||||
defer dg.Close()
|
||||
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-sc
|
||||
|
||||
log.Info("Shutting down")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setupConfig() {
|
||||
var err error
|
||||
|
||||
C = NewConfig()
|
||||
|
||||
flag.Bool("debug", false, "enable debug logging")
|
||||
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
|
||||
pflag.Parse()
|
||||
viper.BindPFlags(pflag.CommandLine)
|
||||
|
||||
viper.SetEnvPrefix("BEEPBOOP")
|
||||
viper.AutomaticEnv()
|
||||
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("toml")
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
err = viper.ReadInConfig()
|
||||
|
||||
viper.BindEnv("DEBUG")
|
||||
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 viper.GetBool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
}
|
31
bot/coin.go
31
bot/coin.go
@ -1,31 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
type Coin bool
|
||||
|
||||
func (c *Coin) Flip() bool {
|
||||
*c = Coin(lib.Itob(lib.RandInt(0, 1)))
|
||||
return bool(*c)
|
||||
}
|
||||
|
||||
func (b *Bot) CoinCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
c Coin
|
||||
msg string
|
||||
)
|
||||
|
||||
if c.Flip() {
|
||||
msg = "heads"
|
||||
} else {
|
||||
msg = "tails"
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, msg)
|
||||
return nil
|
||||
}
|
||||
}
|
@ -18,13 +18,13 @@ type (
|
||||
}
|
||||
|
||||
Command struct {
|
||||
Name string
|
||||
Config Config
|
||||
Func CommandFunc
|
||||
NArgs int
|
||||
Name string
|
||||
Config Config
|
||||
Func func(cmd *Command, args []string) error
|
||||
NArgs int
|
||||
Session *discordgo.Session
|
||||
Message *discordgo.MessageCreate
|
||||
}
|
||||
|
||||
CommandFunc func(args []string, m *discordgo.MessageCreate) error
|
||||
)
|
||||
|
||||
func init() {
|
||||
@ -55,7 +55,7 @@ func GetCommand(name string) (*Command, bool) {
|
||||
return cmd, ok
|
||||
}
|
||||
|
||||
func (b *Bot) CommandHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
|
||||
func NewCommandHandler(config Config) func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
var cmd *Command
|
||||
|
||||
@ -63,23 +63,24 @@ func (b *Bot) CommandHandler() func(*discordgo.Session, *discordgo.MessageCreate
|
||||
return
|
||||
}
|
||||
|
||||
if !lib.HasCommand(m.Content, b.Config.Prefix) {
|
||||
if !lib.HasCommand(m.Content, config.Prefix) {
|
||||
return
|
||||
}
|
||||
|
||||
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.Config.Prefix)
|
||||
cmdName, arg := lib.SplitCommandAndArg(m.Content, config.Prefix)
|
||||
|
||||
cmd, ok := GetCommand(cmdName)
|
||||
|
||||
args := lib.SplitArgs(arg, cmd.NArgs)
|
||||
|
||||
if ok {
|
||||
cmd.Config = b.Config
|
||||
cmd.Config = config
|
||||
cmd.Name = cmdName
|
||||
cmd.Session = s
|
||||
cmd.Message = m
|
||||
|
||||
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
||||
if err := cmd.Func(args, m); err != nil {
|
||||
log.Errorf("failed to execute command: %s", err)
|
||||
}
|
||||
cmd.Func(cmd, args)
|
||||
|
||||
return
|
||||
}
|
||||
|
29
bot/commands/coin.go
Normal file
29
bot/commands/coin.go
Normal file
@ -0,0 +1,29 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
)
|
||||
|
||||
type Coin bool
|
||||
|
||||
func (c *Coin) Flip() bool {
|
||||
*c = Coin(lib.Itob(lib.RandInt(0, 1)))
|
||||
return bool(*c)
|
||||
}
|
||||
|
||||
func CoinCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
c Coin
|
||||
msg string
|
||||
)
|
||||
|
||||
if c.Flip() {
|
||||
msg = "heads"
|
||||
} else {
|
||||
msg = "tails"
|
||||
}
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, msg)
|
||||
return nil
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package bot
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@ -7,11 +7,16 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type (
|
||||
DealHandler struct {
|
||||
config bot.Config
|
||||
Name string
|
||||
}
|
||||
|
||||
Card string
|
||||
|
||||
Deck [52]Card
|
||||
@ -45,37 +50,37 @@ func (d *Deck) Deal(n int) ([]Card, error) {
|
||||
return hand, err
|
||||
}
|
||||
|
||||
func (b *Bot) DealCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
rand.Shuffle(len(deck), func(i, j int) {
|
||||
deck[i], deck[j] = deck[j], deck[i]
|
||||
})
|
||||
func DealCommand(cmd *bot.Command, args []string) error {
|
||||
rand.Shuffle(len(deck), func(i, j int) {
|
||||
deck[i], deck[j] = deck[j], deck[i]
|
||||
})
|
||||
|
||||
log.Debugf("%+v", deck)
|
||||
log.Debugf("%+v", deck)
|
||||
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, "help: `!deal <n>`")
|
||||
return nil
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
log.Errorf("failed to convert string to int: %s", err)
|
||||
}
|
||||
|
||||
hand, err := deck.Deal(n)
|
||||
if err != nil {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf("error: %s\n", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, JoinCards(hand, " "))
|
||||
if len(args) != 1 {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf("help: `!%s <n>`", cmd.Name))
|
||||
return nil
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(args[0])
|
||||
if err != nil {
|
||||
log.Errorf("failed to convert string to int: %s", err)
|
||||
}
|
||||
|
||||
hand, err := deck.Deal(n)
|
||||
if err != nil {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf("error: %s\n", err))
|
||||
return nil
|
||||
}
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, JoinCards(hand, " "))
|
||||
return nil
|
||||
}
|
||||
|
||||
func JoinCards(h []Card, sep string) string {
|
||||
b := make([]string, len(h))
|
||||
var b []string
|
||||
|
||||
b = make([]string, len(h))
|
||||
|
||||
for i, v := range h {
|
||||
b[i] = string(v)
|
@ -1,4 +1,4 @@
|
||||
package bot
|
||||
package commands
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@ -7,9 +7,9 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -78,28 +78,26 @@ func (r *Roll) RollDice() {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) RollCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
err error
|
||||
msg, roll string
|
||||
r *Roll
|
||||
)
|
||||
func RollCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
msg, roll string
|
||||
r *Roll
|
||||
)
|
||||
|
||||
roll = args[0]
|
||||
roll = args[0]
|
||||
|
||||
r, err = ParseRoll(roll)
|
||||
if err != nil {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
r.RollDice()
|
||||
log.Debugf("rolled dice: %+v", r)
|
||||
|
||||
msg = fmt.Sprintf("🎲 %s = %d", lib.JoinInt(r.Rolls, " + "), r.Sum)
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, msg)
|
||||
r, err = ParseRoll(roll)
|
||||
if err != nil {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
|
||||
r.RollDice()
|
||||
log.Debugf("rolled dice: %+v", r)
|
||||
|
||||
msg = fmt.Sprintf("🎲 %s = %d", lib.JoinInt(r.Rolls, " + "), r.Sum)
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, msg)
|
||||
return nil
|
||||
}
|
10
bot/commands/ping.go
Normal file
10
bot/commands/ping.go
Normal file
@ -0,0 +1,10 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
)
|
||||
|
||||
func PingCommand(cmd *bot.Command, args []string) error {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, "pong")
|
||||
return nil
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
package bot
|
||||
package commands
|
||||
|
||||
import (
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
@ -36,7 +36,7 @@ func (g *Gun) Load(n int) {
|
||||
g.N = 0
|
||||
for i := 1; i <= n; {
|
||||
x := lib.RandInt(0, len(g.C)-1)
|
||||
if !g.C[x] {
|
||||
if g.C[x] == false {
|
||||
g.C[x] = true
|
||||
i++
|
||||
} else {
|
||||
@ -58,7 +58,7 @@ func (g *Gun) Fire() bool {
|
||||
|
||||
func (g *Gun) IsEmpty() bool {
|
||||
for _, v := range g.C {
|
||||
if v {
|
||||
if v == true {
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -66,19 +66,17 @@ func (g *Gun) IsEmpty() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *Bot) RouletteCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
if gun.IsEmpty() {
|
||||
gun.Load(Bullets)
|
||||
log.Debugf("reloading gun: %+v\n", gun)
|
||||
}
|
||||
|
||||
log.Debugf("firing gun: %+v\n", gun)
|
||||
if gun.Fire() {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, GunFireMessage)
|
||||
} else {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, GunClickMessage)
|
||||
}
|
||||
return nil
|
||||
func RouletteCommand(cmd *bot.Command, args []string) error {
|
||||
if gun.IsEmpty() {
|
||||
gun.Load(Bullets)
|
||||
log.Debugf("reloading gun: %+v\n", gun)
|
||||
}
|
||||
|
||||
log.Debugf("firing gun: %+v\n", gun)
|
||||
if gun.Fire() {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, GunFireMessage)
|
||||
} else {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, GunClickMessage)
|
||||
}
|
||||
return nil
|
||||
}
|
34
bot/commands/time.go
Normal file
34
bot/commands/time.go
Normal file
@ -0,0 +1,34 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func TimeCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
t time.Time
|
||||
tz string
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
if len(args) == 1 {
|
||||
tz = args[0]
|
||||
loc, err := time.LoadLocation(tz)
|
||||
if err != nil {
|
||||
log.Warnf("failed to load location: %s", err)
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
t = now.In(loc)
|
||||
} else {
|
||||
t = now
|
||||
}
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprint(t))
|
||||
return nil
|
||||
}
|
23
bot/commands/version.go
Normal file
23
bot/commands/version.go
Normal file
@ -0,0 +1,23 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
)
|
||||
|
||||
const (
|
||||
SourceURI = "https://git.kill0.net/chill9/bb"
|
||||
)
|
||||
|
||||
func VersionCommand(cmd *bot.Command, args []string) error {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf(
|
||||
"go version: %s\nplatform: %s\nos: %s\nsource: %s\n",
|
||||
runtime.Version(),
|
||||
runtime.GOARCH,
|
||||
runtime.GOOS,
|
||||
SourceURI,
|
||||
))
|
||||
return nil
|
||||
}
|
67
bot/commands/weather.go
Normal file
67
bot/commands/weather.go
Normal file
@ -0,0 +1,67 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/lib/weather"
|
||||
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type WeatherHandler struct {
|
||||
Config bot.Config
|
||||
Name string
|
||||
}
|
||||
|
||||
func NewWeatherHandler(s string) *WeatherHandler {
|
||||
return &WeatherHandler{Name: s}
|
||||
}
|
||||
|
||||
func (h *WeatherHandler) SetConfig(config bot.Config) {
|
||||
h.Config = config
|
||||
}
|
||||
|
||||
func WeatherCommand(cmd *bot.Command, args []string) error {
|
||||
var (
|
||||
err error
|
||||
loc string
|
||||
w weather.Weather
|
||||
)
|
||||
|
||||
if len(args) != 1 {
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
|
||||
return nil
|
||||
}
|
||||
|
||||
loc = args[0]
|
||||
|
||||
if cmd.Config.OpenWeatherMapToken == "" {
|
||||
log.Error("OpenWeather token is not set")
|
||||
return nil
|
||||
}
|
||||
|
||||
wc := weather.NewClient(cmd.Config.OpenWeatherMapToken)
|
||||
|
||||
log.Debugf("weather requested for '%s'", loc)
|
||||
|
||||
w, err = wc.Get(loc)
|
||||
if err != nil {
|
||||
log.Errorf("weather client error: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Debugf("weather returned for '%s': %+v", loc, w)
|
||||
|
||||
cmd.Session.ChannelMessageSend(cmd.Message.ChannelID, fmt.Sprintf(
|
||||
"%s (%.1f, %.1f) — C:%.1f F:%.1f K:%.1f",
|
||||
loc,
|
||||
w.Coord.Lat,
|
||||
w.Coord.Lon,
|
||||
w.Main.Temp.Celcius(),
|
||||
w.Main.Temp.Fahrenheit(),
|
||||
w.Main.Temp.Kelvin(),
|
||||
))
|
||||
|
||||
return nil
|
||||
}
|
65
bot/handler/reaction.go
Normal file
65
bot/handler/reaction.go
Normal file
@ -0,0 +1,65 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"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 <= lib.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 <= lib.RandInt(1, len(emojis)); i++ {
|
||||
r := emojis[rand.Intn(len(emojis))]
|
||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||
}
|
||||
}
|
||||
}
|
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(*discordgo.Session, *discordgo.MessageCreate)
|
||||
SetConfig(Config)
|
||||
}
|
12
bot/ping.go
12
bot/ping.go
@ -1,12 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
func (b *Bot) PingCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, "pong")
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,52 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"strings"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (b *Bot) ReactionHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
|
||||
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == s.State.User.ID {
|
||||
return
|
||||
}
|
||||
|
||||
emojis := b.Config.Handler.Reaction.Emojis
|
||||
channels := b.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 <= lib.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 <= lib.RandInt(1, len(emojis)); i++ {
|
||||
r := emojis[rand.Intn(len(emojis))]
|
||||
s.MessageReactionAdd(m.ChannelID, m.ID, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
36
bot/time.go
36
bot/time.go
@ -1,36 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (b *Bot) TimeCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
t time.Time
|
||||
tz string
|
||||
)
|
||||
|
||||
now := time.Now()
|
||||
|
||||
if len(args) == 1 {
|
||||
tz = args[0]
|
||||
loc, err := time.LoadLocation(tz)
|
||||
if err != nil {
|
||||
log.Warnf("failed to load location: %s", err)
|
||||
b.Session.ChannelMessageSend(m.ChannelID, err.Error())
|
||||
return nil
|
||||
}
|
||||
t = now.In(loc)
|
||||
} else {
|
||||
t = now
|
||||
}
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprint(t))
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
)
|
||||
|
||||
const (
|
||||
SourceURI = "https://git.kill0.net/chill9/bb"
|
||||
)
|
||||
|
||||
func (b *Bot) VersionCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"go version: %s\nplatform: %s\nos: %s\nsource: %s\n",
|
||||
runtime.Version(),
|
||||
runtime.GOARCH,
|
||||
runtime.GOOS,
|
||||
SourceURI,
|
||||
))
|
||||
return nil
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
package bot
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/lib/weather"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (b *Bot) WeatherCommand() CommandFunc {
|
||||
return func(args []string, m *discordgo.MessageCreate) error {
|
||||
var (
|
||||
err error
|
||||
loc string
|
||||
w weather.Weather
|
||||
)
|
||||
|
||||
if len(args) != 1 {
|
||||
b.Session.ChannelMessageSend(m.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
|
||||
return nil
|
||||
}
|
||||
|
||||
loc = args[0]
|
||||
|
||||
if b.Config.OpenWeatherMapToken == "" {
|
||||
log.Error("OpenWeather token is not set")
|
||||
return nil
|
||||
}
|
||||
|
||||
wc := weather.NewClient(b.Config.OpenWeatherMapToken)
|
||||
|
||||
log.Debugf("weather requested for '%s'", loc)
|
||||
|
||||
w, err = wc.Get(loc)
|
||||
if err != nil {
|
||||
log.Errorf("weather client error: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Debugf("weather returned for '%s': %+v", loc, w)
|
||||
|
||||
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||
"%s (%.1f, %.1f) — C:%.1f F:%.1f K:%.1f",
|
||||
loc,
|
||||
w.Coord.Lat,
|
||||
w.Coord.Lon,
|
||||
w.Main.Temp.Celcius(),
|
||||
w.Main.Temp.Fahrenheit(),
|
||||
w.Main.Temp.Kelvin(),
|
||||
))
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
132
cmd/bb/main.go
132
cmd/bb/main.go
@ -1,13 +1,141 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
//"log"
|
||||
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"git.kill0.net/chill9/beepboop/bot"
|
||||
"git.kill0.net/chill9/beepboop/bot/commands"
|
||||
"git.kill0.net/chill9/beepboop/bot/handler"
|
||||
"git.kill0.net/chill9/beepboop/lib"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/pflag"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var (
|
||||
C bot.Config
|
||||
|
||||
handlers []bot.MessageCreateHandler = []bot.MessageCreateHandler{
|
||||
handler.NewReactionHandler(),
|
||||
}
|
||||
)
|
||||
|
||||
func init() {
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "coin",
|
||||
Func: commands.CoinCommand,
|
||||
})
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "deal",
|
||||
Func: commands.DealCommand,
|
||||
NArgs: 1,
|
||||
})
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "ping",
|
||||
Func: commands.PingCommand,
|
||||
})
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "roll",
|
||||
Func: commands.RollCommand,
|
||||
NArgs: 1,
|
||||
})
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "time",
|
||||
Func: commands.TimeCommand,
|
||||
NArgs: 1,
|
||||
})
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "version",
|
||||
Func: commands.VersionCommand,
|
||||
})
|
||||
bot.AddCommand(&bot.Command{
|
||||
Name: "weather",
|
||||
Func: commands.WeatherCommand,
|
||||
NArgs: 1,
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
if err := bot.Run(); err != nil {
|
||||
os.Exit(1)
|
||||
setupConfig()
|
||||
|
||||
lib.SeedMathRand()
|
||||
|
||||
if C.DiscordToken == "" {
|
||||
log.Fatalf("Discord token is not set")
|
||||
}
|
||||
|
||||
dg, err := discordgo.New(fmt.Sprintf("Bot %s", C.DiscordToken))
|
||||
if err != nil {
|
||||
log.Fatalf("error creating Discord session: %v\n", err)
|
||||
}
|
||||
|
||||
for _, h := range handlers {
|
||||
h.SetConfig(C)
|
||||
dg.AddHandler(h.Handle)
|
||||
}
|
||||
|
||||
dg.AddHandler(bot.NewCommandHandler(C))
|
||||
|
||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||
|
||||
err = dg.Open()
|
||||
if err != nil {
|
||||
log.Fatalf("error opening connection: %v\n", err)
|
||||
}
|
||||
|
||||
log.Info("The bot is now running. Press CTRL-C to exit.")
|
||||
|
||||
defer dg.Close()
|
||||
|
||||
sc := make(chan os.Signal, 1)
|
||||
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
|
||||
<-sc
|
||||
|
||||
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)
|
||||
|
||||
viper.SetEnvPrefix("BEEPBOOP")
|
||||
viper.AutomaticEnv()
|
||||
|
||||
viper.SetConfigName("config")
|
||||
viper.SetConfigType("toml")
|
||||
viper.AddConfigPath(".")
|
||||
|
||||
err = viper.ReadInConfig()
|
||||
|
||||
viper.BindEnv("DEBUG")
|
||||
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 viper.GetBool("debug") {
|
||||
log.SetLevel(log.DebugLevel)
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,9 @@ func Contains[T comparable](s []T, v T) bool {
|
||||
}
|
||||
|
||||
func JoinInt(a []int, sep string) string {
|
||||
b := make([]string, len(a))
|
||||
var b []string
|
||||
|
||||
b = make([]string, len(a))
|
||||
|
||||
for i, v := range a {
|
||||
b[i] = strconv.Itoa(v)
|
||||
@ -35,7 +37,11 @@ func SumInt(a []int) int {
|
||||
}
|
||||
|
||||
func Itob(v int) bool {
|
||||
return v == 1
|
||||
if v == 1 {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func BuildURI(rawuri, rawpath string) string {
|
||||
|
Loading…
Reference in New Issue
Block a user