From 4dc25492e52c5e744babaf2b8ffc596306249161 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 12 Dec 2021 13:34:37 -0600 Subject: [PATCH] Wipe the non-functional code --- package.json | 11 +- src/cmd_handler.ts | 198 ------------------------------- src/commands/next.ts | 84 ------------- src/commands/start.ts | 30 ----- src/constants.ts | 38 ------ src/main.ts | 50 -------- src/services/discord_handler.ts | 129 -------------------- src/services/test_runner.ts | 72 ----------- src/services/twitch_handler.ts | 164 ------------------------- src/types/command_metadata.d.ts | 42 ------- src/types/config.d.ts | 58 --------- src/types/message_metadata.d.ts | 20 ---- src/types/option.d.ts | 17 --- src/types/permission_levels.d.ts | 11 -- src/types/test_object.d.ts | 24 ---- src/types/webhooks.d.ts | 14 --- src/utils/Command.ts | 159 ------------------------- src/utils/Commands.ts | 96 --------------- src/utils/Config.ts | 31 ----- src/utils/flags.ts | 38 ------ src/utils/sorting.ts | 39 ------ src/utils/tests.ts | 31 ----- src/utils/webhook.ts | 80 ------------- 23 files changed, 1 insertion(+), 1435 deletions(-) delete mode 100644 src/cmd_handler.ts delete mode 100644 src/commands/next.ts delete mode 100644 src/commands/start.ts delete mode 100644 src/constants.ts delete mode 100644 src/main.ts delete mode 100644 src/services/discord_handler.ts delete mode 100644 src/services/test_runner.ts delete mode 100644 src/services/twitch_handler.ts delete mode 100644 src/types/command_metadata.d.ts delete mode 100644 src/types/config.d.ts delete mode 100644 src/types/message_metadata.d.ts delete mode 100644 src/types/option.d.ts delete mode 100644 src/types/permission_levels.d.ts delete mode 100644 src/types/test_object.d.ts delete mode 100644 src/types/webhooks.d.ts delete mode 100644 src/utils/Command.ts delete mode 100644 src/utils/Commands.ts delete mode 100644 src/utils/Config.ts delete mode 100644 src/utils/flags.ts delete mode 100644 src/utils/sorting.ts delete mode 100644 src/utils/tests.ts delete mode 100644 src/utils/webhook.ts diff --git a/package.json b/package.json index 5d8dee4..4b20103 100644 --- a/package.json +++ b/package.json @@ -12,15 +12,6 @@ "@types/node": "^12.7.5", "@types/tmi.js": "^1.4.0", "axios": "^0.24.0", - "bufferutil": "^4.0.1", - "eris": "^0.11.0", - "express": "^4.17.1", - "express-tl": "^1.0.2", - "fs": "0.0.1-security", - "mathjs": "^10.0.0", - "request": "^2.88.0", - "request-promise-native": "^1.0.7", - "tmi.js": "^1.5.0", - "utf-8-validate": "^5.0.2" + "tmi.js": "^1.5.0" } } diff --git a/src/cmd_handler.ts b/src/cmd_handler.ts deleted file mode 100644 index 63e85be..0000000 --- a/src/cmd_handler.ts +++ /dev/null @@ -1,198 +0,0 @@ -// -// cmd_handler.ts -// -// Written by: Oliver Akins (2019/11/06 - 2020/01/31) -// - - -/* Imports */ -import { Command, Confirmation } from "./utils/Command"; -import { PERM, FLAG_INDICATOR } from "./constants"; -import { SORT_COMMANDS } from "./utils/sorting"; -import { LOAD_CONFIG } from "./utils/Config"; -import { GET_FLAGS } from "./utils/flags"; -import { log } from "./utils/webhook"; - - - -export var commands: Command[] = []; -export var confirms: Confirmation[] = []; -var global_last_ran: number; -var service_last_rans: any = {} - - -export const REGISTER_COMMAND = (metadata: cmd_metadata): boolean => { - // Ensure command gets added correctly - try { - commands.push(new Command(metadata)); - SORT_COMMANDS(commands); - return true; - } catch (error) { - return false; - }; -}; - - - -export const HANDLE_MESSAGE = (ctx: msg_data): string => { - - let config: config = LOAD_CONFIG(); - let datetime = new Date(); - let timezone = datetime.toLocaleTimeString("en-us", {timeZoneName:"short"}).split(" ")[2]; - let date = `${datetime.getFullYear()}-${datetime.getMonth()+1}-${datetime.getDate()}` - + ` @ ${datetime.getHours()}:${datetime.getMinutes()} ${timezone}`; - - - // Confirmation handling: - // Check if we need any confirmations from users - for (var index in confirms) { - let confirmation = confirms[index]; - - let response: CONFIRM_TYPE = confirmation.matches( - ctx.user, ctx.channel, ctx.message - ); - - if (!["no_match", "expired"].includes(response)) { - confirms.splice(parseInt(index), 1); - let cmd_resp = confirmation.run(response); - - - // Check if we have a response to log - if (cmd_resp) { - log({ - title: `Log Entry (Confirmation Response):`, - msg: `Command:\`\`\`\n${ctx.message}\n\`\`\`\n\nResponse:\`\`\`\n${cmd_resp}\n\`\`\``, - embed: true, - fields: { - "Date:": date, - "Is Mod:": ctx.level >= PERM.MOD, - "Is Admin:": ctx.level >= PERM.ADMIN, - "Level:": ctx.level, - "Channel:": ctx.channel, - "Username": ctx.user, - "Platform": ctx.source, - "Confirm Type": response - }, - no_stdout: true - }); - }; - - return cmd_resp; - } - - else if (response === "expired") { - confirms.splice(parseInt(index), 1); - confirmation.run(response); - }; - }; - - - - // SECTION: Global command cooldowns - if (config.bot.COOLDOWN_TYPE === "GLOBAL" && ctx.cooldown) { - if (global_last_ran) { - if (Date.now() - global_last_ran < config.bot.COOLDOWN_TIME * 1000) { - return null; - }; - }; - global_last_ran = Date.now(); - } - // !SECTION: Global command cooldowns - - - - // SECTION: Service command cooldowns - else if (config.bot.COOLDOWN_TYPE === "SERVICE" && ctx.cooldown) { - if (service_last_rans[ctx.source]) { - if (Date.now() - service_last_rans[ctx.source] < config.bot.COOLDOWN_TIME * 1000) { - return null; - }; - }; - service_last_rans[ctx.source] = Date.now(); - }; - // !SECTION: Service command cooldowns - - - // SECTION: Flag parsing - ctx.flags = GET_FLAGS(ctx.message); - - // removing arguments that are indicated as flags - let re = new RegExp(`\\${FLAG_INDICATOR}\\w+\\s?`, `g`); - ctx.message = ctx.message.replace(re, ``); - // !SECTION: Flag parsing - - - - // Check all registered commands - for (var cmd of commands) { - - - // NOTE: Checking if message doesn't match - if (!cmd.matches(ctx.message.toLowerCase())) { continue; }; - - - // NOTE: Permission checking - if (ctx.level < cmd.level) { - if (config.bot.INVALID_PERM_ERROR) { - return `Invalid Permissions, you must be at least level ${cmd.level}, you are level ${ctx.level}.`; - }; - return null; - }; - - - // NOTE: per-command cooldown - if (config.bot.COOLDOWN_TYPE === "COMMAND" && ctx.cooldown) { - if (cmd.last_ran) { - if (Date.now() - cmd.last_ran < config.bot.COOLDOWN_TIME * 1000) { - return null; - }; - }; - cmd.last_ran = Date.now(); - }; - - - // NOTE: Case sensitivity - if (!cmd.case_sensitive) { - ctx.message = ctx.message.toLowerCase(); - }; - - - // NOTE: Argument parsing - let args = ctx.message - .slice(config.bot.PREFIX.length) - .split(" ") - .slice(cmd.full_name.split(" ").length); - - - // Ensure the use supplied enough arguments - if (args.length < cmd.mand_args) { - return `Not enough arguments, missing argument: \`${cmd.arg_list[args.length]}\``; - }; - - - let response = cmd.execute(ctx, args); - - - log({ - title: `Log Entry:`, - msg: `Command:\`\`\`\n${ctx.message}\n\`\`\`\n\nResponse:\`\`\`\n${response}\n\`\`\``, - embed: true, - fields: { - "Date:": date, - "Is Mod:": ctx.level >= PERM.MOD, - "Is Admin:": ctx.level >= PERM.ADMIN, - "Level:": ctx.level, - "Channel:": ctx.channel, - "Username": ctx.user, - "Platform": ctx.source - }, - no_stdout: true - }); - return response; - }; - return null; -}; - - - -/* Importing all the commands so they can register */ diff --git a/src/commands/next.ts b/src/commands/next.ts deleted file mode 100644 index 4fd3343..0000000 --- a/src/commands/next.ts +++ /dev/null @@ -1,84 +0,0 @@ -// -// next.ts -// -// Written by: Oliver Akins (2021/12/02) -// - - -import axios from "axios"; -import { PERM } from "../constants"; -import { REGISTER_COMMAND } from "../cmd_handler"; -import { question_index, change_index, questions, users, reset_users, user_right_count} from "../main"; - - -const NEXT_COMMAND = (ctx: msg_data, args: string[]): string => { - - // Ensure the quiz has been started. - if (question_index < 0) { - return `Cannot go the next question before "!start"ing the quiz.`; - }; - - // count users who got it right - let users_correct = Object.values(users) - .filter(x => x == questions[question_index]) - .length; - - let correct_users: string[] = []; - for (var user in users) { - let delta = 0; - if (users[user] == questions[question_index]) { - correct_users.push(user); - delta = 1; - }; - - if (user_right_count[user] != null) { - user_right_count[user] = delta; - } else { - user_right_count[user] += delta; - }; - }; - - reset_users(); - - axios.post( - `https://ptb.discord.com/api/webhooks/916068083863539782/9k2b66SUIeY8hl5id7gdnZF6Abc8Fb1f5-RVlgbUJqirNmALkrrgrCWdsufcZ5Bn2a4i`, - { - content: `Users who got question ${question_index + 1} right: -\`\`\`csv -${correct_users.join(", ")} -\`\`\`` - } - ) - - // Check if questions has run dry - if (question_index == questions.length) { - - // Send message in Discord for record-keeping - axios.post( - `https://ptb.discord.com/api/webhooks/916068083863539782/9k2b66SUIeY8hl5id7gdnZF6Abc8Fb1f5-RVlgbUJqirNmALkrrgrCWdsufcZ5Bn2a4i`, - { - content: `\`\`\`json\n${JSON.stringify(user_right_count, null, "\t")}\`\`\`` - } - ); - - change_index(-1); - return `${users_correct} got the last question right. Quiz has ended. Kthxbai.`; - }; - - change_index(question_index + 1); - return `${users_correct} got that question right. Changed to the next question!`; -}; - - -REGISTER_COMMAND({ - description: "", - executable: NEXT_COMMAND, - requires_confirm: false, - case_sensitive: false, - name: "start", - opt_args: 0, - args: [], - level: PERM.MOD, - arg_info: [], - flags: {} -}); \ No newline at end of file diff --git a/src/commands/start.ts b/src/commands/start.ts deleted file mode 100644 index 1c939d4..0000000 --- a/src/commands/start.ts +++ /dev/null @@ -1,30 +0,0 @@ -// -// start.ts -// -// Written by: Oliver Akins (2021/12/02) -// - - -import { PERM } from "../constants"; -import { REGISTER_COMMAND } from "../cmd_handler"; -import { change_index } from "../main"; - - -const START_COMMAND = (ctx: msg_data, args: string[]): string => { - change_index(0); - return `Quiz has been started!`; -}; - - -REGISTER_COMMAND({ - description: "", - executable: START_COMMAND, - requires_confirm: false, - case_sensitive: false, - name: "start", - opt_args: 0, - args: [], - level: PERM.MOD, - arg_info: [], - flags: {} -}); \ No newline at end of file diff --git a/src/constants.ts b/src/constants.ts deleted file mode 100644 index 5538ce8..0000000 --- a/src/constants.ts +++ /dev/null @@ -1,38 +0,0 @@ -// -// constants.ts -// -// Written by: Oliver Akins (2019/11/06 - 2020/07/21) -// - - -export const VERSION: string = "v2020.07.21"; - - -// The indicator used to tell the bot what arguments are flags. -// This indicator cannot be used as the start of any non-flag argument -export const FLAG_INDICATOR: string = "$" - - -// The number of seconds for the confirmation timeouts -export const CONFIRM_TIMEOUT: number = 5; - - -export const PERM: perms = { - ALL: 0, - MOD: 1, - ADMIN: 2 -}; - - -export const LIMIT = { - DISCORD: 2000, - TWITCH: 500 -}; - - -export const TEST_CHANNEL = "#tests_channel#"; -export const TEST_LINKS = "#test_links"; -export const TEST_USER = "#test_user#"; - - -export const REPO = "https://github.com/Oliver-Akins/chatbot-template"; \ No newline at end of file diff --git a/src/main.ts b/src/main.ts deleted file mode 100644 index 50beb45..0000000 --- a/src/main.ts +++ /dev/null @@ -1,50 +0,0 @@ -// -// main.ts -// -// Written by: Oliver Akins (2019/11/06 - 2021/12/02) -// - - -import { run_discord } from "./services/discord_handler"; -import { run_twitch } from "./services/twitch_handler"; -import { run_tests } from "./services/test_runner"; -import { LOAD_CONFIG } from "./utils/Config"; - - -export function change_index(value: number) { - question_index = value; -}; -export function reset_users() { - users = {}; -}; - -export var user_right_count: {[index: string]: number} = {}; -export var users: {[index: string]: string} = {}; -export let question_index: number = -1; - -export const questions: string[] = [] - -export var config = LOAD_CONFIG(); -let args = process.argv.slice(2); - - -// Not enough arguments -if (args.length < 1) { - console.error(`Too few arguments.`); - process.exit(1); -} - - -if (args.includes("--test")) { - process.exit(run_tests(args.includes("--silent"))); -}; - - -if (args.includes("--twitch")) { - run_twitch(); -}; - - -if (args.includes("--discord")) { - run_discord(); -}; \ No newline at end of file diff --git a/src/services/discord_handler.ts b/src/services/discord_handler.ts deleted file mode 100644 index f916b0a..0000000 --- a/src/services/discord_handler.ts +++ /dev/null @@ -1,129 +0,0 @@ -// -// discord_handler.ts -// -// Written by: Oliver Akins (2019/11/23 - 2019/12/19) -// - - -import { log_error, log } from "../utils/webhook"; -import { HANDLE_MESSAGE } from "../cmd_handler"; -import { LOAD_CONFIG } from "../utils/Config"; -import { PERM } from "../constants"; -const Eris = require("eris"); - - - -export const run_discord = (): void => { - - const config: config = LOAD_CONFIG(); - - let bot = new Eris(config.DEV ? config.discord.DEV_TOKEN : config.discord.OAUTH_TOKEN); - - - bot.on("ready", () => { - log({ msg: `* Connected to Discord gateway` }); - }); - - - // Message handler - bot.on("messageCreate", (msg: any) => { - try { - - // Ensure message to parse - if (msg.content.length === 0) { return; }; - - - // SECTION: Exit conditions - - // NOTE: Ensure not a system message - if (msg.type !== 0) { return; } - - // NOTE: Ensure channel type is GUILD_TEXT - else if (msg.channel.type !== 0) { return; } - - // NOTE: Ensure not a webhook or Clyde - else if (msg.author.discriminator === "0000") { return; } - - // NOTE: Ensure not a bot - else if (msg.member.bot) { return; } - - // !SECTION: Exit conditions - - - var is_mod = msg.member.roles.filter( - (x: string) => { return config.discord.MOD_ROLES.includes(x); } - ).length > 0; - var is_admin = config.discord.ADMIN.includes(msg.member.id); - - - var level = PERM.ALL; - if (is_mod) { level = PERM.MOD; }; - if (is_admin) { level = PERM.ADMIN; }; - - - var response: string | void = HANDLE_MESSAGE({ - channel: `Discord:${msg.member.guild.id}`, - level: level, - message: msg.content.trim().replace(/\n/g, ""), - source: "Discord", - user: msg.author.username, - cooldown: true, - test: false - }); - - // NOTE: Ensure response isn't null - if (response !== null) { - - // NOTE: Reply with string - bot.createMessage(msg.channel.id, response) - }; - - } - - // SECTION: Error Handling - catch (error) { - log_error({ - "embeds": [ - { - "title": `${error.name}`, - "color": 13238272, - "description": `**Error Message:**\n\`\`\`\n${error.message}\n\`\`\``, - "fields": [ - { - "name": "**Message Context:**", - "value": `\`\`\`\n${JSON.stringify(msg, null, 2)}\n\`\`\`` - }, - { - "name": "**Message Content:**", - "value": `\`\`\`json\n${msg.content}\`\`\`` - }, - { - "name": "**Is Mod:**", - "value": `\`${is_mod}\``, - "inline": true - }, - { - "name": "**Is Admin:**", - "value": `\`${is_admin}\``, - "inline": true - }, - { - "name": "**Channel:**", - "value": `\`${msg.channel.name}\``, - "inline": true - }, - { - "name": "Source", - "value": "Discord", - "inline": true - } - ] - } - ] - }); - }; - // !SECTION: Error Handling - }); - - bot.connect(); -}; \ No newline at end of file diff --git a/src/services/test_runner.ts b/src/services/test_runner.ts deleted file mode 100644 index 4cf18ed..0000000 --- a/src/services/test_runner.ts +++ /dev/null @@ -1,72 +0,0 @@ -// -// test_runner.ts -// -// Written by: Oliver Akins (2019/11/17 - 2020/07/21) -// - - -import { TEST_CHANNEL, TEST_USER } from "../constants"; -import { HANDLE_MESSAGE } from "../cmd_handler"; -import { LOAD_CONFIG } from "../utils/Config"; -import { tests } from "../utils/tests"; - - -const config = LOAD_CONFIG(); -var fail_count = 0; - - -export const run_tests = (silent: boolean): number => { - - // Run through each test - for (var test of tests) { - - - let response = HANDLE_MESSAGE({ - message: test.msg_meta.message, - level: test.msg_meta.level, - user: TEST_USER, - cooldown: false, - source: test.msg_meta.source, - channel: test.msg_meta.channel || TEST_CHANNEL, - test: true - }); - - - // If the confirmation message is defined, trigger a confirmation - if (test.confirm_msg) { - response = HANDLE_MESSAGE({ - message: test.confirm_msg.message, - level: test.confirm_msg.level, - user: TEST_USER, - cooldown: false, - source: test.confirm_msg.source, - channel: test.msg_meta.channel || TEST_CHANNEL, - test: true - }); - }; - - - // Compare outputs - if (test.expected_return != response) { - fail_count++; - if (!silent) { - console.log("====================================================="); - console.log(`Test ${test.id} failed`); - console.log(` Expected: "${test.expected_return}"`); - console.log(` Received: "${response}"`); - }; - }; - }; - - - // Check if we are being silent - if (!silent && fail_count > 0) { - console.log("====================================================="); - }; - - - // Output summary - console.log(`Tests: ${fail_count} tests failed out of ${tests.length} tests.`); - console.log(` ${Math.round(((tests.length - fail_count) / tests.length) * 100)}% passed`); - return fail_count; -}; \ No newline at end of file diff --git a/src/services/twitch_handler.ts b/src/services/twitch_handler.ts deleted file mode 100644 index fef6a3e..0000000 --- a/src/services/twitch_handler.ts +++ /dev/null @@ -1,164 +0,0 @@ -// -// twitch_handler.ts -// -// Written by: Oliver Akins (2019/12/10 - 2021/12/02) -// - -/* -tmi.js DOCS: https://github.com/tmijs/docs/blob/gh-pages/_posts/v1.4.2/2019-03-03-Events.md -*/ - - -import { log_error, log } from "../utils/webhook"; -import { HANDLE_MESSAGE } from "../cmd_handler"; -import { LOAD_CONFIG } from "../utils/Config"; -import { PERM } from "../constants"; -import * as tmi from "tmi.js"; - -import { question_index, users } from "../main"; - - -export const run_twitch = (): void => { - - const config: config = LOAD_CONFIG(); - - - // Init Client - const client = tmi.Client({ - options: { - debug: config.DEV, - clientId: config.twitch.CLIENT_ID - }, - connection: { - secure: true, - reconnect: true - }, - identity: { - username: config.twitch.USERNAME, - password: config.twitch.OAUTH_TOKEN - }, - channels: config.twitch.CHANNELS - }); - - - - // SECTION: Handle messages - client.on("message", (channel: string, context: tmi.Userstate, message: string, self: boolean) => { - try { - - // SECTION: Context checking - - // NOTE: Ensure not self - if (self) { return; } - - // NOTE: Ensure text channel, not whisper or anything else weird. - else if (context["message-type"] !== "chat") { return; } - - // !SECTION: Context checking - - - // SECTION: Context parsing - var is_admin = config.twitch.ADMIN.includes(context.username); - var is_mod = ( - context.mod || - context.badges ? context.badges.moderator === "1" : false || - context.badges ? context.badges.broadcaster === "1" : false || - is_admin - ); - - var level = PERM.ALL; - if (is_mod) { level = PERM.MOD; }; - if (is_admin) { level = PERM.ADMIN; }; - // !SECTION: Context parsing - - // Check if the user is voting - let match = message.match(/\((?[A-F])\i)/) - if ( - match // choice found in message - && users[context.username] == null // user not voted yet - && question_index != -1 // quiz started - ) { - let choice: string = (match as any).choice; - - // Update the user's answer - users[context.username] = choice; - - console.log(`Answer ${choice} recorded for ${context.username}`); - return; - }; - - // NOTE: Get response - let response: string = HANDLE_MESSAGE({ - message: message, - channel: `Twitch:${channel}`, - level: level, - source: "Twitch", - user: context.username, - cooldown: true, - test: false - }); - - - // NOTE: Ensure response isn't null - if (response) { - client.say( - channel, - response.replace(/`/g, `"`) - ); - }; - } catch (error) { - log_error({ - "embeds": [ - { - "title": `${error.name}`, - "color": 13238272, - "description": `**Error Message:**\n\`\`\`\n${error.message}\n\`\`\``, - "fields": [ - { - "name": "**Message Context:**", - "value": `\`\`\`\n${JSON.stringify(context, null, 2)}\n\`\`\`` - }, - { - "name": "**Message Content:**", - "value": `\`\`\`json\n${message}\`\`\`` - }, - { - "name": "**Is Mod:**", - "value": `\`${is_mod}\``, - "inline": true - }, - { - "name": "**Is Admin:**", - "value": `\`${is_admin}\``, - "inline": true - }, - { - "name": "**Channel:**", - "value": `\`${channel}\``, - "inline": true - }, - { - "name": "Source", - "value": "Twitch", - "inline": true - } - ] - } - ] - }); - } - }); - // !SECTION - - - client.on("disconnected", (reason: string) => { - log({ msg: `* Disconnected from Twitch w/ reason: ${reason}` }); - }); - - - client.on("connected", (addr: string, port: number) => { - log({ msg: `* Connected to Twitch on \`${addr}:${port}\`` }); - }); - - client.connect().catch((_) => {}) -}; \ No newline at end of file diff --git a/src/types/command_metadata.d.ts b/src/types/command_metadata.d.ts deleted file mode 100644 index fa032d9..0000000 --- a/src/types/command_metadata.d.ts +++ /dev/null @@ -1,42 +0,0 @@ -// -// command_metadata.d.ts -// -// Written by: Oliver Akins (2019/11/06 - 2020/01/04) -// - - -type CONFIRM_TYPE = "confirm"|"deny"|"no_match"|"expired"|"invalid"; - - - -interface positions { - head?: string; - head_2?: string; - table_head?: string; - table_foot?: string; - usage?: string; -} - -interface alert_structure { - info?: positions; - warn?: positions; - error?: positions; -} - - - -interface cmd_metadata { - executable(context: msg_data, args: string[]): string; - flags: {[key: string]: string}; - requires_confirm: boolean; - alerts?: alert_structure; - case_sensitive: boolean; - description: string; - keywords?: string[][]; - arg_info: string[]; - opt_args: number; - group?: string; - args: string[]; - level: number; - name?: string; -} \ No newline at end of file diff --git a/src/types/config.d.ts b/src/types/config.d.ts deleted file mode 100644 index 902e816..0000000 --- a/src/types/config.d.ts +++ /dev/null @@ -1,58 +0,0 @@ -// -// config.d.ts -// -// Written by: Oliver Akins (2019/11/07 - 2020/07/21) -// - -interface auth_options { - OAUTH_TOKEN: string; - SECRET?: string; - CLIENT_ID?: string; - ADMIN: string[]; -} - -interface discord_options extends auth_options { - PERMISSIONS_VALUE: string; - MOD_ROLES: string; - DEV_TOKEN: string; -} -interface twitch_options extends auth_options { - CHANNELS: [string]; - USERNAME: string; -} - - -interface bot_options { - PREFIX: string; - COOLDOWN_TIME: number; - INVALID_PERM_ERROR: boolean; - COOLDOWN_TYPE: "GLOBAL"|"COMMAND"|"SERVICE"; -} - - - -interface web_server_options { - ADDRESS: string; - ROOT: string; - PORT: number; -} - - - -interface config { - DEV: boolean; - twitch: twitch_options; - discord: discord_options; - bot: bot_options; - webhooks: { - ENABLED: boolean; - LOGGING: string; - ERROR?: string; - TWITCH_MISSED_BITS?: string; - }; - web: web_server_options; - extra: { [index: string]: any } -} - - -type WEBHOOK_TYPE = "LOGGING"|"ERROR"|"TWITCH_MISSED_BITS"; \ No newline at end of file diff --git a/src/types/message_metadata.d.ts b/src/types/message_metadata.d.ts deleted file mode 100644 index 2d73a01..0000000 --- a/src/types/message_metadata.d.ts +++ /dev/null @@ -1,20 +0,0 @@ -// -// message_metadata.d.ts -// -// Written by: Oliver Akins (2019/11/07 - 2020/01/04) -// - - -type platform = "Discord"|"Twitch" - - -interface msg_data { - cooldown: boolean; - source: platform; - channel: string; - message: string; - flags?: string[]; - level: number; - test: boolean; - user: string; -} \ No newline at end of file diff --git a/src/types/option.d.ts b/src/types/option.d.ts deleted file mode 100644 index 8a39eef..0000000 --- a/src/types/option.d.ts +++ /dev/null @@ -1,17 +0,0 @@ -// -// option.d.ts -// -// Written by: Oliver Akins (2019/11/11 - 2020/01/04) -// - - -interface option { - aliases: string[]; - name: string; - points: { - [key: string]: number - }; - total: number; - data_version: string; - hidden: boolean; -} \ No newline at end of file diff --git a/src/types/permission_levels.d.ts b/src/types/permission_levels.d.ts deleted file mode 100644 index d135add..0000000 --- a/src/types/permission_levels.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -// -// permission_levels.d.ts -// -// Written by: Oliver Akins (2019/11/07 - 2019/11/07) -// - -interface perms { - ALL: number; - MOD: number; - ADMIN: number; -} \ No newline at end of file diff --git a/src/types/test_object.d.ts b/src/types/test_object.d.ts deleted file mode 100644 index 4a8d263..0000000 --- a/src/types/test_object.d.ts +++ /dev/null @@ -1,24 +0,0 @@ -// -// test_object.ts -// -// Written by: Oliver Akins (2019/11/17 - 2020/01/10) -// - - -interface test_msg_data { - source: platform; - channel?: string; - message: string; - level: number; -} - - -interface test { - datafile_should_exist: "EXISTS"|"NOT_EXISTS"|"IGNORES"; - datafile_populated?: boolean; - confirm_msg?: test_msg_data; - expected_return: string; - msg_meta: test_msg_data; - links: {[key: string]: string} - id: string; -} \ No newline at end of file diff --git a/src/types/webhooks.d.ts b/src/types/webhooks.d.ts deleted file mode 100644 index 7e63bca..0000000 --- a/src/types/webhooks.d.ts +++ /dev/null @@ -1,14 +0,0 @@ -// -// webhooks.d.ts -// -// Written by: Oliver Akins (2019/11/11) -// - - -interface log_data { - msg: string; - title?: string; - embed?: boolean; - fields?: object; - no_stdout?: boolean; -} \ No newline at end of file diff --git a/src/utils/Command.ts b/src/utils/Command.ts deleted file mode 100644 index ccca7f4..0000000 --- a/src/utils/Command.ts +++ /dev/null @@ -1,159 +0,0 @@ -// -// Command.ts -// -// Written by: Oliver Akins (2019/11/06 - 2020/07/21) -// - - -import { LOAD_CONFIG } from "./Config"; - - -export class Command { - - readonly requires_confirm: boolean; - readonly case_sensitive: boolean; - readonly keywords: string[][]; - readonly arg_list: string[]; - readonly arg_info: string[]; - readonly mand_args: number; - readonly full_name: string; - readonly opt_args: number; - readonly syntax: string; - readonly level: number; - readonly group: string; - readonly flags: object; - readonly info: string; - readonly name: string; - - readonly alert: alert_structure; - - private _func: (context: msg_data, args: string[]) => string; - - public last_ran: number; - - - constructor (metadata: cmd_metadata) { - this.mand_args = metadata.args.length - metadata.opt_args; - this.case_sensitive = metadata.case_sensitive; - this.opt_args = metadata.opt_args; - this.info = metadata.description; - this._func = metadata.executable; - this.arg_list = metadata.args; - this.group = metadata.group; - this.level = metadata.level; - this.name = metadata.name; - this.keywords = metadata.keywords || []; - this.alert = metadata.alerts; - this.requires_confirm = metadata.requires_confirm; - this.full_name = this.group ? `${this.group} ${this.name}` : `${this.name}`; - this.arg_info = metadata.arg_info; - this.flags = metadata.flags; - - - // NOTE: Create syntax dynamically - let config: config = LOAD_CONFIG(); - - this.syntax = config.bot.PREFIX; - this.syntax += this.group ? `${this.group} ${this.name}` : `${this.name}`; - if (this.arg_list.length > 0) { - this.syntax += ` ${this.arg_list.join(" ")}`; - } - }; - - - // Does the user's message match a command - public matches (message: string): boolean { - - const config: config = LOAD_CONFIG(); - - - // Only check for name/group match if the name is specified - if (this.name != null) { - - // Construct the regex - let regex: string = `^${config.bot.PREFIX}`; - if (this.group != null) { regex += `(${this.group}\ )`; }; - regex += `${this.name}`; - - if (message.match(new RegExp(regex)) != null) { - return true; - }; - }; - - // Compare the keyword sets to the message - for (var keyword_set of this.keywords) { - let all_included: boolean = true; - - // Check that each keyword is in the message - for (var keyword of keyword_set) { - if (!message.includes(keyword)) { - all_included = false; - break; - }; - }; - if (all_included) { return true; }; - } - return false; - }; - - - public execute (ctx: msg_data, args: string[]): string { - return this._func(ctx, args) - }; -}; - - - - -export class Confirmation { - - readonly username: string; - readonly channel: string; - - private data: any; - private created: number; - private timeout: number; - private callback: (type: CONFIRM_TYPE, data?: any) => string; - - - constructor ( - username: string, - channel: string, - timeout: number, - cb: (type: CONFIRM_TYPE, data?: any) => string, - data?: any - ) { - this.username = username; - this.channel = channel; - this.created = Date.now(); - this.callback = cb; - this.timeout = timeout * 1000; - this.data = data; - }; - - - - public matches (user: string, channel: string, msg: string): CONFIRM_TYPE { - - const config: config = LOAD_CONFIG(); - - - // Timeout checking - if (Date.now() - this.created > this.timeout) { return "expired"; } - - // basic user checking - else if (this.username !== user) { return "no_match"; } - else if (this.channel !== channel) { return "no_match"; } - - // Positive or negative match? - else if (msg.match(`^${config.bot.PREFIX}[Yy](es)?$`)) { return "confirm"; } - else if (msg.match(`^${config.bot.PREFIX}[Nn](o)?$`)) { return "deny"; } - - // Not valid - else { return "invalid"; }; - }; - - public run (type: CONFIRM_TYPE): string { - return this.callback(type, this.data) - } -}; \ No newline at end of file diff --git a/src/utils/Commands.ts b/src/utils/Commands.ts deleted file mode 100644 index 78ba635..0000000 --- a/src/utils/Commands.ts +++ /dev/null @@ -1,96 +0,0 @@ -// -// Commands.ts -// -// Written by: Oliver Akins (2021/02/09) -// - -import { Command } from "./Command"; - -type commandTree = {[index: string]: Command|commandTree}; -type invalidArgument = { provided: string, allowed: string[] }; - - -export class Commands { - /** - * The tree of command objects which have been registered. - */ - private static commands: commandTree = {}; - private static channelCommands: {[index: string]: commandTree} = {}; - - /** - * Finds the command object that matches the message if there is one, this - * can return null if no command at all is able to be found. If - * - * @param cmd - */ - public findCommand(cmd: string): Command | string { - let resp = this.findCommandRecurse(cmd.split(` `), Commands.commands); - - // Check what type of response to provide - if (resp instanceof Command) { - return resp; - } else { - return `Invalid argument "${resp.provided}", possible options: ${resp.allowed.join(`, `)}`; - } - }; - - - /** - * Find the command that is attempting to be ran if there are any channel - * specific commands which have been registered. - * - * @param cmd - The message that we are inspecting for a command - * @param channel - The channel that we are looking for commands for - */ - public findChannelCommand(cmd: string, channel: string): Command | string { - - // Assert that we have a commandsTree for that channel - if (!Commands.channelCommands[channel]) { - return null; - }; - - let resp = this.findCommandRecurse( - cmd.split(` `), - Commands.channelCommands[channel] - ); - - // Check what type of response to provide - if (resp instanceof Command) { - return resp; - } else { - return `Invalid argument "${resp.provided}", possible options: ${resp.allowed.join(`, `)}`; - } - } - - - /** - * The helper function to be able to find the proper command object or the - * arguments that are valid at that point. - * - * @param command - The array of strings that the user has in their message - * for parsing the command arguments. - * @param tree - The commands object we are checking for properties - */ - private findCommandRecurse( - command: string[], - tree: commandTree - ): Command|invalidArgument { - // BASE CASE: The subcommand isn't found or the command ran out of text - if (command.length == 0 || tree[command[0]] == null){ - return { - provided: command[0], - allowed: Object.keys(tree), - }; - }; - - // BASE CASE: The command has been found - if (tree[command[0]] instanceof Command) { - return tree[command[0]] as Command; - }; - - // RECURSIVE CASE: check the subcommand tree - return this.findCommandRecurse(command.slice(1), tree[command[0]] as commandTree); - }; - - public static registerCommand() {}; -} \ No newline at end of file diff --git a/src/utils/Config.ts b/src/utils/Config.ts deleted file mode 100644 index 56569e3..0000000 --- a/src/utils/Config.ts +++ /dev/null @@ -1,31 +0,0 @@ -// -// Config.ts -// -// Written by: Oliver Akins (2019/11/07 - 2019/12/29) -// - - -import { readFileSync, writeFile } from "fs" - - - -export const LOAD_CONFIG = (): config => { - let config = require.resolve("../../config.json"); - - let data = readFileSync(config); - - // @ts-ignore - return JSON.parse(data); -}; - - - -export const UPDATE_CONFIG = (data: config): void => { - writeFile( - require.resolve("../../config.json"), - JSON.stringify(data, null, 2), - () => { - console.log("* [Config] Config written to."); - } - ) -}; \ No newline at end of file diff --git a/src/utils/flags.ts b/src/utils/flags.ts deleted file mode 100644 index a03ea86..0000000 --- a/src/utils/flags.ts +++ /dev/null @@ -1,38 +0,0 @@ -// -// flags.ts -// -// Written by: Oliver Akins (2020/01/04 - 2020/01/31) -// - - -import { FLAG_INDICATOR } from "../constants"; - - - -export const GET_FLAGS = (msg: string): string[] => { - - // Array of flags included in the message - let flags: string[] = [] - - - // Check each parameter of the message - for (var argument of msg.split(" ")) { - - // Check if the argument is indicated as a flag - if (argument.startsWith(FLAG_INDICATOR)) { - - // Check each flag - for (var temp_flag of argument.slice(FLAG_INDICATOR.length)) { - - temp_flag = temp_flag.toLowerCase(); - - // Only add flags to the array once - if (!flags.includes(temp_flag)) { - flags.push(temp_flag); - }; - }; - }; - }; - - return flags; -} \ No newline at end of file diff --git a/src/utils/sorting.ts b/src/utils/sorting.ts deleted file mode 100644 index 3e1d209..0000000 --- a/src/utils/sorting.ts +++ /dev/null @@ -1,39 +0,0 @@ -// -// sorting.ts -// -// Written by: Oliver Akins (2019/11/29 - 2019/12/13) -// - - -import { Command } from "./Command"; - - -export const SORT_OPTIONS = (data: option[]): option[] => { - return data.sort((a: option, b: option): number => { - let a_total: number = a.total; - let b_total: number = b.total; - - - if (a_total < b_total) { return 1; } - - else if (a_total > b_total) { return -1; } - - else { return 0; }; - }); -}; - - - -export const SORT_COMMANDS = (data: Command[]): Command[] => { - return data.sort((a: Command, b: Command): number => { - let a_name: string = a.full_name; - let b_name: string = b.full_name; - - - if (a_name < b_name) { return -1; } - - else if (a_name > b_name) { return 1; } - - else { return 0; }; - }); -}; \ No newline at end of file diff --git a/src/utils/tests.ts b/src/utils/tests.ts deleted file mode 100644 index c2086d4..0000000 --- a/src/utils/tests.ts +++ /dev/null @@ -1,31 +0,0 @@ -// -// tests.ts -// -// Written by: Oliver Akins (2019/11/17 - 2020/01/10) -// - - -import { PERM, VERSION, TEST_CHANNEL, REPO } from "../constants"; -import { LOAD_CONFIG } from "./Config"; - - -const config: config = LOAD_CONFIG(); - -export const SEND_INVALID_PERM: boolean = config.bot.INVALID_PERM_ERROR -export const PREFIX: string = config.bot.PREFIX; - - -export let tests: test[] = [ - { - id: `general:01`, - links: {}, - datafile_should_exist: `IGNORES`, - msg_meta: { - source: `Twitch`, - message: `potato salad`, - level: PERM.ALL, - channel: TEST_CHANNEL - }, - expected_return: null - } -]; \ No newline at end of file diff --git a/src/utils/webhook.ts b/src/utils/webhook.ts deleted file mode 100644 index 71f01e8..0000000 --- a/src/utils/webhook.ts +++ /dev/null @@ -1,80 +0,0 @@ -// -// webhook.ts -// -// Written by: Oliver Akins (2019/11/11 - 2019/12/19) -// - - -import * as requests from "request-promise-native"; -import { LOAD_CONFIG } from "./Config"; - - -const config: config = LOAD_CONFIG(); - - - -export const log = (context: log_data) => { - - - // Should we output the data to the console, ensure the data is console-outputable - if (config.DEV && !context.no_stdout) { - console.log(context.msg); - }; - - // Are we embedding the response or not? - if (context.embed) { - let payload = { - "content": "Log Entry:", - "embeds": [ - { - color: 43520, - title: context.title, - description: context.msg, - fields: [] - } - ] - }; - - // Add fields - for (var field in context.fields) { - payload.embeds[0].fields.push({ - name: field, - value: context.fields[field], - inline: true - }); - }; - - push(payload, "LOGGING") - } else { - push({ - "content": context.msg - }, "LOGGING"); - }; -}; - - - -export const log_error = (payload: any) => { - push(payload, "ERROR"); -}; - - - -export const push = (payload: any, webhook: WEBHOOK_TYPE) => { - - // Output to stdout? - if (payload.content && !payload.no_stdout && payload.no_stdout != undefined) { - console.log(payload.content) - }; - - // Don't try to execute webhook if they aren't enabled - if (!config.webhooks.ENABLED) { return; } - - requests.post({ - uri: config.webhooks[webhook], - body: payload, - json: true - }).catch((_: any) => { - console.error("OHNO, Shit Went DOWWWNNNNNN"); - }); -}; \ No newline at end of file