Skip to content

Commit 66c9560

Browse files
Merge branch 'init-setup' into 'master'
Setting up a simple bot command framework and linter/formatter See merge request csc/discord-bot!1
2 parents f5da256 + 2466107 commit 66c9560

File tree

7 files changed

+1918
-1
lines changed

7 files changed

+1918
-1
lines changed

.eslintrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"parser": "@typescript-eslint/parser", // Specifies the ESLint parser
3+
"env": {
4+
"ecmaVersion": 2020 // Allows for the parsing of modern ECMAScript features
5+
},
6+
"extends": [
7+
"plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
8+
"prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
9+
"plugin:prettier/recommended"
10+
]
11+
}

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/node_modules
2+
.env

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "none",
4+
"singleQuote": true,
5+
"printWidth": 120
6+
}

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
# Codey Bot
1+
# Codey Bot
2+
3+
## Required environment variables
4+
- `BOT_TOKEN`: the token found in the bot user account.
5+
- `NOTIF_CHANNEL_ID`: the ID of the channel the bot will send system notifications to.
6+
7+
## Running the bot locally
8+
1. Run `yarn` to install dependencies.
9+
1. Add the required environment variables in a `.env` file in the root directory.
10+
1. Run `yarn dev` to start the bot locally.

index.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import dotenv = require('dotenv')
2+
dotenv.config()
3+
4+
import Discord = require('discord.js')
5+
import _ = require('lodash')
6+
7+
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID
8+
const BOT_TOKEN: string = process.env.BOT_TOKEN
9+
const BOT_PREFIX = "."
10+
11+
const client = new Discord.Client()
12+
13+
const parseCommand = message => {
14+
// extract arguments by splitting by spaces and grouping strings in quotes
15+
// e.g. .ping 1 "2 3" => ['ping', '1', '2 3']
16+
let args = message.content.slice(BOT_PREFIX.length).match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
17+
args = _.map(args, arg => {
18+
if (arg[0].match(/'|"/g) && arg[arg.length-1].match(/'|"/g)) {
19+
return arg.slice(1,arg.length-1)
20+
}
21+
return arg
22+
})
23+
const firstArg = args.shift()
24+
if (!firstArg) return
25+
const command = firstArg.toLowerCase()
26+
return { command, args }
27+
}
28+
29+
const handleCommand = async (message, command, args) => {
30+
switch(command) {
31+
case 'ping':
32+
await message.channel.send('pong')
33+
}
34+
}
35+
36+
const handleMessage = async message => {
37+
// ignore messages without bot prefix and messages from other bots
38+
if (!message.content.startsWith(BOT_PREFIX) || message.author.bot) return
39+
// obtain command and args from the command message
40+
const { command, args } = parseCommand(message)
41+
// TODO: log commands
42+
43+
try {
44+
await handleCommand(message, command, args)
45+
} catch(e) {
46+
// TODO: handle error
47+
}
48+
}
49+
50+
const startBot = async () => {
51+
client.once('ready', async () => {
52+
const notif = await client.channels.fetch(NOTIF_CHANNEL_ID) as Discord.TextChannel
53+
notif.send('Codey is up!')
54+
})
55+
56+
client.on('message', handleMessage)
57+
58+
client.login(BOT_TOKEN)
59+
}
60+
61+
startBot()

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "csc-discord-bot",
3+
"version": "0.1.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"dev": "nodemon index.ts",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"discord.js": "^12.5.3",
14+
"dotenv": "^8.2.0",
15+
"lodash": "^4.17.21",
16+
"typescript": "^4.2.4"
17+
},
18+
"devDependencies": {
19+
"@types/lodash": "^4.14.168",
20+
"@types/node": "^15.0.1",
21+
"@typescript-eslint/eslint-plugin": "^4.22.0",
22+
"@typescript-eslint/parser": "^4.22.0",
23+
"eslint": "^7.25.0",
24+
"eslint-config-prettier": "^8.3.0",
25+
"eslint-plugin-prettier": "^3.4.0",
26+
"nodemon": "^2.0.7",
27+
"prettier": "^2.2.1",
28+
"ts-node": "^9.1.1"
29+
}
30+
}

0 commit comments

Comments
 (0)