remove db file
This commit is contained in:
parent
7bd99acf36
commit
79c48f2368
22
.eslintrc
22
.eslintrc
|
@ -1,11 +1,11 @@
|
||||||
{
|
{
|
||||||
"parser": "@typescript-eslint/parser", // Specifies the ESLint parser
|
"parser": "@typescript-eslint/parser", // Specifies the ESLint parser
|
||||||
"env": {
|
"env": {
|
||||||
"ecmaVersion": 2020 // Allows for the parsing of modern ECMAScript features
|
"ecmaVersion": 2020 // Allows for the parsing of modern ECMAScript features
|
||||||
},
|
},
|
||||||
"extends": [
|
"extends": [
|
||||||
"plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
|
"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
|
"prettier/@typescript-eslint", // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
|
||||||
"plugin:prettier/recommended"
|
"plugin:prettier/recommended"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
/node_modules
|
/node_modules
|
||||||
.env
|
.env
|
||||||
|
/db/*.db
|
10
.prettierrc
10
.prettierrc
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"semi": true,
|
"semi": true,
|
||||||
"trailingComma": "none",
|
"trailingComma": "none",
|
||||||
"singleQuote": true,
|
"singleQuote": true,
|
||||||
"printWidth": 120
|
"printWidth": 120
|
||||||
}
|
}
|
20
README.md
20
README.md
|
@ -1,10 +1,10 @@
|
||||||
# Codey Bot
|
# Codey Bot
|
||||||
|
|
||||||
## Required environment variables
|
## Required environment variables
|
||||||
- `BOT_TOKEN`: the token found in the bot user account.
|
- `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.
|
- `NOTIF_CHANNEL_ID`: the ID of the channel the bot will send system notifications to.
|
||||||
|
|
||||||
## Running the bot locally
|
## Running the bot locally
|
||||||
1. Run `yarn` to install dependencies.
|
1. Run `yarn` to install dependencies.
|
||||||
1. Add the required environment variables in a `.env` file in the root directory.
|
1. Add the required environment variables in a `.env` file in the root directory.
|
||||||
1. Run `yarn dev` to start the bot locally.
|
1. Run `yarn dev` to start the bot locally.
|
||||||
|
|
158
index.ts
158
index.ts
|
@ -1,79 +1,79 @@
|
||||||
import dotenv = require('dotenv')
|
import dotenv = require('dotenv')
|
||||||
dotenv.config()
|
dotenv.config()
|
||||||
|
|
||||||
import Discord = require('discord.js')
|
import Discord = require('discord.js')
|
||||||
import _ = require('lodash')
|
import _ = require('lodash')
|
||||||
import { db } from './components/db'
|
import { db } from './components/db'
|
||||||
|
|
||||||
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID
|
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID
|
||||||
const BOT_TOKEN: string = process.env.BOT_TOKEN
|
const BOT_TOKEN: string = process.env.BOT_TOKEN
|
||||||
const BOT_PREFIX = "."
|
const BOT_PREFIX = "."
|
||||||
|
|
||||||
const client = new Discord.Client()
|
const client = new Discord.Client()
|
||||||
|
|
||||||
const parseCommand = message => {
|
const parseCommand = message => {
|
||||||
// extract arguments by splitting by spaces and grouping strings in quotes
|
// extract arguments by splitting by spaces and grouping strings in quotes
|
||||||
// e.g. .ping 1 "2 3" => ['ping', '1', '2 3']
|
// e.g. .ping 1 "2 3" => ['ping', '1', '2 3']
|
||||||
let args = message.content.slice(BOT_PREFIX.length).match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
|
let args = message.content.slice(BOT_PREFIX.length).match(/[^\s"']+|"([^"]*)"|'([^']*)'/g)
|
||||||
args = _.map(args, arg => {
|
args = _.map(args, arg => {
|
||||||
if (arg[0].match(/'|"/g) && arg[arg.length-1].match(/'|"/g)) {
|
if (arg[0].match(/'|"/g) && arg[arg.length-1].match(/'|"/g)) {
|
||||||
return arg.slice(1,arg.length-1)
|
return arg.slice(1,arg.length-1)
|
||||||
}
|
}
|
||||||
return arg
|
return arg
|
||||||
})
|
})
|
||||||
const firstArg = args.shift()
|
const firstArg = args.shift()
|
||||||
if (!firstArg) return
|
if (!firstArg) return
|
||||||
const command = firstArg.toLowerCase()
|
const command = firstArg.toLowerCase()
|
||||||
return { command, args }
|
return { command, args }
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleCommand = async (message, command, args) => {
|
const handleCommand = async (message, command, args) => {
|
||||||
switch(command) {
|
switch(command) {
|
||||||
case 'ping':
|
case 'ping':
|
||||||
await message.channel.send('pong')
|
await message.channel.send('pong')
|
||||||
break
|
break
|
||||||
|
|
||||||
//dev testing commands
|
//dev testing commands
|
||||||
case 'save':
|
case 'save':
|
||||||
if(args.length<1){
|
if(args.length<1){
|
||||||
await message.channel.send('no args')
|
await message.channel.send('no args')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
db.run('INSERT INTO saved_data (msg_id,data)'+
|
db.run('INSERT INTO saved_data (msg_id,data)'+
|
||||||
'VALUES(?,?)', message.id, args[0])
|
'VALUES(?,?)', message.id, args[0])
|
||||||
await message.channel.send('saved "'+args[0]+'" with id '+message.id)
|
await message.channel.send('saved "'+args[0]+'" with id '+message.id)
|
||||||
break
|
break
|
||||||
case 'dump':
|
case 'dump':
|
||||||
db.each('SELECT * FROM saved_data', async (err, rows) =>{
|
db.each('SELECT * FROM saved_data', async (err, rows) =>{
|
||||||
await message.channel.send(JSON.stringify(rows))
|
await message.channel.send(JSON.stringify(rows))
|
||||||
})
|
})
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleMessage = async message => {
|
const handleMessage = async message => {
|
||||||
// ignore messages without bot prefix and messages from other bots
|
// ignore messages without bot prefix and messages from other bots
|
||||||
if (!message.content.startsWith(BOT_PREFIX) || message.author.bot) return
|
if (!message.content.startsWith(BOT_PREFIX) || message.author.bot) return
|
||||||
// obtain command and args from the command message
|
// obtain command and args from the command message
|
||||||
const { command, args } = parseCommand(message)
|
const { command, args } = parseCommand(message)
|
||||||
// TODO: log commands
|
// TODO: log commands
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await handleCommand(message, command, args)
|
await handleCommand(message, command, args)
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
// TODO: handle error
|
// TODO: handle error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const startBot = async () => {
|
const startBot = async () => {
|
||||||
client.once('ready', async () => {
|
client.once('ready', async () => {
|
||||||
const notif = await client.channels.fetch(NOTIF_CHANNEL_ID) as Discord.TextChannel
|
const notif = await client.channels.fetch(NOTIF_CHANNEL_ID) as Discord.TextChannel
|
||||||
notif.send('Codey is up!')
|
notif.send('Codey is up!')
|
||||||
})
|
})
|
||||||
|
|
||||||
client.on('message', handleMessage)
|
client.on('message', handleMessage)
|
||||||
|
|
||||||
client.login(BOT_TOKEN)
|
client.login(BOT_TOKEN)
|
||||||
}
|
}
|
||||||
|
|
||||||
startBot()
|
startBot()
|
||||||
|
|
Loading…
Reference in New Issue