Add discordgo starter (#178)

* Added discordgo starter

* Removed unnecessary check
This commit is contained in:
Evernote
2021-10-25 22:23:37 +05:30
committed by GitHub
parent 1c7bcb98f0
commit 27ab677f74
5 changed files with 111 additions and 0 deletions

15
examples/discordgo/.gitignore vendored Normal file
View File

@@ -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/

View File

@@ -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`.

View File

@@ -0,0 +1,5 @@
module github.com/railwayapp/starters/examples/discordgo
go 1.16
require github.com/bwmarrin/discordgo v0.23.2 // indirect

View File

@@ -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=

View File

@@ -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
}
}