Compare commits
1 Commits
develop
...
333bc3b26c
Author | SHA1 | Date | |
---|---|---|---|
333bc3b26c
|
128
bot/bot.go
128
bot/bot.go
@ -7,9 +7,6 @@ import (
|
|||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/command"
|
|
||||||
"git.kill0.net/chill9/beepboop/config"
|
|
||||||
"git.kill0.net/chill9/beepboop/handler"
|
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
"github.com/bwmarrin/discordgo"
|
"github.com/bwmarrin/discordgo"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
@ -17,23 +14,14 @@ import (
|
|||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
|
||||||
var C *config.Config
|
var C Config
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Bot struct {
|
Bot struct {
|
||||||
config *config.Config
|
Session *discordgo.Session
|
||||||
session *discordgo.Session
|
Config Config
|
||||||
commands map[string]*Command
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Command struct {
|
|
||||||
Name string
|
|
||||||
Func CommandFunc
|
|
||||||
NArgs int
|
|
||||||
}
|
|
||||||
|
|
||||||
CommandFunc func(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error
|
|
||||||
|
|
||||||
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
|
MessageHandler func(s *discordgo.Session, m *discordgo.MessageCreate)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -44,110 +32,55 @@ func init() {
|
|||||||
viper.BindPFlags(pflag.CommandLine)
|
viper.BindPFlags(pflag.CommandLine)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBot(config *config.Config, s *discordgo.Session) *Bot {
|
func NewBot(s *discordgo.Session, config Config) *Bot {
|
||||||
return &Bot{
|
return &Bot{Session: s, Config: config}
|
||||||
session: s,
|
|
||||||
commands: make(map[string]*Command),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) AddHandler(handler interface{}) func() {
|
func (b *Bot) RegisterCommands() {
|
||||||
return b.session.AddHandler(handler)
|
AddCommand(&Command{
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) AddCommand(cmd *Command) {
|
|
||||||
b.commands[cmd.Name] = cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) GetCommand(name string) (*Command, bool) {
|
|
||||||
cmd, ok := b.commands[name]
|
|
||||||
return cmd, ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) CommandHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
|
||||||
if m.Author.ID == s.State.User.ID {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if !lib.HasCommand(m.Content, b.config.Prefix) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.config.Prefix)
|
|
||||||
|
|
||||||
cmd, ok := b.GetCommand(cmdName)
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
args := lib.SplitArgs(arg, cmd.NArgs)
|
|
||||||
|
|
||||||
if ok {
|
|
||||||
log.Debugf("command: %v, args: %v, nargs: %d", cmd.Name, args, len(args))
|
|
||||||
if err := cmd.Func(args, s, m); err != nil {
|
|
||||||
log.Errorf("failed to execute command: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Warnf("unknown command: %v, args: %v, nargs: %d", cmdName, args, len(args))
|
|
||||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (b *Bot) Init(h *handler.Handlers, ch *command.Handlers) {
|
|
||||||
// Register handlers
|
|
||||||
b.AddHandler(h.Reaction)
|
|
||||||
|
|
||||||
// Register commands
|
|
||||||
b.AddCommand(&Command{
|
|
||||||
Name: "coin",
|
Name: "coin",
|
||||||
Func: ch.Coin,
|
Func: b.CoinCommand(),
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "deal",
|
Name: "deal",
|
||||||
Func: ch.Deal,
|
Func: b.DealCommand(),
|
||||||
NArgs: 1,
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "ping",
|
Name: "ping",
|
||||||
Func: ch.Ping,
|
Func: b.PingCommand(),
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "roll",
|
Name: "roll",
|
||||||
Func: ch.Roll,
|
Func: b.RollCommand(),
|
||||||
NArgs: 1,
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "roulette",
|
|
||||||
Func: ch.Roulette,
|
|
||||||
})
|
|
||||||
b.AddCommand(&Command{
|
|
||||||
Name: "rps",
|
Name: "rps",
|
||||||
Func: ch.Rps,
|
Func: b.RpsCommand(),
|
||||||
NArgs: 1,
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "rpsls",
|
|
||||||
Func: ch.Rpsls,
|
|
||||||
NArgs: 1,
|
|
||||||
})
|
|
||||||
b.AddCommand(&Command{
|
|
||||||
Name: "time",
|
Name: "time",
|
||||||
Func: ch.Time,
|
Func: b.TimeCommand(),
|
||||||
NArgs: 1,
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "version",
|
Name: "version",
|
||||||
Func: ch.Version,
|
Func: b.VersionCommand(),
|
||||||
})
|
})
|
||||||
b.AddCommand(&Command{
|
AddCommand(&Command{
|
||||||
Name: "weather",
|
Name: "weather",
|
||||||
Func: ch.Weather,
|
Func: b.WeatherCommand(),
|
||||||
NArgs: 1,
|
NArgs: 1,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *Bot) RegisterHandlers() {
|
||||||
|
b.Session.AddHandler(b.CommandHandler())
|
||||||
|
b.Session.AddHandler(b.ReactionHandler())
|
||||||
|
}
|
||||||
|
|
||||||
func Run() error {
|
func Run() error {
|
||||||
initConfig()
|
initConfig()
|
||||||
go reloadConfig()
|
go reloadConfig()
|
||||||
@ -165,8 +98,9 @@ func Run() error {
|
|||||||
return fmt.Errorf("error creating discord session: %v", err)
|
return fmt.Errorf("error creating discord session: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b := NewBot(C, dg)
|
b := NewBot(dg, C)
|
||||||
b.Init(handler.NewHandlers(C), command.NewHandlers(C))
|
b.RegisterHandlers()
|
||||||
|
b.RegisterCommands()
|
||||||
|
|
||||||
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
dg.Identify.Intents = discordgo.IntentsGuildMessages | discordgo.IntentsDirectMessages
|
||||||
|
|
||||||
@ -188,7 +122,7 @@ func Run() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
C = config.NewConfig()
|
C = NewConfig()
|
||||||
|
|
||||||
viper.SetEnvPrefix("BEEPBOOP")
|
viper.SetEnvPrefix("BEEPBOOP")
|
||||||
viper.AutomaticEnv()
|
viper.AutomaticEnv()
|
||||||
|
31
bot/coin.go
Normal file
31
bot/coin.go
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
90
bot/command.go
Normal file
90
bot/command.go
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
DefaultCommander *Commander
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Commander struct {
|
||||||
|
commands map[string]*Command
|
||||||
|
}
|
||||||
|
|
||||||
|
Command struct {
|
||||||
|
Name string
|
||||||
|
Config Config
|
||||||
|
Func CommandFunc
|
||||||
|
NArgs int
|
||||||
|
}
|
||||||
|
|
||||||
|
CommandFunc func(args []string, m *discordgo.MessageCreate) error
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
DefaultCommander = NewCommander()
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCommander() *Commander {
|
||||||
|
cmdr := new(Commander)
|
||||||
|
cmdr.commands = make(map[string]*Command)
|
||||||
|
return cmdr
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cmdr *Commander) AddCommand(cmd *Command) {
|
||||||
|
cmdr.commands[cmd.Name] = cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cmdr *Commander) GetCommand(name string) (*Command, bool) {
|
||||||
|
cmd, ok := cmdr.commands[name]
|
||||||
|
return cmd, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func AddCommand(cmd *Command) {
|
||||||
|
DefaultCommander.AddCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetCommand(name string) (*Command, bool) {
|
||||||
|
cmd, ok := DefaultCommander.GetCommand(name)
|
||||||
|
return cmd, ok
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Bot) CommandHandler() func(*discordgo.Session, *discordgo.MessageCreate) {
|
||||||
|
return func(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||||
|
var cmd *Command
|
||||||
|
|
||||||
|
if m.Author.ID == s.State.User.ID {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if !lib.HasCommand(m.Content, b.Config.Prefix) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdName, arg := lib.SplitCommandAndArg(m.Content, b.Config.Prefix)
|
||||||
|
|
||||||
|
cmd, ok := GetCommand(cmdName)
|
||||||
|
|
||||||
|
args := lib.SplitArgs(arg, cmd.NArgs)
|
||||||
|
|
||||||
|
if ok {
|
||||||
|
cmd.Config = b.Config
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Warnf("unknown command: %v, args: %v, nargs: %d", cmdName, args, len(args))
|
||||||
|
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("unknown command: %s", cmdName))
|
||||||
|
}
|
||||||
|
}
|
40
bot/config.go
Normal file
40
bot/config.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
const (
|
||||||
|
defaultPrefix = "!"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultReactions []string = []string{
|
||||||
|
"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩",
|
||||||
|
"🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
Config struct {
|
||||||
|
Debug bool `mapstructure:"debug"`
|
||||||
|
Handler HandlerConfig `mapstructure:"handler"`
|
||||||
|
Prefix string `mapstructure:"prefix"`
|
||||||
|
DiscordToken string `mapstructure:"discord_token"`
|
||||||
|
OpenWeatherMapToken string `mapstructure:"open_weather_map_token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
HandlerConfig struct {
|
||||||
|
Reaction ReactionConfig `mapstructure:"reaction"`
|
||||||
|
}
|
||||||
|
|
||||||
|
ReactionConfig struct {
|
||||||
|
Emojis []string
|
||||||
|
Channels []string
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func NewConfig() Config {
|
||||||
|
var c Config
|
||||||
|
|
||||||
|
c.Prefix = defaultPrefix
|
||||||
|
c.Handler.Reaction.Emojis = defaultReactions
|
||||||
|
|
||||||
|
return c
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -45,31 +45,33 @@ func (d *Deck) Deal(n int) ([]Card, error) {
|
|||||||
return hand, err
|
return hand, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) Deal(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
func (b *Bot) DealCommand() CommandFunc {
|
||||||
rand.Shuffle(len(deck), func(i, j int) {
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
deck[i], deck[j] = deck[j], deck[i]
|
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 {
|
if len(args) != 1 {
|
||||||
s.ChannelMessageSend(m.ChannelID, "help: `!deal <n>`")
|
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, " "))
|
||||||
return nil
|
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 {
|
|
||||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("error: %s\n", err))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, JoinCards(hand, " "))
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func JoinCards(h []Card, sep string) string {
|
func JoinCards(h []Card, sep string) string {
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -78,26 +78,28 @@ func (r *Roll) RollDice() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) Roll(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
func (b *Bot) RollCommand() CommandFunc {
|
||||||
var (
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
err error
|
var (
|
||||||
msg, roll string
|
err error
|
||||||
r *Roll
|
msg, roll string
|
||||||
)
|
r *Roll
|
||||||
|
)
|
||||||
|
|
||||||
roll = args[0]
|
roll = args[0]
|
||||||
|
|
||||||
r, err = ParseRoll(roll)
|
r, err = ParseRoll(roll)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.ChannelMessageSend(m.ChannelID, err.Error())
|
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)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
r.RollDice()
|
|
||||||
log.Debugf("rolled dice: %+v", r)
|
|
||||||
|
|
||||||
msg = fmt.Sprintf("🎲 %s = %d", lib.JoinInt(r.Rolls, " + "), r.Sum)
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, msg)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
12
bot/ping.go
Normal file
12
bot/ping.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
52
bot/reaction.go
Normal file
52
bot/reaction.go
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package command
|
package bot
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
@ -66,17 +66,19 @@ func (g *Gun) IsEmpty() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handlers) Roulette(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
func (b *Bot) RouletteCommand() CommandFunc {
|
||||||
if gun.IsEmpty() {
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
gun.Load(Bullets)
|
if gun.IsEmpty() {
|
||||||
log.Debugf("reloading gun: %+v\n", gun)
|
gun.Load(Bullets)
|
||||||
}
|
log.Debugf("reloading gun: %+v\n", gun)
|
||||||
|
}
|
||||||
|
|
||||||
log.Debugf("firing gun: %+v\n", gun)
|
log.Debugf("firing gun: %+v\n", gun)
|
||||||
if gun.Fire() {
|
if gun.Fire() {
|
||||||
s.ChannelMessageSend(m.ChannelID, GunFireMessage)
|
b.Session.ChannelMessageSend(m.ChannelID, GunFireMessage)
|
||||||
} else {
|
} else {
|
||||||
s.ChannelMessageSend(m.ChannelID, GunClickMessage)
|
b.Session.ChannelMessageSend(m.ChannelID, GunClickMessage)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
84
bot/rps.go
Normal file
84
bot/rps.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
Rock = iota
|
||||||
|
Paper
|
||||||
|
Scissors
|
||||||
|
Lizard
|
||||||
|
Spock
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rpsVictoryMap map[int][]int = map[int][]int{
|
||||||
|
Rock: {Scissors},
|
||||||
|
Paper: {Rock},
|
||||||
|
Scissors: {Paper},
|
||||||
|
}
|
||||||
|
|
||||||
|
rpsChoiceMap map[string]int = map[string]int{
|
||||||
|
"rock": Rock,
|
||||||
|
"paper": Paper,
|
||||||
|
"scissors": Scissors,
|
||||||
|
}
|
||||||
|
|
||||||
|
rpsEmojiMap map[int]string = map[int]string{
|
||||||
|
Rock: "🪨️",
|
||||||
|
Paper: "📝",
|
||||||
|
Scissors: "✂️",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Bot) RpsCommand() CommandFunc {
|
||||||
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
|
var (
|
||||||
|
bc, pc int
|
||||||
|
be, pe string
|
||||||
|
c string
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(args) != 1 {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c = strings.ToLower(args[0])
|
||||||
|
|
||||||
|
pc, ok := rpsChoiceMap[c] // player's choice
|
||||||
|
if !ok {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
bc = lib.MapRand(rpsChoiceMap) // bot's choice
|
||||||
|
pe = rpsEmojiMap[pc] // player's emoji
|
||||||
|
be = rpsEmojiMap[bc] // bot's emoji
|
||||||
|
|
||||||
|
if bc == pc {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: draw", be, pe,
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
} else if lib.Contains(rpsVictoryMap[bc], pc) {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, bc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpsChoiceMap, pc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
82
bot/rpsls.go
Normal file
82
bot/rpsls.go
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
package bot
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.kill0.net/chill9/beepboop/lib"
|
||||||
|
"github.com/bwmarrin/discordgo"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
rpslsVictoryMap map[int][]int = map[int][]int{
|
||||||
|
Rock: {Scissors, Lizard},
|
||||||
|
Paper: {Rock, Spock},
|
||||||
|
Scissors: {Paper, Lizard},
|
||||||
|
Lizard: {Paper, Spock},
|
||||||
|
Spock: {Scissors, Rock},
|
||||||
|
}
|
||||||
|
|
||||||
|
rpslsChoiceMap map[string]int = map[string]int{
|
||||||
|
"rock": Rock,
|
||||||
|
"paper": Paper,
|
||||||
|
"scissors": Scissors,
|
||||||
|
"lizard": Lizard,
|
||||||
|
"spock": Spock,
|
||||||
|
}
|
||||||
|
|
||||||
|
rpslsEmojiMap map[int]string = map[int]string{
|
||||||
|
Rock: "🪨️",
|
||||||
|
Paper: "📝",
|
||||||
|
Scissors: "✂️",
|
||||||
|
Lizard: "🦎",
|
||||||
|
Spock: "🖖",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (b *Bot) RpslsCommand() CommandFunc {
|
||||||
|
return func(args []string, m *discordgo.MessageCreate) error {
|
||||||
|
var (
|
||||||
|
bc, pc int
|
||||||
|
be, pe string
|
||||||
|
c string
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(args) != 1 {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock )`",
|
||||||
|
)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c = strings.ToLower(args[0])
|
||||||
|
|
||||||
|
pc, ok := rpslsChoiceMap[c] // player's choice
|
||||||
|
if !ok {
|
||||||
|
b.Session.ChannelMessageSend(
|
||||||
|
m.ChannelID, "help: `!rpsls (rock | paper | scissors | lizard | spock)`",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
bc = lib.MapRand(rpslsChoiceMap) // bot's choice
|
||||||
|
pe = rpslsEmojiMap[pc] // player's emoji
|
||||||
|
be = rpslsEmojiMap[bc] // bot's emoji
|
||||||
|
|
||||||
|
if bc == pc {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: draw", be, pe,
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
} else if lib.Contains(rpslsVictoryMap[bc], pc) {
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, bc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
b.Session.ChannelMessageSend(m.ChannelID, fmt.Sprintf(
|
||||||
|
"%s v %s: %s wins", be, pe, lib.MapKey(rpslsChoiceMap, pc),
|
||||||
|
))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
36
bot/time.go
Normal file
36
bot/time.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
25
bot/version.go
Normal file
25
bot/version.go
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
56
bot/weather.go
Normal file
56
bot/weather.go
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
@ -1,29 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
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 (h *Handlers) Coin(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
||||||
var (
|
|
||||||
c Coin
|
|
||||||
msg string
|
|
||||||
)
|
|
||||||
|
|
||||||
if c.Flip() {
|
|
||||||
msg = "heads"
|
|
||||||
} else {
|
|
||||||
msg = "tails"
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, msg)
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import "git.kill0.net/chill9/beepboop/config"
|
|
||||||
|
|
||||||
type Handlers struct {
|
|
||||||
config *config.Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandlers(config *config.Config) *Handlers {
|
|
||||||
return &Handlers{config: config}
|
|
||||||
}
|
|
@ -1,10 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Ping(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
||||||
s.ChannelMessageSend(m.ChannelID, "pong")
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib/rps"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Rps(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
||||||
if len(args) != 1 {
|
|
||||||
s.ChannelMessageSend(
|
|
||||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pc := strings.ToLower(args[0]) // player's choice
|
|
||||||
|
|
||||||
g := rps.NewGame(rps.RulesRps, rps.EmojiMapRps)
|
|
||||||
|
|
||||||
bc := g.Rand() // bot's choice
|
|
||||||
|
|
||||||
out, err := g.Play(bc, pc)
|
|
||||||
if _, ok := err.(rps.InvalidChoiceError); ok {
|
|
||||||
s.ChannelMessageSend(
|
|
||||||
m.ChannelID, "help: `!rps (rock | paper | scissors)`",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, out)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib/rps"
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Rpsls(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
||||||
if len(args) != 1 {
|
|
||||||
s.ChannelMessageSend(
|
|
||||||
m.ChannelID, "help: `!rps (rock | paper | scissors | lizard | spock)`",
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
pc := strings.ToLower(args[0]) // player's choice
|
|
||||||
|
|
||||||
g := rps.NewGame(rps.RulesRpsls, rps.EmojiMapRpsls)
|
|
||||||
|
|
||||||
bc := g.Rand() // bot's choice
|
|
||||||
|
|
||||||
out, err := g.Play(bc, pc)
|
|
||||||
if _, ok := err.(rps.InvalidChoiceError); ok {
|
|
||||||
s.ChannelMessageSend(
|
|
||||||
m.ChannelID, "help: `!rps (rock | paper | scissors | lizard | spock)`",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, out)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,34 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Time(args []string, s *discordgo.Session, 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)
|
|
||||||
s.ChannelMessageSend(m.ChannelID, err.Error())
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
t = now.In(loc)
|
|
||||||
} else {
|
|
||||||
t = now
|
|
||||||
}
|
|
||||||
|
|
||||||
s.ChannelMessageSend(m.ChannelID, fmt.Sprint(t))
|
|
||||||
return nil
|
|
||||||
}
|
|
@ -1,23 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
)
|
|
||||||
|
|
||||||
const (
|
|
||||||
SourceURI = "https://git.kill0.net/chill9/bb"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Version(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
||||||
s.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,54 +0,0 @@
|
|||||||
package command
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib/weather"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Weather(args []string, s *discordgo.Session, m *discordgo.MessageCreate) error {
|
|
||||||
var (
|
|
||||||
err error
|
|
||||||
loc string
|
|
||||||
w weather.Weather
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(args) != 1 {
|
|
||||||
s.ChannelMessageSend(m.ChannelID, "help: `!weather <CITY>,<STATE>,<COUNTRY>`")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
loc = args[0]
|
|
||||||
|
|
||||||
if h.config.OpenWeatherMapToken == "" {
|
|
||||||
log.Error("OpenWeather token is not set")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
wc := weather.NewClient(h.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)
|
|
||||||
|
|
||||||
s.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
|
|
||||||
}
|
|
@ -1,58 +0,0 @@
|
|||||||
package config
|
|
||||||
|
|
||||||
const (
|
|
||||||
defaultPrefix = "!"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
defaultReactions []string = []string{
|
|
||||||
"👍", "🌶️", "🤣", "😂", "🍆", "🍑", "❤️", "💦", "😍", "💩",
|
|
||||||
"🔥", "🍒", "🎉", "🥳", "🎊", "📉", "📈", "💀", "☠️",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
Config struct {
|
|
||||||
Debug bool `mapstructure:"debug"`
|
|
||||||
Handler HandlerConfig `mapstructure:"handler"`
|
|
||||||
Prefix string `mapstructure:"prefix"`
|
|
||||||
DiscordToken string `mapstructure:"discord_token"`
|
|
||||||
OpenWeatherMapToken string `mapstructure:"open_weather_map_token"`
|
|
||||||
Mongo MongoConfig `mapstructure:"mongo"`
|
|
||||||
Redis RedisConfig `mapstructure:"redis"`
|
|
||||||
Postgres PostgresConfig `mapstructure:"postgres"`
|
|
||||||
}
|
|
||||||
|
|
||||||
HandlerConfig struct {
|
|
||||||
Reaction ReactionConfig `mapstructure:"reaction"`
|
|
||||||
}
|
|
||||||
|
|
||||||
ReactionConfig struct {
|
|
||||||
Emojis []string
|
|
||||||
Channels []string
|
|
||||||
}
|
|
||||||
|
|
||||||
MongoConfig struct {
|
|
||||||
Uri string `mapstructure:"uri"`
|
|
||||||
Database string `mapstructure:"database"`
|
|
||||||
}
|
|
||||||
|
|
||||||
RedisConfig struct {
|
|
||||||
Addr string `mapstructure:"addr"`
|
|
||||||
Password string `mapstructure:"password"`
|
|
||||||
DB int `mapstructure:"database"`
|
|
||||||
}
|
|
||||||
|
|
||||||
PostgresConfig struct {
|
|
||||||
Uri string `mapstructure:"uri"`
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewConfig() *Config {
|
|
||||||
var c *Config = &Config{}
|
|
||||||
|
|
||||||
c.Prefix = defaultPrefix
|
|
||||||
c.Handler.Reaction.Emojis = defaultReactions
|
|
||||||
|
|
||||||
return c
|
|
||||||
}
|
|
@ -1,15 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"git.kill0.net/chill9/beepboop/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Handlers struct {
|
|
||||||
config *config.Config
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewHandlers(config *config.Config) *Handlers {
|
|
||||||
return &Handlers{
|
|
||||||
config: config,
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,50 +0,0 @@
|
|||||||
package handler
|
|
||||||
|
|
||||||
import (
|
|
||||||
"math/rand"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
|
||||||
|
|
||||||
"github.com/bwmarrin/discordgo"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (h *Handlers) Reaction(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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -49,9 +49,3 @@ func MapRand[K comparable, V any](m map[K]V) V {
|
|||||||
}
|
}
|
||||||
panic("unreachable")
|
panic("unreachable")
|
||||||
}
|
}
|
||||||
|
|
||||||
func MapRandKey[K comparable, V any](m map[K]V) K {
|
|
||||||
keys := MapKeys(m)
|
|
||||||
n := rand.Intn(len(m))
|
|
||||||
return keys[n]
|
|
||||||
}
|
|
||||||
|
118
lib/rps/rps.go
118
lib/rps/rps.go
@ -1,118 +0,0 @@
|
|||||||
package rps
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"git.kill0.net/chill9/beepboop/lib"
|
|
||||||
)
|
|
||||||
|
|
||||||
type (
|
|
||||||
Game struct {
|
|
||||||
rules [][]string
|
|
||||||
emojiMap map[string]string
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
type InvalidChoiceError struct {
|
|
||||||
s string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (e InvalidChoiceError) Error() string {
|
|
||||||
return fmt.Sprintf("%q is an invalid choice", e.s)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
|
||||||
RulesRps [][]string = [][]string{
|
|
||||||
{"rock", "scissors", "crushes"},
|
|
||||||
{"paper", "rock", "covers"},
|
|
||||||
{"scissors", "paper", "cuts"},
|
|
||||||
}
|
|
||||||
|
|
||||||
EmojiMapRps map[string]string = map[string]string{
|
|
||||||
"rock": "🪨️",
|
|
||||||
"paper": "📝",
|
|
||||||
"scissors": "✂️",
|
|
||||||
}
|
|
||||||
|
|
||||||
RulesRpsls [][]string = [][]string{
|
|
||||||
{"rock", "scissors", "crushes"},
|
|
||||||
{"rock", "lizard", "crushes"},
|
|
||||||
{"paper", "rock", "covers"},
|
|
||||||
{"paper", "spock", "disproves"},
|
|
||||||
{"scissors", "paper", "cuts"},
|
|
||||||
{"scissors", "lizard", "decapitates"},
|
|
||||||
{"lizard", "paper", "eats"},
|
|
||||||
{"lizard", "spock", "poisons"},
|
|
||||||
{"spock", "scissors", "smashes"},
|
|
||||||
{"spock", "rock", "vaporizes"},
|
|
||||||
}
|
|
||||||
|
|
||||||
EmojiMapRpsls map[string]string = map[string]string{
|
|
||||||
"rock": "🪨️",
|
|
||||||
"paper": "📝",
|
|
||||||
"scissors": "✂️",
|
|
||||||
"lizard": "🦎",
|
|
||||||
"spock": "🖖",
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
func NewGame(rules [][]string, emojiMap map[string]string) *Game {
|
|
||||||
return &Game{rules: rules, emojiMap: emojiMap}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Game) Rand() string {
|
|
||||||
return lib.MapRandKey(g.emojiMap)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Game) Play(c1, c2 string) (string, error) {
|
|
||||||
var b strings.Builder
|
|
||||||
|
|
||||||
if !g.Valid(c1) {
|
|
||||||
return "", InvalidChoiceError{s: c1}
|
|
||||||
}
|
|
||||||
if !g.Valid(c2) {
|
|
||||||
return "", InvalidChoiceError{s: c2}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(&b, "%s v %s: ", g.emojiMap[c1], g.emojiMap[c2])
|
|
||||||
|
|
||||||
for _, rule := range g.rules {
|
|
||||||
verb := rule[2]
|
|
||||||
|
|
||||||
if c1 == c2 {
|
|
||||||
fmt.Fprintf(&b, "draw")
|
|
||||||
return b.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if c1 == rule[0] && c2 == rule[1] {
|
|
||||||
fmt.Fprintf(&b, "%s %s %s", c1, verb, c2)
|
|
||||||
return b.String(), nil
|
|
||||||
} else if c2 == rule[0] && c1 == rule[1] {
|
|
||||||
fmt.Fprintf(&b, "%s %s %s", c2, verb, c1)
|
|
||||||
return b.String(), nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return b.String(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *Game) Valid(c string) bool {
|
|
||||||
_, ok := g.emojiMap[c]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func Play(rules [][]string, c1, c2 string) string {
|
|
||||||
for _, rule := range rules {
|
|
||||||
if c1 == c2 {
|
|
||||||
return "draw"
|
|
||||||
}
|
|
||||||
|
|
||||||
if c1 == rule[0] && c2 == rule[1] {
|
|
||||||
return fmt.Sprintf("%s %s %s", c1, rule[2], c2)
|
|
||||||
} else if c2 == rule[0] && c1 == rule[1] {
|
|
||||||
return fmt.Sprintf("%s %s %s", c2, rule[2], c1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
Reference in New Issue
Block a user