Initial commit.

This commit is contained in:
2020-07-16 06:24:19 +00:00
commit ec780df92f
15 changed files with 735 additions and 0 deletions

68
lib/discord/dispatcher.go Normal file
View File

@ -0,0 +1,68 @@
package discord
// Dispatcher is designed specifically to ease handling of text commands
// of the form "/command options"
// For other event types, you'll need the full Discord session,
// exported as the variable `Session`.
import (
"fmt"
"strings"
"github.com/bwmarrin/discordgo"
)
type DiscordMessage func(*discordgo.Session, *discordgo.MessageCreate)
var handlers map[string]DiscordMessage
var helpHandlers map[string]DiscordMessage
// TODO: is this load-order dependent?
func init() {
handlers = make(map[string]DiscordMessage)
helpHandlers = make(map[string]DiscordMessage)
handlers["/help"] = helpDispatcher
}
func RegisterHandler(command string, handler DiscordMessage, helpHandler DiscordMessage) {
handlers[command] = handler
helpHandlers[command[1:]] = helpHandler
}
func dispatcher(session *discordgo.Session, msg *discordgo.MessageCreate) {
command := strings.Split(msg.Content, " ")[0]
if msg.Author.ID == session.State.User.ID {
return
}
if handler, ok := handlers[command]; ok {
handler(session, msg)
}
}
func helpDispatcher(session *discordgo.Session, msg *discordgo.MessageCreate) {
data := strings.Split(msg.Content, " ")[1:]
if len(data) == 0 {
printCommandList(session, msg)
return
}
command := data[0]
if handler, ok := helpHandlers[command]; ok {
handler(session, msg)
}
}
func printCommandList(session *discordgo.Session, msg *discordgo.MessageCreate) {
var output strings.Builder
output.WriteString("Here are all the commands I know about:\n\n")
for command, _ := range helpHandlers {
output.WriteString(fmt.Sprintf(" %s\n", command))
}
output.WriteString("\nType `/help <command>` For more information about a particular command.")
session.ChannelMessageSend(msg.ChannelID, output.String())
}

39
lib/discord/server.go Normal file
View File

@ -0,0 +1,39 @@
// Discord bot infrastructure.
package discord
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/bwmarrin/discordgo"
)
func InitServer(auth_token string) *discordgo.Session {
session, err := discordgo.New("Bot " + auth_token)
if err != nil {
log.Fatal(err.Error())
}
defer session.Close()
// Register handler
session.AddHandler(dispatcher)
err = session.Open()
if err != nil {
log.Fatal(err.Error())
}
return session
}
func RunServer() {
log.Println("Bot is now running. Press CTRL-C to exit.")
sig_chan := make(chan os.Signal, 1)
signal.Notify(sig_chan, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sig_chan
}

34
lib/util/file.go Normal file
View File

@ -0,0 +1,34 @@
package util
import (
"io/ioutil"
"log"
"path/filepath"
)
type ParseFunc func(data []byte) int
func ParseFiles(dir string, parser ParseFunc) {
files, err := ioutil.ReadDir(dir)
if err != nil {
log.Print(err.Error())
return
}
for _, file := range files {
if filepath.Ext(file.Name()) == ".yml" || filepath.Ext(file.Name()) == ".yaml" {
ParseFile(dir+file.Name(), parser)
}
}
}
func ParseFile(fileName string, parser ParseFunc) {
data, err := ioutil.ReadFile(fileName)
if err != nil {
log.Print(err.Error())
return
}
count := parser(data)
log.Printf("util.parseFile: Added %d new entries from %s", count, fileName)
}