discord-pocketbot-go/pocketbot.go

107 lines
2.9 KiB
Go
Raw Normal View History

2019-08-05 22:33:07 +00:00
package main
import (
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/bwmarrin/disgord/x/mux"
"io/ioutil"
2019-08-05 22:33:07 +00:00
"log"
"math/rand"
2019-08-05 22:33:07 +00:00
"os"
"os/signal"
"syscall"
"time"
2019-08-05 22:33:07 +00:00
)
var Session, _ = discordgo.New()
var bot_token string
var bot_channel string
var bot_admin_role string
2019-08-05 22:33:07 +00:00
var Router = mux.New()
func init() {
rand.Seed(time.Now().UnixNano())
2019-08-05 22:33:07 +00:00
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")
}
2019-08-05 22:33:07 +00:00
}
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)
2019-08-06 00:57:23 +00:00
//Set the command prefix
if os.Getenv("POCKETBOT_PREFIX") != "" {
Router.Prefix = os.Getenv("POCKETBOT_PREFIX")
} else {
Router.Prefix = "!"
}
2019-08-05 22:33:07 +00:00
// Register the build-in help command.
Router.Route("help", "Display this message.", Router.Help)
2019-08-06 00:49:23 +00:00
Router.Route("swear", "Make me swear!", getSwear)
2019-08-06 01:05:30 +00:00
Router.Route("maki", "It's Maki Monday my dudes!", makiMonday)
Router.Route("monday", "It's Maki Monday my dudes!", makiMonday)
2019-08-06 01:10:18 +00:00
Router.Route("mixes", "Post the link to my fire mixtapes!", postMixes)
2019-08-07 02:51:53 +00:00
Router.Route("rmix", "Post a random mix!", getRandomMix)
2019-08-06 23:59:18 +00:00
Router.Route("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD)
2019-08-07 01:45:09 +00:00
Router.Route("restart", "Restart the bot.", restartBot)
2019-08-07 01:49:49 +00:00
Router.Route("kill", "Restart the bot.", restartBot)
2020-04-26 07:19:29 +00:00
Router.Route("sleep", "Something about sleeping...", goToSleep)
2020-04-26 08:45:16 +00:00
Router.Route("setplaying", "Set the nowplaying message for the bot.", setNowPlaying)
2020-05-01 05:46:47 +00:00
Router.Route("roll", "Roll some dice!", rollDice)
2020-05-21 01:40:22 +00:00
Router.Route("conch", "Ask the Magic Conch™ shell to predict the future!", askConch)
2020-05-01 05:48:09 +00:00
Router.Route("uwu", "Say UwU", sayUwU)
2019-08-05 22:33:07 +00:00
//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...")
}
2019-08-05 22:33:07 +00:00
}