From 5647328ecf23cefc972f740cce0e2e2e63edc260 Mon Sep 17 00:00:00 2001 From: marko-polo-cheno <37851975+marko-polo-cheno@users.noreply.github.com> Date: Thu, 20 May 2021 20:59:15 -0400 Subject: [PATCH 1/4] made test suggest command --- .vscode/settings.json | 3 +- commands/ping.ts | 7 ++++ commands/suggest.ts | 7 ++++ index.ts | 29 +++++++++++++-- package.json | 2 +- yarn.lock | 84 +------------------------------------------ 6 files changed, 45 insertions(+), 87 deletions(-) create mode 100644 commands/ping.ts create mode 100644 commands/suggest.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index c04db92..986164b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,5 +3,6 @@ "editor.formatOnSave": true, "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" - } + }, + "eslint.workingDirectories": ["src"] } diff --git a/commands/ping.ts b/commands/ping.ts new file mode 100644 index 0000000..a089b04 --- /dev/null +++ b/commands/ping.ts @@ -0,0 +1,7 @@ +// Codey ping Command + +import Discord from 'discord.js'; + +export const pingCmd = async (message: Discord.Message, command: string, args: string[]) => { + message.channel.send('pong'); +}; diff --git a/commands/suggest.ts b/commands/suggest.ts new file mode 100644 index 0000000..36d897d --- /dev/null +++ b/commands/suggest.ts @@ -0,0 +1,7 @@ +// Codey suggest Command +import Discord from 'discord.js'; + +export const suggestCmd = async (message: Discord.Message, command: string, args: string[]) => { + message.channel.send('yay'); + message.channel.send('test suggestion was: ' + args.toString()); +}; diff --git a/index.ts b/index.ts index 526a8d7..cad65d7 100644 --- a/index.ts +++ b/index.ts @@ -6,6 +6,9 @@ import _ from 'lodash'; import logger from './logger'; +import { pingCmd } from './commands/ping'; +import { suggestCmd } from './commands/suggest'; + const NOTIF_CHANNEL_ID: string = process.env.NOTIF_CHANNEL_ID || '.'; const BOT_TOKEN: string = process.env.BOT_TOKEN || '.'; const BOT_PREFIX = '.'; @@ -43,10 +46,27 @@ const handleCommand = async (message: Discord.Message, command: string, args: st switch (command) { case 'ping': - await message.channel.send('pong'); + pingCmd(message, command, args); } }; +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, command, 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; @@ -55,7 +75,12 @@ const handleMessage = async (message: Discord.Message) => { if (!command) return; try { - await handleCommand(message, command, args); + // 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({ diff --git a/package.json b/package.json index 490aaca..7647371 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "dev": "concurrently \"yarn run-dev\" \"yarn watch\"", - "run-dev": "NODE_ENV=dev nodemon index.ts", + "run-dev": "nodemon index.ts", "watch": "tsc --watch", "build": "tsc", "test": "echo \"Error: no test specified\" && exit 1" diff --git a/yarn.lock b/yarn.lock index 3e110d0..db6fdc9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -556,7 +556,7 @@ create-require@^1.1.0: resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== -cross-spawn@^7.0.2, cross-spawn@^7.0.3: +cross-spawn@^7.0.2: version "7.0.3" resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6" integrity sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w== @@ -673,11 +673,6 @@ duplexer3@^0.1.4: resolved "https://registry.yarnpkg.com/duplexer3/-/duplexer3-0.1.4.tgz#ee01dd1cac0ed3cbc7fdbea37dc0a8f1ce002ce2" integrity sha1-7gHdHKwO08vH/b6jfcCo8c4ALOI= -duplexer@~0.1.1: - version "0.1.2" - resolved "https://registry.yarnpkg.com/duplexer/-/duplexer-0.1.2.tgz#3abe43aef3835f8ae077d136ddce0f276b0400e6" - integrity sha512-jtD6YG370ZCIi/9GTaJKQxWTZD045+4R4hTk/x1UyoqadyJ9x9CgSi1RlVDQF8U2sxLLSnFkCaMihqljHIWgMg== - emoji-regex@^7.0.1: version "7.0.3" resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-7.0.3.tgz#933a04052860c85e83c122479c4748a8e4c72156" @@ -852,19 +847,6 @@ esutils@^2.0.2: resolved "https://registry.yarnpkg.com/esutils/-/esutils-2.0.3.tgz#74d2eb4de0b8da1293711910d50775b9b710ef64" integrity sha512-kVscqXk4OCp68SZ0dkgEKVi6/8ij300KBWTJq32P/dYeWTSwK41WyTxalN1eRmA5Z9UU/LX9D7FWSmV9SAYx6g== -event-stream@=3.3.4: - version "3.3.4" - resolved "https://registry.yarnpkg.com/event-stream/-/event-stream-3.3.4.tgz#4ab4c9a0f5a54db9338b4c34d86bfce8f4b35571" - integrity sha1-SrTJoPWlTbkzi0w02Gv86PSzVXE= - dependencies: - duplexer "~0.1.1" - from "~0" - map-stream "~0.1.0" - pause-stream "0.0.11" - split "0.3" - stream-combiner "~0.0.4" - through "~2.3.1" - event-target-shim@^5.0.0: version "5.0.1" resolved "https://registry.yarnpkg.com/event-target-shim/-/event-target-shim-5.0.1.tgz#5d4d3ebdf9583d63a5333ce2deb7480ab2b05789" @@ -958,11 +940,6 @@ fn.name@1.x.x: resolved "https://registry.yarnpkg.com/fn.name/-/fn.name-1.1.0.tgz#26cad8017967aea8731bc42961d04a3d5988accc" integrity sha512-GRnmB5gPyJpAhTQdSZTSp9uaPSvl09KoYcMQtsB9rQoOmzs9dH6ffeccH+Z+cv6P68Hu5bC6JjRh4Ah/mHSNRw== -from@~0: - version "0.1.7" - resolved "https://registry.yarnpkg.com/from/-/from-0.1.7.tgz#83c60afc58b9c56997007ed1a768b3ab303a44fe" - integrity sha1-g8YK/Fi5xWmXAH7Rp2izqzA6RP4= - fs.realpath@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" @@ -1400,11 +1377,6 @@ make-error@^1.1.1: resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== -map-stream@~0.1.0: - version "0.1.0" - resolved "https://registry.yarnpkg.com/map-stream/-/map-stream-0.1.0.tgz#e56aa94c4c8055a16404a0674b78f215f7c8e194" - integrity sha1-5WqpTEyAVaFkBKBnS3jyFffI4ZQ= - merge2@^1.3.0: version "1.4.1" resolved "https://registry.yarnpkg.com/merge2/-/merge2-1.4.1.tgz#4368892f885e907455a6fd7dc55c0c9d404990ae" @@ -1472,11 +1444,6 @@ natural-compare@^1.4.0: resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7" integrity sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc= -node-cleanup@^2.1.2: - version "2.1.2" - resolved "https://registry.yarnpkg.com/node-cleanup/-/node-cleanup-2.1.2.tgz#7ac19abd297e09a7f72a71545d951b517e4dde2c" - integrity sha1-esGavSl+Caf3KnFUXZUbUX5N3iw= - node-fetch@^2.6.1: version "2.6.1" resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052" @@ -1608,13 +1575,6 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== -pause-stream@0.0.11: - version "0.0.11" - resolved "https://registry.yarnpkg.com/pause-stream/-/pause-stream-0.0.11.tgz#fe5a34b0cbce12b5aa6a2b403ee2e73b602f1445" - integrity sha1-/lo0sMvOErWqaitAPuLnO2AvFEU= - dependencies: - through "~2.3" - picomatch@^2.0.4, picomatch@^2.2.1, picomatch@^2.2.3: version "2.2.3" resolved "https://registry.yarnpkg.com/picomatch/-/picomatch-2.2.3.tgz#465547f359ccc206d3c48e46a1bcb89bf7ee619d" @@ -1657,13 +1617,6 @@ progress@^2.0.0: resolved "https://registry.yarnpkg.com/progress/-/progress-2.0.3.tgz#7e8cf8d8f5b8f239c1bc68beb4eb78567d572ef8" integrity sha512-7PiHtLll5LdnKIMw100I+8xJXR5gW2QwWYkT6iJva0bXitZKa/XMrSbdmg3r2Xnaidz9Qumd0VPaMrZlF9V9sA== -ps-tree@^1.2.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/ps-tree/-/ps-tree-1.2.0.tgz#5e7425b89508736cdd4f2224d028f7bb3f722ebd" - integrity sha512-0VnamPPYHl4uaU/nSFeZZpR21QAWRz+sRv4iW9+v/GS/J5U5iZB5BNN6J0RMoOvdx2gWM2+ZFMIm58q24e4UYA== - dependencies: - event-stream "=3.3.4" - pstree.remy@^1.1.7: version "1.1.8" resolved "https://registry.yarnpkg.com/pstree.remy/-/pstree.remy-1.1.8.tgz#c242224f4a67c21f686839bbdb4ac282b8373d3a" @@ -1939,13 +1892,6 @@ spdx-license-ids@^3.0.0: resolved "https://registry.yarnpkg.com/spdx-license-ids/-/spdx-license-ids-3.0.8.tgz#eb1e97ad99b11bf3f82a3b71a0472dd9a00f2ecf" integrity sha512-NDgA96EnaLSvtbM7trJj+t1LUR3pirkDCcz9nOUlPb5DMBGsH7oES6C3hs3j7R9oHEa1EMvReS/BUAIT5Tcr0g== -split@0.3: - version "0.3.3" - resolved "https://registry.yarnpkg.com/split/-/split-0.3.3.tgz#cd0eea5e63a211dfff7eb0f091c4133e2d0dd28f" - integrity sha1-zQ7qXmOiEd//frDwkcQTPi0N0o8= - dependencies: - through "2" - sprintf-js@~1.0.2: version "1.0.3" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" @@ -1956,18 +1902,6 @@ stack-trace@0.0.x: resolved "https://registry.yarnpkg.com/stack-trace/-/stack-trace-0.0.10.tgz#547c70b347e8d32b4e108ea1a2a159e5fdde19c0" integrity sha1-VHxws0fo0ytOEI6hoqFZ5f3eGcA= -stream-combiner@~0.0.4: - version "0.0.4" - resolved "https://registry.yarnpkg.com/stream-combiner/-/stream-combiner-0.0.4.tgz#4d5e433c185261dde623ca3f44c586bcf5c4ad14" - integrity sha1-TV5DPBhSYd3mI8o/RMWGvPXErRQ= - dependencies: - duplexer "~0.1.1" - -string-argv@^0.1.1: - version "0.1.2" - resolved "https://registry.yarnpkg.com/string-argv/-/string-argv-0.1.2.tgz#c5b7bc03fb2b11983ba3a72333dd0559e77e4738" - integrity sha512-mBqPGEOMNJKXRo7z0keX0wlAhbBAjilUdPW13nN0PecVryZxdHIeM7TqbsSUA7VYuS00HGC6mojP7DlQzfa9ZA== - string-width@^3.0.0: version "3.1.0" resolved "https://registry.yarnpkg.com/string-width/-/string-width-3.1.0.tgz#22767be21b62af1081574306f69ac51b62203961" @@ -2073,11 +2007,6 @@ text-table@^0.2.0: resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4" integrity sha1-f17oI66AUgfACvLfSoTsP8+lcLQ= -through@2, through@~2.3, through@~2.3.1: - version "2.3.8" - resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5" - integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU= - to-readable-stream@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/to-readable-stream/-/to-readable-stream-1.0.0.tgz#ce0aa0c2f3df6adf852efb404a783e77c0475771" @@ -2119,17 +2048,6 @@ ts-node@^9.1.1: source-map-support "^0.5.17" yn "3.1.1" -tsc-watch@^4.2.9: - version "4.2.9" - resolved "https://registry.yarnpkg.com/tsc-watch/-/tsc-watch-4.2.9.tgz#d93fc74233ca4ef7ee6b12d08c0fe6aca3e19044" - integrity sha512-DlTaoDs74+KUpyWr7dCGhuscAUKCz6CiFduBN7R9RbLJSSN1moWdwoCLASE7+zLgGvV5AwXfYDiEMAsPGaO+Vw== - dependencies: - cross-spawn "^7.0.3" - node-cleanup "^2.1.2" - ps-tree "^1.2.0" - string-argv "^0.1.1" - strip-ansi "^6.0.0" - tslib@^1.8.1, tslib@^1.9.0: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" -- 2.39.2 From 60f4c80e5dc9bd7ab529800a5509ba89c8dde62a Mon Sep 17 00:00:00 2001 From: marko-polo-cheno <37851975+marko-polo-cheno@users.noreply.github.com> Date: Thu, 20 May 2021 21:02:44 -0400 Subject: [PATCH 2/4] undo settings --- .vscode/settings.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 986164b..c04db92 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,5 @@ "editor.formatOnSave": true, "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" - }, - "eslint.workingDirectories": ["src"] + } } -- 2.39.2 From 60ffb916f845e2ba5acbe3596d58691744467dfd Mon Sep 17 00:00:00 2001 From: marko-polo-cheno <37851975+marko-polo-cheno@users.noreply.github.com> Date: Thu, 20 May 2021 21:07:29 -0400 Subject: [PATCH 3/4] undo settings --- commands/suggest.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/commands/suggest.ts b/commands/suggest.ts index 36d897d..24ca672 100644 --- a/commands/suggest.ts +++ b/commands/suggest.ts @@ -4,4 +4,7 @@ import Discord from 'discord.js'; export const suggestCmd = async (message: Discord.Message, command: string, args: string[]) => { message.channel.send('yay'); message.channel.send('test suggestion was: ' + args.toString()); + + // ack suggestion was taken + // save suggestion into DB }; -- 2.39.2 From bb9a753acaf92ddac89aa8e226fadb205e868e57 Mon Sep 17 00:00:00 2001 From: marko-polo-cheno <37851975+marko-polo-cheno@users.noreply.github.com> Date: Thu, 3 Jun 2021 20:29:40 -0400 Subject: [PATCH 4/4] suggestion created --- commands/ping.ts | 2 +- commands/suggest.ts | 42 +++++++++++++++++++++++++++++++++++++----- index.ts | 5 +++-- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/commands/ping.ts b/commands/ping.ts index a089b04..30a8261 100644 --- a/commands/ping.ts +++ b/commands/ping.ts @@ -2,6 +2,6 @@ import Discord from 'discord.js'; -export const pingCmd = async (message: Discord.Message, command: string, args: string[]) => { +export const pingCmd = async (message: Discord.Message) => { message.channel.send('pong'); }; diff --git a/commands/suggest.ts b/commands/suggest.ts index 24ca672..b840d3b 100644 --- a/commands/suggest.ts +++ b/commands/suggest.ts @@ -1,10 +1,42 @@ // Codey suggest Command import Discord from 'discord.js'; +import { openDB, testDb } from '../components/db'; -export const suggestCmd = async (message: Discord.Message, command: string, args: string[]) => { - message.channel.send('yay'); - message.channel.send('test suggestion was: ' + args.toString()); +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 + ' '; + } - // ack suggestion was taken - // save suggestion into DB + (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.'); + } }; diff --git a/index.ts b/index.ts index f283c84..b3fa003 100644 --- a/index.ts +++ b/index.ts @@ -8,6 +8,7 @@ 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 || '.'; @@ -46,7 +47,7 @@ const handleCommand = async (message: Discord.Message, command: string, args: st switch (command) { case 'ping': - pingCmd(message, command, args); + pingCmd(message); } //dev testing @@ -66,7 +67,7 @@ const handleAnonCommand = async (message: Discord.Message, command: string, args switch (command) { case 'suggest': - suggestCmd(message, command, args); + suggestCmd(message, args); } }; -- 2.39.2