discord-pocketbot-go/pocketbot.go

107 lines
2.9 KiB
Go

package main
import (
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/disgord/x/mux"
"io/ioutil"
"log"
"math/rand"
"os"
"os/signal"
"syscall"
"time"
)
var Session, _ = discordgo.New()
var bot_token string
var bot_channel string
var bot_admin_role string
var Router = mux.New()
func init() {
rand.Seed(time.Now().UnixNano())
flag.StringVar(&bot_token, "t", "", "Discord Authentication Token")
flag.StringVar(&bot_channel, "bc", "", "Bot status channel")
flag.Parse()
if os.Getenv("DISCORD_TOKEN") != "" {
Session.Token = "Bot " + os.Getenv("DISCORD_TOKEN")
} else {
Session.Token = "Bot " + bot_token
}
if os.Getenv("DISCORD_BOT_CHANNEL") != "" {
bot_channel = os.Getenv("DISCORD_BOT_CHANNEL")
}
if os.Getenv("POCKETBOT_ADMIN_ROLE") != "" {
bot_admin_role = os.Getenv("POCKETBOT_ADMIN_ROLE")
}
}
func main() {
var err error
fmt.Println("pocketbot")
//flag.Parse()
if Session.Token == "" {
log.Println("You must provide a Discord auth token!")
return
}
//Handlers
Session.AddHandler(ready)
Session.AddHandler(Router.OnMessageCreate)
//Set the command prefix
if os.Getenv("POCKETBOT_PREFIX") != "" {
Router.Prefix = os.Getenv("POCKETBOT_PREFIX")
} else {
Router.Prefix = "!"
}
// Register the build-in help command.
Router.Route("help", "Display this message.", Router.Help)
Router.Route("swear", "Make me swear!", getSwear)
Router.Route("maki", "It's Maki Monday my dudes!", makiMonday)
Router.Route("monday", "It's Maki Monday my dudes!", makiMonday)
Router.Route("mixes", "Post the link to my fire mixtapes!", postMixes)
Router.Route("rmix", "Post a random mix!", getRandomMix)
Router.Route("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD)
Router.Route("restart", "Restart the bot.", restartBot)
Router.Route("kill", "Restart the bot.", restartBot)
Router.Route("sleep", "Something about sleeping...", goToSleep)
Router.Route("setplaying", "Set the nowplaying message for the bot.", setNowPlaying)
Router.Route("roll", "Roll some dice!", rollDice)
Router.Route("conch", "Ask the Magic Conch™ shell to predict the future!", askConch)
Router.Route("uwu", "Say UwU", sayUwU)
//Open a connection to Discord
err = Session.Open()
if err != nil {
log.Printf("Error opening connection to Discord, %s\n", err)
os.Exit(1)
}
//Wait for a CTRL-C
log.Printf("Now running. Press CTRL-C to exit.")
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
Session.Close()
}
func ready(s *discordgo.Session, event *discordgo.Ready) {
playingstatus := "with droids!"
_, err := os.Stat("userdata/nowplaying.txt")
if !os.IsNotExist(err) {
content, err := ioutil.ReadFile("userdata/nowplaying.txt")
if err != nil {
log.Fatal(err)
}
playingstatus = string(content)
}
s.UpdateStatus(0, playingstatus)
if os.Getenv("POCKETBOT_STARTUP_MESSAGE") == "TRUE" {
s.ChannelMessageSend(bot_channel, "This isn't Tatooine...")
}
}