From 79f1fc62cb92a7fd566f116294dd814e20413b6d Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Mon, 5 Aug 2019 18:33:07 -0400 Subject: [PATCH 01/25] Initial commit --- .gitignore | 26 ++++++++++++++++++++ pocketbot.go | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 .gitignore create mode 100644 pocketbot.go diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d3beee5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,26 @@ +# ---> Go +# Compiled Object files, Static and Dynamic libs (Shared Objects) +*.o +*.a +*.so + +# Folders +_obj +_test + +# Architecture specific extensions/prefixes +*.[568vq] +[568vq].out + +*.cgo1.go +*.cgo2.c +_cgo_defun.c +_cgo_gotypes.go +_cgo_export.* + +_testmain.go + +*.exe +*.test +*.prof + diff --git a/pocketbot.go b/pocketbot.go new file mode 100644 index 0000000..63170e2 --- /dev/null +++ b/pocketbot.go @@ -0,0 +1,67 @@ +package main + +import ( + "flag" + "fmt" + "github.com/bwmarrin/discordgo" + "github.com/bwmarrin/disgord/x/mux" + "log" + "os" + "os/signal" + "syscall" +) + +var Session, _ = discordgo.New() +var bot_token string +var bot_channel string +var Router = mux.New() + +func init() { + 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") + } +} + +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) + + // Register the build-in help command. + Router.Route("help", "Display this message.", Router.Help) + + //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) { + s.UpdateStatus(0, "with droids!") + s.ChannelMessageSend(bot_channel, "This isn't Tatooine...") +} From 05adff76d0d02c0d3798d5181407fbe7568a0e8b Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Mon, 5 Aug 2019 20:49:23 -0400 Subject: [PATCH 02/25] Add swear command --- commands.go | 27 +++++++++++++++++++++++++++ pocketbot.go | 1 + 2 files changed, 28 insertions(+) create mode 100644 commands.go diff --git a/commands.go b/commands.go new file mode 100644 index 0000000..2b97917 --- /dev/null +++ b/commands.go @@ -0,0 +1,27 @@ +package main + +import ( + "github.com/bwmarrin/discordgo" + "github.com/bwmarrin/disgord/x/mux" + "io/ioutil" + "net/http" +) + +//Generate a heckin swear word +func getSwear(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + resp := "" + swear, err := http.Get("https://swear.jawa.moe/") + if err != nil { + resp = "Error fetching swear: " + err.Error() + } + defer swear.Body.Close() + + if swear.StatusCode == http.StatusOK { + body, err := ioutil.ReadAll(swear.Body) + if err != nil { + resp = "Error fetching swear: " + err.Error() + } + resp = string(body) + } + s.ChannelMessageSend(m.ChannelID, resp) +} diff --git a/pocketbot.go b/pocketbot.go index 63170e2..f713ccd 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -45,6 +45,7 @@ func main() { // Register the build-in help command. Router.Route("help", "Display this message.", Router.Help) + Router.Route("swear", "Make me swear!", getSwear) //Open a connection to Discord err = Session.Open() From b9bc185650dfedf044ebf81d5685abbe6ee2a29f Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Mon, 5 Aug 2019 20:57:23 -0400 Subject: [PATCH 03/25] Set the command prefix --- pocketbot.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pocketbot.go b/pocketbot.go index f713ccd..fd0adec 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -43,6 +43,13 @@ func main() { 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) From 0c5d70da8ffcff63d56b8f13c4494b0dc2a66c60 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Mon, 5 Aug 2019 21:05:30 -0400 Subject: [PATCH 04/25] Add Maki Monday command --- commands.go | 6 ++++++ pocketbot.go | 2 ++ 2 files changed, 8 insertions(+) diff --git a/commands.go b/commands.go index 2b97917..a61c829 100644 --- a/commands.go +++ b/commands.go @@ -25,3 +25,9 @@ func getSwear(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { } s.ChannelMessageSend(m.ChannelID, resp) } + +//Post the Maki Monday pic! +func makiMonday(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + resp := "https://maki.jawa.moe/monday.png" + s.ChannelMessageSend(m.ChannelID, resp) +} diff --git a/pocketbot.go b/pocketbot.go index fd0adec..791e9bc 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -53,6 +53,8 @@ func main() { // 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) //Open a connection to Discord err = Session.Open() From 538db40037b572a7323258b8b84613ae2e0a4f20 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Mon, 5 Aug 2019 21:10:18 -0400 Subject: [PATCH 05/25] Add mixes command --- commands.go | 6 ++++++ pocketbot.go | 1 + 2 files changed, 7 insertions(+) diff --git a/commands.go b/commands.go index a61c829..7d9afb3 100644 --- a/commands.go +++ b/commands.go @@ -31,3 +31,9 @@ func makiMonday(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { resp := "https://maki.jawa.moe/monday.png" s.ChannelMessageSend(m.ChannelID, resp) } + +//Post link to mixes B) +func postMixes(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + resp := "https://maki.jawa.moe/mixes/" + s.ChannelMessageSend(m.ChannelID, resp) +} diff --git a/pocketbot.go b/pocketbot.go index 791e9bc..040386a 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -55,6 +55,7 @@ func main() { 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) //Open a connection to Discord err = Session.Open() From 491f28236c8f8f431b500903ef6ebabb33b65220 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 19:59:18 -0400 Subject: [PATCH 06/25] Add XKCD command --- commands.go | 31 +++++++++++++++++++++++++++++++ pocketbot.go | 1 + 2 files changed, 32 insertions(+) diff --git a/commands.go b/commands.go index 7d9afb3..175b48b 100644 --- a/commands.go +++ b/commands.go @@ -3,8 +3,11 @@ package main import ( "github.com/bwmarrin/discordgo" "github.com/bwmarrin/disgord/x/mux" + "github.com/nishanths/go-xkcd" "io/ioutil" "net/http" + "regexp" + "strconv" ) //Generate a heckin swear word @@ -37,3 +40,31 @@ func postMixes(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { resp := "https://maki.jawa.moe/mixes/" s.ChannelMessageSend(m.ChannelID, resp) } + +//Post XKCD comic! +func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + resp := "" + xkclient := xkcd.NewClient() + var err error + var comic xkcd.Comic + var arg string + if len(ctx.Fields) > 1 { + arg = ctx.Fields[1] + } else { + arg = "random" + } + matchedNum, _ := regexp.MatchString(`[0-9]+`, arg) + if arg == "latest" { + comic, err = xkclient.Latest() + } else if matchedNum { + comicNum, _ := strconv.Atoi(arg) + comic, err = xkclient.Get(comicNum) + } else { + comic, err = xkclient.Random() + } + if err != nil { + resp = err.Error() + } + resp = comic.ImageURL + s.ChannelMessageSend(m.ChannelID, resp) +} diff --git a/pocketbot.go b/pocketbot.go index 040386a..c8d689c 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -56,6 +56,7 @@ func main() { 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("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD) //Open a connection to Discord err = Session.Open() From 202d852b1b0dcd25363274b20c10f72a3803b0ac Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 21:44:49 -0400 Subject: [PATCH 07/25] Add function to check member roles --- commands.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/commands.go b/commands.go index 175b48b..3fae3ea 100644 --- a/commands.go +++ b/commands.go @@ -10,6 +10,22 @@ import ( "strconv" ) +//Check member roles +func checkRole(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context, roleid string) bool { + if ctx.IsPrivate { + return false + } + channel, _ := s.Channel(m.ChannelID) + guild := channel.GuildID + member, _ := s.GuildMember(guild, m.Author.ID) + for _, role := range member.Roles { + if role == roleid { + return true + } + } + return false +} + //Generate a heckin swear word func getSwear(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { resp := "" From 636b5d4bb61ab499328c970f48c2e4c1978717df Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 21:45:09 -0400 Subject: [PATCH 08/25] Add bot restart/kill command --- commands.go | 16 ++++++++++++++++ pocketbot.go | 1 + 2 files changed, 17 insertions(+) diff --git a/commands.go b/commands.go index 3fae3ea..1de9ed7 100644 --- a/commands.go +++ b/commands.go @@ -5,7 +5,9 @@ import ( "github.com/bwmarrin/disgord/x/mux" "github.com/nishanths/go-xkcd" "io/ioutil" + "log" "net/http" + "os" "regexp" "strconv" ) @@ -84,3 +86,17 @@ func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { resp = comic.ImageURL s.ChannelMessageSend(m.ChannelID, resp) } + +//Restart the bot +func restartBot(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + if !checkRole(s, m, ctx, os.Getenv("POCKETBOT_ADMIN_ROLE")) { + resp := "OwO you aren't my daddy..." + s.ChannelMessageSend(m.ChannelID, resp) + return + } + resp := "Goodnight ;-;" + s.ChannelMessageSend(m.ChannelID, resp) + Session.Close() + log.Println("brb dying at the request of ", m.Author.String()) + os.Exit(0) +} diff --git a/pocketbot.go b/pocketbot.go index c8d689c..e755ec1 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -57,6 +57,7 @@ func main() { Router.Route("monday", "It's Maki Monday my dudes!", makiMonday) Router.Route("mixes", "Post the link to my fire mixtapes!", postMixes) Router.Route("xkcd", "Post a specific, random, or the latest XKCD comic", getXKCD) + Router.Route("restart", "Restart the bot.", restartBot) //Open a connection to Discord err = Session.Open() From 9155051b8334aa87dfbf3e4e6a0655dd48f317a9 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 21:47:57 -0400 Subject: [PATCH 09/25] Added Drone config --- .drone.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .drone.yml diff --git a/.drone.yml b/.drone.yml new file mode 100644 index 0000000..e3ae91b --- /dev/null +++ b/.drone.yml @@ -0,0 +1,11 @@ +kind: pipeline +name: default +workspace: + base: /go + path: src/discord-pocketbot-go +steps: +- name: test + image: golang + commands: + - go get + - go build \ No newline at end of file From ed09ca153ca9ad4f1d660a16db00e3a1c8ca4816 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 21:49:49 -0400 Subject: [PATCH 10/25] Added kill alias to restart command --- pocketbot.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pocketbot.go b/pocketbot.go index e755ec1..3ddd08f 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -58,6 +58,7 @@ func main() { Router.Route("mixes", "Post the link to my fire mixtapes!", postMixes) 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) //Open a connection to Discord err = Session.Open() From 8ff9042596750cd0342e5851e27495cf790c86d1 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 22:51:53 -0400 Subject: [PATCH 11/25] Add command to post random mixes --- commands.go | 30 ++++++++++++++++++++++++++++++ pocketbot.go | 1 + 2 files changed, 31 insertions(+) diff --git a/commands.go b/commands.go index 1de9ed7..48afb5a 100644 --- a/commands.go +++ b/commands.go @@ -1,11 +1,13 @@ package main import ( + "encoding/json" "github.com/bwmarrin/discordgo" "github.com/bwmarrin/disgord/x/mux" "github.com/nishanths/go-xkcd" "io/ioutil" "log" + "math/rand" "net/http" "os" "regexp" @@ -59,6 +61,34 @@ func postMixes(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { s.ChannelMessageSend(m.ChannelID, resp) } +//Post a link to a random mix +func getRandomMix(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + + //Define mix details + type Mix struct { + Name string `json:"name"` + Type string `json:"type"` + Mtime string `json:"mtime"` + Size int `json:"size"` + } + + mixresp, err := http.Get("https://maki.jawa.moe/mixesjson/") + if err != nil { + resp := "Error fetching mixes: " + err.Error() + s.ChannelMessageSend(m.ChannelID, resp) + return + } + defer mixresp.Body.Close() + + if mixresp.StatusCode == http.StatusOK { + var mixes []*Mix + err = json.NewDecoder(mixresp.Body).Decode(&mixes) + rmix := mixes[rand.Intn(len(mixes))] + resp := "https://maki.jawa.moe/mixes/" + rmix.Name + s.ChannelMessageSend(m.ChannelID, resp) + } +} + //Post XKCD comic! func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { resp := "" diff --git a/pocketbot.go b/pocketbot.go index 3ddd08f..8ec88ce 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -56,6 +56,7 @@ func main() { 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) From 6cd0a56e26540944c82c63e339143f4218f72d5c Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Tue, 6 Aug 2019 22:52:10 -0400 Subject: [PATCH 12/25] Change the rand seed to something actually random --- pocketbot.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pocketbot.go b/pocketbot.go index 8ec88ce..ecb33c1 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -6,9 +6,11 @@ import ( "github.com/bwmarrin/discordgo" "github.com/bwmarrin/disgord/x/mux" "log" + "math/rand" "os" "os/signal" "syscall" + "time" ) var Session, _ = discordgo.New() @@ -17,6 +19,7 @@ var bot_channel 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() From c5b91113a3572e131c5f39946b84f40ec18110e8 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Apr 2020 02:10:20 -0400 Subject: [PATCH 13/25] Only post startup message to Discord if env var POCKETBOT_STARTUP_MESSAGE is TRUE --- pocketbot.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pocketbot.go b/pocketbot.go index ecb33c1..2ff2df9 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -81,5 +81,7 @@ func main() { func ready(s *discordgo.Session, event *discordgo.Ready) { s.UpdateStatus(0, "with droids!") - s.ChannelMessageSend(bot_channel, "This isn't Tatooine...") + if os.Getenv("POCKETBOT_STARTUP_MESSAGE") == "TRUE" { + s.ChannelMessageSend(bot_channel, "This isn't Tatooine...") + } } From c2142bb7414030781267e797a7f7ab71bb9dc692 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Apr 2020 02:56:15 -0400 Subject: [PATCH 14/25] Fix XKCD command after new go-xkcd broke it. --- commands.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/commands.go b/commands.go index 48afb5a..b45585a 100644 --- a/commands.go +++ b/commands.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "github.com/bwmarrin/discordgo" "github.com/bwmarrin/disgord/x/mux" @@ -103,12 +104,14 @@ func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { } matchedNum, _ := regexp.MatchString(`[0-9]+`, arg) if arg == "latest" { - comic, err = xkclient.Latest() + comic, err = xkclient.Latest(context.Background()) } else if matchedNum { comicNum, _ := strconv.Atoi(arg) - comic, err = xkclient.Get(comicNum) + comic, err = xkclient.Get(context.Background(), comicNum) } else { - comic, err = xkclient.Random() + latestcomic, _ := xkclient.Latest(context.Background()) + randcomic := rand.Intn(latestcomic.Number) + comic, err = xkclient.Get(context.Background(), randcomic) } if err != nil { resp = err.Error() From be4b4e06def47d2f5a52b9b85af72cc0e8f37982 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Apr 2020 02:57:21 -0400 Subject: [PATCH 15/25] Move bot admin role to be a global variable. --- commands.go | 2 +- pocketbot.go | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/commands.go b/commands.go index b45585a..dab7bcb 100644 --- a/commands.go +++ b/commands.go @@ -122,7 +122,7 @@ func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { //Restart the bot func restartBot(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { - if !checkRole(s, m, ctx, os.Getenv("POCKETBOT_ADMIN_ROLE")) { + if !checkRole(s, m, ctx, bot_admin_role) { resp := "OwO you aren't my daddy..." s.ChannelMessageSend(m.ChannelID, resp) return diff --git a/pocketbot.go b/pocketbot.go index 2ff2df9..df0a091 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -16,6 +16,7 @@ import ( var Session, _ = discordgo.New() var bot_token string var bot_channel string +var bot_admin_role string var Router = mux.New() func init() { @@ -31,6 +32,9 @@ func init() { 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() { From 995039d56575b2fa3a90fcb1786de5a943886ac8 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Apr 2020 03:19:29 -0400 Subject: [PATCH 16/25] Added sleep command --- commands.go | 12 ++++++++++++ pocketbot.go | 1 + 2 files changed, 13 insertions(+) diff --git a/commands.go b/commands.go index dab7bcb..4f2d1e6 100644 --- a/commands.go +++ b/commands.go @@ -133,3 +133,15 @@ func restartBot(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { log.Println("brb dying at the request of ", m.Author.String()) os.Exit(0) } + +//Morgana yells at you to sleep... +func goToSleep(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + embed := &discordgo.MessageEmbed{ + Description: "Aren't you tired?\nLet's call it a day\nand get some sleep.", + Thumbnail: &discordgo.MessageEmbedThumbnail{ + URL: "https://cdn.discordapp.com/emojis/396429379686629378.png", + }, + Title: "Morgana", + } + s.ChannelMessageSendEmbed(m.ChannelID, embed) +} diff --git a/pocketbot.go b/pocketbot.go index df0a091..2669360 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -67,6 +67,7 @@ func main() { 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) //Open a connection to Discord err = Session.Open() From 73e90c8e48a7ae78a033834ac84d568adbac84f2 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Apr 2020 03:35:19 -0400 Subject: [PATCH 17/25] Add userdata folder --- userdata/.gitignore | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 userdata/.gitignore diff --git a/userdata/.gitignore b/userdata/.gitignore new file mode 100644 index 0000000..c96a04f --- /dev/null +++ b/userdata/.gitignore @@ -0,0 +1,2 @@ +* +!.gitignore \ No newline at end of file From 3d6d093e87d3ea5cdaecdcc013fb2b801ab41bd3 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Sun, 26 Apr 2020 04:45:16 -0400 Subject: [PATCH 18/25] Add setplaying command --- commands.go | 18 ++++++++++++++++++ pocketbot.go | 1 + 2 files changed, 19 insertions(+) diff --git a/commands.go b/commands.go index 4f2d1e6..3016e04 100644 --- a/commands.go +++ b/commands.go @@ -13,6 +13,7 @@ import ( "os" "regexp" "strconv" + "strings" ) //Check member roles @@ -145,3 +146,20 @@ func goToSleep(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { } s.ChannelMessageSendEmbed(m.ChannelID, embed) } + +func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + if !checkRole(s, m, ctx, bot_admin_role) { + resp := "OwO you aren't my daddy..." + s.ChannelMessageSend(m.ChannelID, resp) + return + } + var resp string + if len(ctx.Fields) > 1 { + arg := strings.Join(ctx.Fields[1:], " ") + s.UpdateStatus(0, arg) + resp = "Now playing: " + arg + } else { + resp = "random" + } + s.ChannelMessageSend(m.ChannelID, resp) +} diff --git a/pocketbot.go b/pocketbot.go index 2669360..85a6159 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -68,6 +68,7 @@ func main() { 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) //Open a connection to Discord err = Session.Open() From 1f9dc16e3e8b4da23260e5540606356259325308 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Fri, 1 May 2020 00:08:14 -0400 Subject: [PATCH 19/25] Fixed regex for xkcd comic selection. --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index 3016e04..2ae37cb 100644 --- a/commands.go +++ b/commands.go @@ -103,7 +103,7 @@ func getXKCD(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { } else { arg = "random" } - matchedNum, _ := regexp.MatchString(`[0-9]+`, arg) + matchedNum, _ := regexp.MatchString(`^[0-9]+$`, arg) if arg == "latest" { comic, err = xkclient.Latest(context.Background()) } else if matchedNum { From e8aa115dfaf2db04c811818009816c472883731f Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Fri, 1 May 2020 01:46:47 -0400 Subject: [PATCH 20/25] Added dice rolling command! --- commands.go | 43 +++++++++++++++++++++++++++++++++++++++++++ pocketbot.go | 1 + 2 files changed, 44 insertions(+) diff --git a/commands.go b/commands.go index 2ae37cb..4e1645d 100644 --- a/commands.go +++ b/commands.go @@ -163,3 +163,46 @@ func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) } s.ChannelMessageSend(m.ChannelID, resp) } + +//Roll any number of dice of the same kind +func diceRoller(dienum, diesides int) (total int, diceroles []string) { + for 0 < dienum { + dienum-- + rollresult := rand.Intn(diesides) + 1 + total += rollresult + diceroles = append(diceroles, "D"+strconv.Itoa(diesides)+": **"+strconv.Itoa(rollresult)+"**") + } + return +} + +//Dice rolling command +func rollDice(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + var resp string + var diceroles []string + var dicetotal int + var matchDicePattern = regexp.MustCompile(`^[0-9]*[Dd][1-9][0-9]*$`) + var args = ctx.Fields[1:] + for i := range args { + if matchDicePattern.MatchString(args[i]) { + dLocation := strings.Index(strings.ToLower(args[i]), "d") + dienum, _ := strconv.Atoi(args[i][:dLocation]) + diesides, _ := strconv.Atoi(args[i][dLocation+1:]) + if dienum == 0 { + dienum = 1 + } + resultsTotal, resultsRolls := diceRoller(dienum, diesides) + if dienum > 25 { + diceroles = append(diceroles, strconv.Itoa(dienum)+"*D"+strconv.Itoa(diesides)+": **"+strconv.Itoa(resultsTotal)+"**") + } else { + diceroles = append(diceroles, resultsRolls...) + } + dicetotal += resultsTotal + } + } + results := strings.Join(diceroles, ", ") + resp = m.Author.Username + " rolled " + results + ", for a total of **" + strconv.Itoa(dicetotal) + "**!" + if dicetotal == 0 { + resp = "Oops, looks like no dice were rolled! Try something like `!roll d20`, `!roll 2d6`, or `!roll 3d6 d20` instead." + } + s.ChannelMessageSend(m.ChannelID, resp) +} diff --git a/pocketbot.go b/pocketbot.go index 85a6159..27c3f56 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -69,6 +69,7 @@ func main() { 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) //Open a connection to Discord err = Session.Open() From 2905b1698bc59dcb0aadc7742121b56ac6dd8e78 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Fri, 1 May 2020 01:48:09 -0400 Subject: [PATCH 21/25] UwU --- commands.go | 5 +++++ pocketbot.go | 1 + 2 files changed, 6 insertions(+) diff --git a/commands.go b/commands.go index 4e1645d..a4b6e3e 100644 --- a/commands.go +++ b/commands.go @@ -206,3 +206,8 @@ func rollDice(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { } s.ChannelMessageSend(m.ChannelID, resp) } + +//Test command that says UwU +func sayUwU(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + s.ChannelMessageSend(m.ChannelID, "UwU *nuzzles*") +} diff --git a/pocketbot.go b/pocketbot.go index 27c3f56..a8313af 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -70,6 +70,7 @@ func main() { 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("uwu", "Say UwU", sayUwU) //Open a connection to Discord err = Session.Open() From 2e91d16a028bc438b44607270a6d203e57dcc0b8 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Wed, 20 May 2020 21:40:22 -0400 Subject: [PATCH 22/25] Add magic conch shell command --- commands.go | 12 ++++++++++++ pocketbot.go | 1 + 2 files changed, 13 insertions(+) diff --git a/commands.go b/commands.go index a4b6e3e..ebbe70e 100644 --- a/commands.go +++ b/commands.go @@ -207,6 +207,18 @@ func rollDice(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { s.ChannelMessageSend(m.ChannelID, resp) } +//Ask the Magic Conch shell to predict the future! +func askConch(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { + var resp string + if len(ctx.Fields) > 1 { + conchResponses := []string{"It is certain", "It is decidedly so", "Without a doubt", "Yes definitely", "You may rely on it", "As I see it, yes", "Most likely", "Outlook good", "Yes", "Signs point to yes", "Reply hazy try again", "Ask again later", "Better not tell you now", "Cannot predict now", "Concentrate and ask again", "Don't count on it", "My reply is no", "My sources say no", "Outlook not so good", "Very doubtful"} + resp = conchResponses[rand.Intn(len(conchResponses))] + } else { + resp = "The Magic Conchâ„¢ may be able to see into the future, but it can't read your mind! Please include a question." + } + s.ChannelMessageSend(m.ChannelID, resp) +} + //Test command that says UwU func sayUwU(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) { s.ChannelMessageSend(m.ChannelID, "UwU *nuzzles*") diff --git a/pocketbot.go b/pocketbot.go index a8313af..3c6e632 100644 --- a/pocketbot.go +++ b/pocketbot.go @@ -70,6 +70,7 @@ func main() { 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 From d3db70e72d466c885d8312574961171c973e45f7 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Wed, 20 May 2020 21:45:30 -0400 Subject: [PATCH 23/25] Minor text fix (when nothing is provided to setplaying) --- commands.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands.go b/commands.go index ebbe70e..bcf2917 100644 --- a/commands.go +++ b/commands.go @@ -159,7 +159,7 @@ func setNowPlaying(s *discordgo.Session, m *discordgo.Message, ctx *mux.Context) s.UpdateStatus(0, arg) resp = "Now playing: " + arg } else { - resp = "random" + resp = "Please tell me what to play!" } s.ChannelMessageSend(m.ChannelID, resp) } From 4b011c983d4618f7e4d6849a4727d2a7a0964e47 Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Wed, 20 May 2020 22:39:41 -0400 Subject: [PATCH 24/25] Create initial Dockerfile --- Dockerfile | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..debf7ce --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM golang:latest AS build +WORKDIR /go/src/discord-pocketbot +COPY . . +RUN go get -d -v ./... +RUN CGO_ENABLED=0 GOOS=linux go install -v ./... + +FROM alpine:latest +run apk add --no-cache ca-certificates +WORKDIR /app +COPY --from=build /go/bin/discord-pocketbot discord-pocketbot +CMD ./discord-pocketbot \ No newline at end of file From 0986fe83e35903e48d67fc690a98c94d4d06c52a Mon Sep 17 00:00:00 2001 From: pocketjawa Date: Wed, 20 May 2020 22:40:33 -0400 Subject: [PATCH 25/25] Build Docker image and push to Docker Hub on master --- .drone.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.drone.yml b/.drone.yml index e3ae91b..5763c53 100644 --- a/.drone.yml +++ b/.drone.yml @@ -8,4 +8,13 @@ steps: image: golang commands: - go get - - go build \ No newline at end of file + - go build +- name: docker + image: plugins/docker + settings: + repo: pocketjawa/discord-pocketbot + auto_tag: true + username: + from_secret: docker_username + password: + from_secret: docker_password \ No newline at end of file