initial commit
This commit is contained in:
@@ -1,11 +0,0 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
12
go.mod
Normal file
12
go.mod
Normal file
@@ -0,0 +1,12 @@
|
||||
module golang-discord-bot
|
||||
|
||||
go 1.19
|
||||
|
||||
require (
|
||||
github.com/bwmarrin/discordgo v0.26.1 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/joho/godotenv v1.4.0 // indirect
|
||||
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
|
||||
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326 // indirect
|
||||
golang.org/x/sys v0.1.0 // indirect
|
||||
)
|
||||
18
go.sum
Normal file
18
go.sum
Normal file
@@ -0,0 +1,18 @@
|
||||
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=
|
||||
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
|
||||
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
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/exp v0.0.0-20221031165847-c99f073a8326 h1:QfTh0HpN6hlw6D3vu8DAwC8pBIwikq0AI1evdm+FksE=
|
||||
golang.org/x/exp v0.0.0-20221031165847-c99f073a8326/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
|
||||
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/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
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=
|
||||
178
main.go
Normal file
178
main.go
Normal file
@@ -0,0 +1,178 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
var (
|
||||
Token string
|
||||
BotPrefix string
|
||||
|
||||
config *configStruct
|
||||
)
|
||||
|
||||
type configStruct struct {
|
||||
Token string `json : "Token"`
|
||||
BotPrefix string `json : "BotPrefix"`
|
||||
}
|
||||
|
||||
func ReadConfig() error {
|
||||
|
||||
err := godotenv.Load(".env")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
Token = os.Getenv("TOKEN")
|
||||
BotPrefix = os.Getenv("BOTPREFIX")
|
||||
|
||||
return nil
|
||||
|
||||
}
|
||||
|
||||
var BotId string
|
||||
var goBot *discordgo.Session
|
||||
|
||||
func Start() {
|
||||
goBot, err := discordgo.New("Bot " + Token)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
u, err := goBot.User("@me")
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
BotId = u.ID
|
||||
|
||||
goBot.AddHandler(messageHandler)
|
||||
|
||||
err = goBot.Open()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
fmt.Println("Bot is running !")
|
||||
}
|
||||
|
||||
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
|
||||
if m.Author.ID == BotId {
|
||||
return
|
||||
}
|
||||
|
||||
var response string
|
||||
baseurl := "https://kong.durp.info/"
|
||||
switch m.Content {
|
||||
case BotPrefix + "ping":
|
||||
response = "pong"
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "meme":
|
||||
response = getJson(baseurl+"random-meme", "url")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "catfact":
|
||||
response = getJson(baseurl+"cat-facts/fact", "fact")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "cat":
|
||||
response = getJson(baseurl+"random-cats", "file")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "yomama":
|
||||
response = getJson(baseurl+"yomama", "joke")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "dadjoke":
|
||||
response = getJson(baseurl+"dadjoke", "joke")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "dog":
|
||||
response = getJson(baseurl+"random-dogs", "message")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "jinglebells":
|
||||
response = getJson(baseurl+"foaas/jinglebells/durp", "message")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
case BotPrefix + "swanson":
|
||||
response = getJson(baseurl+"ronswanson", "base")
|
||||
s.ChannelMessageSend(m.ChannelID, response)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := ReadConfig()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
Start()
|
||||
|
||||
<-make(chan struct{})
|
||||
return
|
||||
}
|
||||
|
||||
func getJson(url, value string) (output string) {
|
||||
|
||||
var s string
|
||||
switch value {
|
||||
case "base":
|
||||
resp, err := http.Get(url)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
sc := strings.Trim(string(body), "[")
|
||||
sc = strings.Trim(sc, "]")
|
||||
sc = strings.Trim(sc, "\"")
|
||||
|
||||
s = fmt.Sprintf("%v", sc)
|
||||
default:
|
||||
client := http.Client{
|
||||
Timeout: time.Second * 2, // Timeout after 2 seconds
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
defer res.Body.Close()
|
||||
|
||||
b, err := io.ReadAll(res.Body)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
json.Unmarshal([]byte(b), &result)
|
||||
s = fmt.Sprintf("%v", result[value])
|
||||
}
|
||||
return string(s)
|
||||
}
|
||||
1911
package-lock.json
generated
1911
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
19
package.json
19
package.json
@@ -1,19 +0,0 @@
|
||||
{
|
||||
"name": "durpot",
|
||||
"version": "0.0.0",
|
||||
"description": "",
|
||||
"main": "src",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"@discordjs/opus": "^0.5.3",
|
||||
"discord.js": "^12.5.3",
|
||||
"dotenv": "^10.0.0",
|
||||
"esm": "^3.2.25",
|
||||
"node-fetch": "^2.6.1",
|
||||
"request": "^2.88.2"
|
||||
}
|
||||
}
|
||||
210
src/app.js
210
src/app.js
@@ -1,210 +0,0 @@
|
||||
const Discord = require('discord.js');
|
||||
const { Client, MessageAttachment, MessageEmbed } = require('discord.js');
|
||||
const fetch = require('node-fetch');
|
||||
const querystring = require('querystring');
|
||||
const request = require('request');
|
||||
require('dotenv').config()
|
||||
|
||||
const client = new Client();
|
||||
const prefix = '!';
|
||||
|
||||
const trim = (str, max) => (str.length > max ? `${str.slice(0, max - 3)}...` : str);
|
||||
|
||||
import { DISCORD_TOKEN, CHANNEL_ID } from './constants'
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log(`Logged in as ${client.user.tag}!`);
|
||||
});
|
||||
|
||||
client.on("guildMemberAdd", (member) => {
|
||||
// console.log(member);
|
||||
const channelId = "775100831590252557";
|
||||
|
||||
const message = `Welcome <@${
|
||||
member.id
|
||||
}> to our server!`;
|
||||
|
||||
const channel = member.guild.channels.cache.get(channelId);
|
||||
channel.send(message);
|
||||
});
|
||||
|
||||
client.on('guildMemberRemove', member => {
|
||||
const channelId = "775100831590252557";
|
||||
|
||||
const message = `Goodbye ${
|
||||
member.user.username
|
||||
}`;
|
||||
|
||||
const channel = member.guild.channels.cache.get(channelId);
|
||||
channel.send(message);
|
||||
})
|
||||
|
||||
client.on('message', async message => {
|
||||
if (!message.content.startsWith(prefix) || message.author.bot) return;
|
||||
|
||||
const args = message.content.slice(prefix.length).trim().split(/ +/);
|
||||
const command = args.shift().toLowerCase();
|
||||
|
||||
if (command === 'commands') {
|
||||
message.channel.send('\
|
||||
I know the follwing commands\n\
|
||||
!cat\n\
|
||||
!urban <Search term>\n\
|
||||
!catfact\n\
|
||||
!meme\n\
|
||||
!yomama\n\
|
||||
!dadjoke\n\
|
||||
!dog\n\
|
||||
!geekjoke\n\
|
||||
!swanson\n\
|
||||
');
|
||||
}
|
||||
|
||||
if (command === 'cat') {
|
||||
const { file } = await fetch('https://kong.durp.info/random-cats').then(response => response.json());
|
||||
|
||||
message.channel.send(file);
|
||||
}
|
||||
|
||||
if (command === 'urban') {
|
||||
if (!args.length) {
|
||||
return message.channel.send('You need to supply a search term!');
|
||||
}
|
||||
|
||||
const query = querystring.stringify({ term: args.join(' ') });
|
||||
|
||||
const { list } = await fetch(`https://kong.durp.info/urban-dictionary/v0/define?${query}`).then(response => response.json());
|
||||
|
||||
if (!list.length) {
|
||||
return message.channel.send(`No results found for **${args.join(' ')}**.`);
|
||||
}
|
||||
|
||||
const [answer] = list;
|
||||
|
||||
const embed = new Discord.MessageEmbed()
|
||||
.setColor('#EFFF00')
|
||||
.setTitle(answer.word)
|
||||
.setURL(answer.permalink)
|
||||
.addFields(
|
||||
{ name: 'Definition', value: trim(answer.definition, 1024) },
|
||||
{ name: 'Example', value: trim(answer.example, 1024) },
|
||||
{ name: 'Rating', value: `${answer.thumbs_up} thumbs up. ${answer.thumbs_down} thumbs down.` },
|
||||
);
|
||||
message.channel.send(embed);
|
||||
}
|
||||
|
||||
if (command === 'catfact') {
|
||||
|
||||
request({
|
||||
url:"https://kong.durp.info/cat-facts/fact",
|
||||
json: true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.fact);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'meme') {
|
||||
request({
|
||||
url:"https://kong.durp.info/random-meme",
|
||||
json: true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.url);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'yomama') {
|
||||
request({
|
||||
url:"https://kong.durp.info/yomama",
|
||||
json: true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.joke);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'dadjoke') {
|
||||
request({
|
||||
url:"https://kong.durp.info/dadjoke",
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.joke);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'dog') {
|
||||
request({
|
||||
url:"https://kong.durp.info/random-dogs",
|
||||
json: true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.message);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'geekjoke') {
|
||||
request({
|
||||
url:"https://kong.durp.info/geekjoke",
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'swanson') {
|
||||
request({
|
||||
url:"https://kong.durp.info/ronswanson",
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'jinglebells') {
|
||||
|
||||
request({
|
||||
url:"https://kong.durp.info/foaas/jinglebells/durp",
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.message);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'nugget') {
|
||||
|
||||
if (!args.length) {
|
||||
return message.channel.send('You need to supply a search term!');
|
||||
}
|
||||
|
||||
request({
|
||||
url:'https://kong.durp.info/foaas/nugget/' + args + '/durp',
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.message);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'rockstar') {
|
||||
|
||||
if (!args.length) {
|
||||
return message.channel.send('You need to supply a search term!');
|
||||
}
|
||||
|
||||
request({
|
||||
url:'https://kong.durp.info/foaas/rockstar/' + args + '/durp',
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.message);
|
||||
});
|
||||
}
|
||||
|
||||
if (command === 'because') {
|
||||
|
||||
request({
|
||||
url:"https://kong.durp.info/foaas/because/durp",
|
||||
json:true
|
||||
}, (err, response, body) => {
|
||||
message.channel.send(body.message);
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
client.login(DISCORD_TOKEN);
|
||||
@@ -1,2 +0,0 @@
|
||||
export const DISCORD_TOKEN = process.env.DISCORD_TOKEN
|
||||
export const CHANNEL_ID = process.env.CHANNEL_ID
|
||||
@@ -1,2 +0,0 @@
|
||||
require = require("esm")(module/*, options*/)
|
||||
module.exports = require("./app.js")
|
||||
Reference in New Issue
Block a user