parent
7bd99acf36
commit
79c48f2368
@ -1,11 +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" |
||||
] |
||||
} |
||||
{ |
||||
"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" |
||||
] |
||||
} |
||||
|
@ -1,2 +1,3 @@ |
||||
/node_modules |
||||
.env |
||||
/node_modules |
||||
.env |
||||
/db/*.db |
@ -1,6 +1,6 @@ |
||||
{ |
||||
"semi": true, |
||||
"trailingComma": "none", |
||||
"singleQuote": true, |
||||
"printWidth": 120 |
||||
{ |
||||
"semi": true, |
||||
"trailingComma": "none", |
||||
"singleQuote": true, |
||||
"printWidth": 120 |
||||
} |
@ -1,10 +1,10 @@ |
||||
# Codey Bot |
||||
|
||||
## Required environment variables |
||||
- `BOT_TOKEN`: the token found in the bot user account. |
||||
- `NOTIF_CHANNEL_ID`: the ID of the channel the bot will send system notifications to. |
||||
|
||||
## Running the bot locally |
||||
1. Run `yarn` to install dependencies. |
||||
1. Add the required environment variables in a `.env` file in the root directory. |
||||
1. Run `yarn dev` to start the bot locally. |
||||
# Codey Bot |
||||
|
||||
## Required environment variables |
||||
- `BOT_TOKEN`: the token found in the bot user account. |
||||
- `NOTIF_CHANNEL_ID`: the ID of the channel the bot will send system notifications to. |
||||
|
||||
## Running the bot locally |
||||
1. Run `yarn` to install dependencies. |
||||
1. Add the required environment variables in a `.env` file in the root directory. |
||||
1. Run `yarn dev` to start the bot locally. |
||||
|
@ -1,79 +1,79 @@ |
||||
import dotenv = require('dotenv') |
||||
dotenv.config() |
||||
|
||||
import Discord = require('discord.js') |
||||
import _ = require('lodash') |
||||
import { db } from './components/db' |
||||
|
||||
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID |
||||
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') |
||||
break |
||||
|
||||
//dev testing commands
|
||||
case 'save': |
||||
if(args.length<1){ |
||||
await message.channel.send('no args') |
||||
return |
||||
}
|
||||
db.run('INSERT INTO saved_data (msg_id,data)'+ |
||||
'VALUES(?,?)', message.id, args[0]) |
||||
await message.channel.send('saved "'+args[0]+'" with id '+message.id) |
||||
break |
||||
case 'dump': |
||||
db.each('SELECT * FROM saved_data', async (err, rows) =>{ |
||||
await message.channel.send(JSON.stringify(rows)) |
||||
}) |
||||
break; |
||||
} |
||||
} |
||||
|
||||
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() |
||||
import dotenv = require('dotenv') |
||||
dotenv.config() |
||||
|
||||
import Discord = require('discord.js') |
||||
import _ = require('lodash') |
||||
import { db } from './components/db' |
||||
|
||||
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID |
||||
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') |
||||
break |
||||
|
||||
//dev testing commands
|
||||
case 'save': |
||||
if(args.length<1){ |
||||
await message.channel.send('no args') |
||||
return |
||||
}
|
||||
db.run('INSERT INTO saved_data (msg_id,data)'+ |
||||
'VALUES(?,?)', message.id, args[0]) |
||||
await message.channel.send('saved "'+args[0]+'" with id '+message.id) |
||||
break |
||||
case 'dump': |
||||
db.each('SELECT * FROM saved_data', async (err, rows) =>{ |
||||
await message.channel.send(JSON.stringify(rows)) |
||||
}) |
||||
break; |
||||
} |
||||
} |
||||
|
||||
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() |
||||
|
Loading…
Reference in new issue