This repository has been archived by the owner on Jan 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Robin Golang Discord Bot (#91)
* Robin Discord Go Bot * add discordgo bot activity
- Loading branch information
Oscar Kevin
authored
Nov 12, 2022
1 parent
677ddbd
commit caa00b0
Showing
11 changed files
with
259 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Select Docker image | ||
FROM golang:1.19.3-alpine3.16 | ||
|
||
# Create app directory | ||
WORKDIR /app | ||
|
||
# Copy Source Code into /app | ||
ADD . /app/ | ||
|
||
# Install app dependencies | ||
RUN go build -o main . | ||
|
||
# Run Bot | ||
CMD ["./main"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Robin, the Scale Tipper | ||
A Discord Bot written in Golang, he is the prime tactition of the FEH-Gauntlet-Bot Army. | ||
|
||
## Features | ||
* Converts Twitter messages to Atemosta Nitter Instance | ||
* MongoDB Integration | ||
|
||
## Invite Robin Bot to Server | ||
Paste the following url in the browser to invite the client to your server (will ask for admin permissions). | ||
[https://discord.com/api/oauth2/authorize?client_id=1040424451638050887&permissions=8&scope=bot](https://discord.com/api/oauth2/authorize?client_id=1040424451638050887&permissions=8&scope=bot) | ||
|
||
Make sure you enable `Message Intent` in **Discord Developer Bot Settings** | ||
|
||
## Setting Up | ||
1. Install Go via [https://dev.to/aurelievache/learning-go-by-examples-introduction-448n](https://dev.to/aurelievache/learning-go-by-examples-introduction-448n) | ||
2. `go mod init golang-discord-bot-robin` -> Create a go.mod file init. | ||
3. `go mod tidy` -> Just a requirement. | ||
4. `go get "github.com/bwmarrin/discordgo" ` -> Package which we will be using to create our discord bot in Golang. | ||
|
||
## Start the Bot | ||
### Using Docker Compose and config.json | ||
``` | ||
cp config.json.example config.json | ||
docker-compose up -d | ||
``` | ||
### Using Environment Variables | ||
``` | ||
export BOT_TOKEN=<<BOT_TOKEN>> | ||
go run main.go -t $BOT_TOKEN | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package bot | ||
|
||
import ( | ||
"fmt" //to print errors | ||
"golang-discord-bot/config" //importing our config package which we have created above | ||
"github.com/bwmarrin/discordgo" //discordgo package from the repo of bwmarrin . | ||
"strings" | ||
) | ||
|
||
var BotId string | ||
var goBot *discordgo.Session | ||
|
||
func Start() { | ||
|
||
//creating new bot session | ||
goBot, err := discordgo.New("Bot " + config.Token) | ||
|
||
//Handling error | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
// Making our bot a user using User function . | ||
u, err := goBot.User("@me") | ||
//Handling error | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
// Storing our id from u to BotId . | ||
BotId = u.ID | ||
|
||
// Adding handler function to handle our messages using AddHandler from discordgo package. We will declare messageHandler function later. | ||
goBot.AddHandler(setPresence) | ||
goBot.AddHandler(messageHandler) | ||
|
||
err = goBot.Open() | ||
//Error handling | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
//If every thing works fine we will be printing this. | ||
fmt.Println("Bot is running !") | ||
} | ||
|
||
// Set Bot Activity | ||
func setPresence(s *discordgo.Session, event *discordgo.Ready) { | ||
s.UpdateGameStatus(0,"Fire Emblem: Awakening") | ||
} | ||
|
||
//Definition of messageHandler function it takes two arguments first one is discordgo.Session which is s , second one is discordgo.MessageCreate which is m. | ||
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) { | ||
//Bot musn't reply to it's own messages , to confirm it we perform this check. | ||
if m.Author.ID == BotId { | ||
return | ||
} | ||
|
||
// Health Check commands | ||
if m.Content == "owo" { | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "uwu") | ||
} | ||
if m.Content == "uwu" { | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "owo") | ||
} | ||
if m.Content == "kuma" || m.Content == "bear" || m.Content == "oso" { | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "ʕ •ᴥ•ʔ") | ||
} | ||
|
||
// Time to tip the scales! | ||
if m.Content == config.BotPrefix + "time" || m.Content == config.BotPrefix + "scales" || m.Content == "What time is it?" { | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "Time to tip the scales!") | ||
} | ||
|
||
// Convert Twitter links to ATOS Nitter Instance | ||
if strings.Contains(m.Content, "twitter.com") { | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "`Converted to Atemosta's Nitter Instance 🪄`\n" + strings.Replace(m.Content, "twitter.com", "nitter.atemosta.com", -1)) | ||
} | ||
|
||
// fmt.Println("It's Pizza Time") | ||
// fmt.Println(config.BotPrefix) | ||
|
||
//If we message ping to our bot in our discord it will return us pong . | ||
if m.Content == "ping" { | ||
_, _ = s.ChannelMessageSend(m.ChannelID, "pong") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"Token" : "<<BOT_TOKEN>>", | ||
"BotPrefix" : "++" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package config | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" //used to print errors majorly. | ||
"io/ioutil" //it will be used to help us read our config.json file. | ||
) | ||
|
||
var ( | ||
Token string //To store value of Token from config.json . | ||
BotPrefix string // To store value of BotPrefix from config.json. | ||
|
||
config *configStruct //To store value extracted from config.json. | ||
) | ||
|
||
type configStruct struct { | ||
Token string `json : "Token"` | ||
BotPrefix string `json : "BotPrefix"` | ||
} | ||
|
||
func ReadConfig() error { | ||
fmt.Println("Reading config file...") | ||
file, err := ioutil.ReadFile("./config.json") // ioutil package's ReadFile method which we read config.json and return it's value we will then store it in file variable and if an error ocurrs it will be stored in err . | ||
|
||
//Handling error and printing it using fmt package's Println function and returning it . | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return err | ||
} | ||
|
||
// We are here printing value of file variable by explicitly converting it to string . | ||
|
||
fmt.Println(string(file)) | ||
// Here we performing a simple task by copying value of file into config variable which we have declared above , and if there any error we are storing it in err . Unmarshal takes second arguments reference remember it . | ||
err = json.Unmarshal(file, &config) | ||
|
||
//Handling error | ||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return err | ||
} | ||
|
||
// After storing value in config variable we will access it and storing it in our declared variables . | ||
Token = config.Token | ||
BotPrefix = config.BotPrefix | ||
|
||
//If there isn't any error we will return nil. | ||
return nil | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
version: "3.8" | ||
services: | ||
robin: | ||
build: ./ | ||
container_name: robin | ||
command: go run main.go | ||
restart: always |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module golang-discord-bot | ||
|
||
go 1.19 | ||
|
||
require ( | ||
github.com/bwmarrin/discordgo v0.26.1 // indirect | ||
github.com/gorilla/websocket v1.4.2 // indirect | ||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
github.com/bwmarrin/discordgo v0.26.1 h1:AIrM+g3cl+iYBr4yBxCBp9tD9jR3K7upEjl0d89FRkE= | ||
github.com/bwmarrin/discordgo v0.26.1/go.mod h1:NJZpH+1AfhIcyQsPeuBKsUtYrRnjkyu0kIVMCHkZtRY= | ||
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= | ||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= | ||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg= | ||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= | ||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= | ||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= | ||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"golang-discord-bot/bot" //we will create this later | ||
"golang-discord-bot/config" //we will create this later | ||
) | ||
|
||
func main() { | ||
err := config.ReadConfig() | ||
|
||
if err != nil { | ||
fmt.Println(err.Error()) | ||
return | ||
} | ||
|
||
bot.Start() | ||
|
||
<-make(chan struct{}) | ||
return | ||
} |