Skip to content

Instantly share code, notes, and snippets.

@ducc
Created May 21, 2020 18:39
Show Gist options
  • Save ducc/7759414ba6d5fb58b1886f1663ed21b9 to your computer and use it in GitHub Desktop.
Save ducc/7759414ba6d5fb58b1886f1663ed21b9 to your computer and use it in GitHub Desktop.
package main
import (
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/sirupsen/logrus"
"strconv"
"strings"
)
var (
level string
token string
admin string
shardCount int
minShard int
numShards int
)
func init() {
flag.StringVar(&level, "level", "debug", "")
flag.StringVar(&token, "token", "", "")
flag.StringVar(&admin, "admin", "", "")
flag.IntVar(&shardCount, "shard-count", 0, "")
flag.IntVar(&minShard, "min-shard", 0, "")
flag.IntVar(&numShards, "num-shards", 0, "")
}
func main() {
flag.Parse()
if ll, err := logrus.ParseLevel(level); err != nil {
logrus.WithError(err).Fatal("parsing log elvel")
} else {
logrus.SetLevel(ll)
}
for i := minShard; i < minShard + numShards; i++ {
log := logrus.WithField("shard_id", i)
log.Info("starting shard")
if err := handleCommand(nil, StartShard, map[string]interface{}{
"shard_id": i,
}); err != nil {
logrus.WithError(err).Error("starting shard")
}
}
<-make(chan struct{}, 0)
}
var sessions = make([]*discordgo.Session, 0)
type Command int
const (
StartShard Command = iota
ParseMessage
SendMessage
)
func handleCommand(s *discordgo.Session, command Command, data map[string]interface{}) error {
switch command {
case StartShard:
session, err := discordgo.New("Bot " + token)
if err != nil {
return err
}
session.ShardCount = shardCount
session.ShardID = data["shard_id"].(int)
session.AddHandler(messageCreate)
if err := session.Open(); err != nil {
return err
}
sessions = append(sessions, session)
case ParseMessage:
content := data["content"].(string)
if strings.HasPrefix(content, "!!!shard") {
splits := strings.Split(content, " ")
if len(splits) < 2 {
return handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "BAD FORMAT HOT DOG",
})
}
guildID, err := strconv.ParseInt(splits[1], 10, 64)
if err != nil {
return handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "BAD GUILD ID HOT DOG",
})
}
shardID := (guildID >> 22) % int64(shardCount)
return handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "HOT DOG " + fmt.Sprint(shardID),
})
}
if strings.HasPrefix(content, "!!!start") && data["author"] == admin {
splits := strings.Split(content, " ")
if len(splits) < 2 {
return handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "BAD FORMAT HOT DOG",
})
}
shardID, err := strconv.ParseInt(splits[1], 10, 64)
if err != nil {
return handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "BAD SHARD ID HOT DOG",
})
}
if err := handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "MORE HOTDOG COMING",
}); err != nil {
return err
}
if err := handleCommand(s, StartShard, map[string]interface{}{
"shard_id": int(shardID),
}); err != nil {
return err
}
if err := handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "MORE HOTDOG came",
}); err != nil {
return err
}
}
if strings.HasPrefix(content, "!!!") && len(content) > 3 {
return handleCommand(s, SendMessage, map[string]interface{}{
"channel": data["channel"],
"content": "where is my hot dog egg?",
})
}
case SendMessage:
_, err := s.ChannelMessageSend(data["channel"].(string), data["content"].(string))
return err
}
return nil
}
func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) {
if err := handleCommand(s, ParseMessage, map[string]interface{}{
"channel": m.ChannelID,
"content": m.Content,
"author": m.Author.ID,
}); err != nil {
logrus.WithError(err).Error(err)
}
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment