diff --git a/examples/discordgo/.gitignore b/examples/discordgo/.gitignore new file mode 100644 index 0000000..f7b3492 --- /dev/null +++ b/examples/discordgo/.gitignore @@ -0,0 +1,15 @@ +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Test binary, built with `go test -c` +*.test + +# Output of the go coverage tool, specifically when used with LiteIDE +*.out + +# Dependency directories (remove the comment below to include it) +# vendor/ \ No newline at end of file diff --git a/examples/discordgo/README.md b/examples/discordgo/README.md new file mode 100644 index 0000000..03c8811 --- /dev/null +++ b/examples/discordgo/README.md @@ -0,0 +1,28 @@ +--- +title: Discord Golang bot +description: A Discord bot written in Golang +tags: +- discordgo +- golang +--- + +# DiscordGo Example + +This example starts a Discord bot using [discordgo](https://github.com/bwmarrin/discordgo) and [Golang](https://golang.org/). + +[![Deploy on Railway](https://railway.app/button.svg)](https://railway.app/new?template=https%3A%2F%2Fgithub.com%2Frailwayapp%2Fexamples%2Ftree%2Fmaster%2Fexamples%2Fdiscordgo&envs=DISCORD_TOKEN&DISCORD_TOKENDesc=Token+of+your+Discord+bot) + +## ✨ Features + +- DiscordGo +- Golang + +## 💁‍♀️ How to use + +- Install dependencies `go mod download` +- Connect to your Railway project `railway link` +- Start the bot `railway run go run main.go` + +## 📝 Notes + +The server started launches a Discord bot with a couple of basic commands. The code is located at `main.go`. \ No newline at end of file diff --git a/examples/discordgo/go.mod b/examples/discordgo/go.mod new file mode 100644 index 0000000..0180475 --- /dev/null +++ b/examples/discordgo/go.mod @@ -0,0 +1,5 @@ +module github.com/railwayapp/starters/examples/discordgo + +go 1.16 + +require github.com/bwmarrin/discordgo v0.23.2 // indirect diff --git a/examples/discordgo/go.sum b/examples/discordgo/go.sum new file mode 100644 index 0000000..28dbed2 --- /dev/null +++ b/examples/discordgo/go.sum @@ -0,0 +1,6 @@ +github.com/bwmarrin/discordgo v0.23.2 h1:BzrtTktixGHIu9Tt7dEE6diysEF9HWnXeHuoJEt2fH4= +github.com/bwmarrin/discordgo v0.23.2/go.mod h1:c1WtWUGN6nREDmzIpyTp/iD3VYt4Fpx+bVyfBG7JE+M= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= +github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16 h1:y6ce7gCWtnH+m3dCjzQ1PCuwl28DDIc3VNnvY29DlIA= +golang.org/x/crypto v0.0.0-20181030102418-4d3f4d9ffa16/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= diff --git a/examples/discordgo/main.go b/examples/discordgo/main.go new file mode 100644 index 0000000..b741938 --- /dev/null +++ b/examples/discordgo/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "fmt" + "github.com/bwmarrin/discordgo" + "os" + "os/signal" + "syscall" +) + +func main() { + // Create a new session using the DISCORD_TOKEN environment variable from Railway + dg, err := discordgo.New("Bot " + os.Getenv("DISCORD_TOKEN")) + if err != nil { + fmt.Printf("Error while starting bot: %s", err) + return + } + + // Add the message handler + dg.AddHandler(messageCreate) + + // Add the Guild Messages intent + dg.Identify.Intents = discordgo.IntentsGuildMessages + + // Connect to the gateway + err = dg.Open() + if err != nil { + fmt.Printf("Error while connecting to gateway: %s", err) + return + } + + // Wait until Ctrl+C or another signal is received + fmt.Println("The bot is 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 + + // Close the Discord session + dg.Close() +} + +func messageCreate(s *discordgo.Session, m *discordgo.MessageCreate) { + // Don't proceed if the message author is a bot + if m.Author.Bot { + return + } + + if m.Content == "ping" { + s.ChannelMessageSend(m.ChannelID, "Pong 🏓") + return + } + + if m.Content == "hello" { + s.ChannelMessageSend(m.ChannelID, "Choo choo! 🚅") + return + } +}