setting up discord bot framework using discord API

This commit is contained in:
Charles Zhang 2021-05-02 16:29:57 -04:00
parent f5da2566d5
commit 0f495f9e6b
6 changed files with 1908 additions and 0 deletions

11
.eslintrc Normal file
View File

@ -0,0 +1,11 @@
{
"parser": "@typescript-eslint/parser", // Specifies the ESLint parser
"env": {
"ecmaVersion": 2020 // Allows for the parsing of modern ECMAScript features
},
"extends": [
"plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
"prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
"plugin:prettier/recommended"
]
}

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/node_modules
.env

6
.prettierrc Normal file
View File

@ -0,0 +1,6 @@
{
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 120
}

61
index.ts Normal file
View File

@ -0,0 +1,61 @@
import dotenv = require('dotenv')
dotenv.config()
import Discord = require('discord.js')
import _ = require('lodash')
const NOTIF_CHANNEL_ID = "701335585272627203"
const BOT_TOKEN: string = process.env.BOT_TOKEN
const BOT_PREFIX = "."
const client = new Discord.Client()
const parseCommand = message => {
// extract arguments by splitting by spaces and grouping strings in quotes
// e.g. .ping 1 "2 3" => ['ping', '1', '2 3']
let args = message.content.slice(BOT_PREFIX.length).match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
args = _.map(args, arg => {
if (arg[0].match(/'|"/g) && arg[arg.length-1].match(/'|"/g)) {
return arg.slice(1,arg.length-1)
}
return arg
})
const firstArg = args.shift()
if (!firstArg) return
const command = firstArg.toLowerCase()
return { command, args }
}
const handleCommand = async (message, command, args) => {
switch(command) {
case 'ping':
await message.channel.send('pong')
}
}
const handleMessage = async message => {
// ignore messages without bot prefix and messages from other bots
if (!message.content.startsWith(BOT_PREFIX) || message.author.bot) return
// obtain command and args from the command message
const { command, args } = parseCommand(message)
// TODO: log commands
try {
await handleCommand(message, command, args)
} catch(e) {
// TODO: handle error
}
}
const startBot = async () => {
client.once('ready', async () => {
const notif = await client.channels.fetch(NOTIF_CHANNEL_ID) as Discord.TextChannel
notif.send('Codey is up!')
})
client.on('message', handleMessage)
client.login(BOT_TOKEN)
}
startBot()

30
package.json Normal file
View File

@ -0,0 +1,30 @@
{
"name": "csc-discord-bot",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "nodemon index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"discord.js": "^12.5.3",
"dotenv": "^8.2.0",
"lodash": "^4.17.21",
"typescript": "^4.2.4"
},
"devDependencies": {
"@types/lodash": "^4.14.168",
"@types/node": "^15.0.1",
"@typescript-eslint/eslint-plugin": "^4.22.0",
"@typescript-eslint/parser": "^4.22.0",
"eslint": "^7.25.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"nodemon": "^2.0.7",
"prettier": "^2.2.1",
"ts-node": "^9.1.1"
}
}

1798
yarn.lock Normal file

File diff suppressed because it is too large Load Diff