made test suggest command #3
|
@ -0,0 +1,7 @@
|
|||
// Codey ping Command
|
||||
|
||||
import Discord from 'discord.js';
|
||||
|
||||
export const pingCmd = async (message: Discord.Message) => {
|
||||
message.channel.send('pong');
|
||||
};
|
|
@ -0,0 +1,42 @@
|
|||
// Codey suggest Command
|
||||
import Discord from 'discord.js';
|
||||
import { openDB, testDb } from '../components/db';
|
||||
|
||||
export const suggestCmd = async (message: Discord.Message, args: string[]) => {
|
||||
try {
|
||||
// save suggestion into DB
|
||||
const state = 'C'; // Create state = C
|
||||
const db = openDB();
|
||||
var words = '';
|
||||
var word = '';
|
||||
for (word in args) {
|
||||
words += word + ' ';
|
||||
}
|
||||
|
||||
(await db).run(
|
||||
'BEGIN TRANSACTION;' +
|
||||
'CREATE TABLE IF NOT EXISTS suggestions (' +
|
||||
' suggestion_id IDENTITY(1,1) PRIMARY KEY,' +
|
||||
' suggestion_author VARCHAR(255) NOT NULL,' +
|
||||
' created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,' +
|
||||
' suggestion VARCHAR(500) NOT NULL,' +
|
||||
' suggestion_state VARCHAR(1) NOT NULL' +
|
||||
');' +
|
||||
'INSERT INTO suggestions(suggestion_author, suggestion, suggestion_state)' +
|
||||
' VALUES(' +
|
||||
message.id +
|
||||
', ' +
|
||||
words +
|
||||
', ' +
|
||||
state +
|
||||
');' +
|
||||
'COMMIT;'
|
||||
);
|
||||
|
||||
// confirm suggestion was taken
|
||||
message.channel.send('Codey has recieved your suggestion: ' + args[0] + ' ' + args[1] + ' ' + args[2] + '... ');
|
||||
} catch (err) {
|
||||
// Error message
|
||||
message.channel.send('Sorry! There has been an error. Please try again later or let a mod know this happened.');
|
||||
}
|
||||
};
|
29
index.ts
29
index.ts
|
@ -6,6 +6,10 @@ import _ from 'lodash';
|
|||
import { openDB, testDb } from './components/db';
|
||||
import logger from './logger';
|
||||
|
||||
import { pingCmd } from './commands/ping';
|
||||
import { suggestCmd } from './commands/suggest';
|
||||
import { Database, Statement } from 'sqlite3';
|
||||
|
||||
const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.';
|
||||
const BOT_TOKEN: string = process.env.BOT_TOKEN || '.';
|
||||
const BOT_PREFIX = '.';
|
||||
|
@ -43,8 +47,7 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
|
|||
|
||||
switch (command) {
|
||||
case 'ping':
|
||||
await message.channel.send('pong');
|
||||
break;
|
||||
pingCmd(message);
|
||||
}
|
||||
|
||||
//dev testing
|
||||
|
@ -53,6 +56,23 @@ const handleCommand = async (message: Discord.Message, command: string, args: st
|
|||
}
|
||||
};
|
||||
|
||||
const handleAnonCommand = async (message: Discord.Message, command: string, args: string[]) => {
|
||||
// log command and its author info
|
||||
logger.info({
|
||||
event: 'command',
|
||||
messageId: message.id,
|
||||
command,
|
||||
args
|
||||
});
|
||||
|
||||
switch (command) {
|
||||
case 'suggest':
|
||||
suggestCmd(message, args);
|
||||
}
|
||||
};
|
||||
|
||||
const ANON_CMDS = new Set(['suggest', '2', '3']);
|
||||
|
||||
const handleMessage = async (message: Discord.Message) => {
|
||||
// ignore messages without bot prefix and messages from other bots
|
||||
if (!message.content.startsWith(BOT_PREFIX) || message.author.bot) return;
|
||||
|
@ -61,7 +81,12 @@ const handleMessage = async (message: Discord.Message) => {
|
|||
if (!command) return;
|
||||
|
||||
try {
|
||||
// if cmd is a anon cmd
|
||||
if (ANON_CMDS.has(command)) {
|
||||
await handleAnonCommand(message, command, args);
|
||||
} else {
|
||||
await handleCommand(message, command, args);
|
||||
}
|
||||
} catch (e) {
|
||||
// log error
|
||||
logger.error({
|
||||
|
|
Loading…
Reference in New Issue