Archived
0
0
Fork 0

feat: redone code to my framework & slash cmds

This commit is contained in:
Daryl Ronningen 2021-11-01 21:06:31 -07:00
parent 3a63cadfc7
commit 5648d36906
Signed by: Daryl Ronningen
GPG key ID: FD23F0C934A5EC6B
6 changed files with 347 additions and 69 deletions

View file

@ -0,0 +1,12 @@
{
"db": {
"host": "",
"port": "0000",
"user": "",
"password": "",
"database": ""
},
"logLevel": "",
"ownerId": "",
"token": "",
}

View file

@ -16,7 +16,7 @@
"scripts": {
"clean": "rimraf dist",
"compile": "tsc",
"commit": "commit",
"commit": "cz",
"lint": "eslint --format=pretty src/**/*.ts",
"lint:save": "eslint --format=pretty --save src/**/*.ts",
"migrate": "node-pg-migrate",
@ -31,7 +31,7 @@
},
"dependencies": {
"@discordjs/rest": "^0.1.0-canary.0",
"@neonjs/framework": "^0.0.1-778b0e3813ed61dfb931f042511e1e1f681adefc.1635794341",
"@neonjs/framework": "^1.0.0",
"bufferutil": "^4.0.5",
"config": "^3.3.6",
"discord-api-types": "^0.24.0",
@ -46,6 +46,7 @@
"sql-template-strings": "^2.2.2",
"terminal-link": "^3.0.0",
"tslib": "^2.3.1",
"typescript": "^4.4.4",
"utf-8-validate": "^5.0.7",
"vm2": "^3.9.5",
"zlib-sync": "^0.1.7"
@ -54,11 +55,14 @@
"@commitlint/cli": "^14.1.0",
"@commitlint/config-conventional": "^14.1.0",
"@commitlint/cz-commitlint": "^14.1.0",
"@types/config": "^0.0.40",
"@types/figlet": "^1.5.4",
"@types/gradient-string": "^1.1.2",
"@types/i18next-fs-backend": "^1.1.2",
"@types/inquirer": "^8.1.3",
"@types/luxon": "^2.0.5",
"@types/pg": "^8.6.1",
"@types/pino": "^6.3.12",
"@types/rimraf": "^3.0.2",
"@types/source-map-support": "^0.5.4",
"@typescript-eslint/eslint-plugin": "^5.3.0",
@ -78,7 +82,6 @@
"source-map-support": "^0.5.20",
"standard-version": "^9.3.2",
"ts-sql-plugin": "^0.10.0",
"typescript": "^4.4.4",
"typescript-eslint-language-service": "^4.1.5"
},
"lint-staged": {

102
src/commands/utils/vm.ts Normal file
View file

@ -0,0 +1,102 @@
/*
* This file is part of ArgonBot
*
* ArgonBot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ArgonBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/
import Command from '@neonjs/framework/dist/lib/structures/command';
import type { CacheType, Client, CommandInteraction } from 'discord.js';
import { transpile } from 'typescript';
import { NodeVM } from 'vm2';
export default class extends Command {
public constructor(client: Client, file: string) {
super(client, file, {
args: [
{
name: 'language',
description: 'The language to use',
autocomplete: true,
type: 'STRING',
required: true,
choices: [
{
name: 'javascript',
value: 'javascript',
},
{
name: 'typescript',
value: 'typescript',
},
],
},
{
name: 'code',
description: 'The code to run',
autocomplete: true,
type: 'STRING',
required: true,
},
],
cooldown: 5,
extendedDescription: 'Runs Vanilla JavaScript/TypeScript code direct from Discord in a secure VM',
group: 'utils',
name: 'vm',
shortDescription: 'Runs JavaScript/TypeScript code direct from Discord.',
usage: 'js const test = \'test\'; return test;',
});
}
public async run(interaction: CommandInteraction<CacheType>): Promise<void> {
const code = interaction.options.getString('code', true);
const javascriptVM = new NodeVM({ eval: false, timeout: 1000, wasm: false, console: 'off' });
const vmLang = interaction.options.getString('language', true);
switch (vmLang.toLowerCase()) {
case 'javascript':
await interaction.reply('Computing JavaScript Code...');
try {
let returnVal: string;
if (!code.includes('module.exports = '))
returnVal = await javascriptVM.run(`module.exports = (() => {${code}})();`);
else
returnVal = await javascriptVM.run(`(() => {${code}})()`);
await interaction.editReply(`\`\`\`\n${returnVal}\`\`\``);
} catch (err) {
await interaction.editReply(`An error has occurred! \`\`\`${(err as Error).message}\`\`\``);
}
break;
case 'typescript':
await interaction.reply('Computing TypeScript Code...');
try {
const jsCode = transpile(code);
let returnVal: string;
if (!jsCode.includes('module.exports = '))
returnVal = await javascriptVM.run(`module.exports = (() => {${jsCode}}()`);
else
returnVal = await javascriptVM.run(`(() => {${jsCode}}()`);
await interaction.editReply(`\`\`\`\n${returnVal}\`\`\``);
} catch (err) {
await interaction.editReply(`An error has occurred! \`\`\`${(err as Error).message}\`\`\``);
}
break;
default:
await interaction.reply('This should not happen tf');
}
}
}

65
src/index.ts Normal file
View file

@ -0,0 +1,65 @@
/*
* This file is part of ArgonBot
*
* ArgonBot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ArgonBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/
import { NeonClient } from '@neonjs/framework';
import config from 'config';
import figlet from 'figlet';
import gradient from 'gradient-string';
import i18next from 'i18next';
import Backend from 'i18next-chained-backend';
import FSBackend from 'i18next-fs-backend';
import { connectToDB } from './lib/utils/utils';
const client = new NeonClient(config.get('ownerId'), {
intents: ['GUILDS'],
});
figlet('Argon Bot', (err, data) => {
if (err) client.logger.error(`Figlet encountered an error!\n${err.message}`);
client.logger.info(gradient.rainbow.multiline(`\n${data}`));
});
client.on('ready', async () => {
client.logger.info('Loading translations...');
await i18next.use(Backend).init({
supportedLngs: ['en-US'],
lng: 'en-US',
fallbackLng: 'en-US',
defaultNS: 'common',
fallbackNS: 'common',
ns: ['common', 'commands'],
backend: {
backends: [FSBackend],
backendOptions: [
{
loadPath: `${process.cwd()}/translations/{{lng}}/{{ns}}.json`,
},
],
},
});
client.logger.info('Finished loading translations');
client.logger.info('Creating Database Pool...');
await connectToDB(config.get('db'));
client.logger.info('Finished creating Database Pool');
client.logger.info('Bot is ready!');
});
client.login(config.get('token'));

61
src/lib/utils/utils.ts Normal file
View file

@ -0,0 +1,61 @@
/*
* This file is part of ArgonBot
*
* ArgonBot is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ArgonBot is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ArgonBot. If not, see <https: //www.gnu.org/licenses/>.
*/
import { NeonClient } from '@neonjs/framework';
import pg, { QueryResult } from 'pg';
import type { SQLStatement } from 'sql-template-strings';
let pool: pg.Pool;
export const connectToDB = async (connectionData: pg.PoolConfig): Promise<void> => {
pool = new pg.Pool(connectionData);
pool.on('connect', () => {
NeonClient.logger.info('Connected to the Postgres Database!');
});
pool.on('error', (err) => {
NeonClient.logger.error(`An error has occurred with node-postgres!\n${err.message}`);
});
};
export const query = async <t>(query: SQLStatement): Promise<QueryResult<t> | void> => {
if (!pool) return NeonClient.logger.error('Attempted to query to the Database when a connection has not been made yet... Discarding request.');
else {
const client = await pool.connect();
try {
NeonClient.logger.info('DB: BEGIN');
await client.query('BEGIN');
NeonClient.logger.info(query.text);
const result = await client.query<t>(query);
NeonClient.logger.info('DB: COMMIT');
await client.query('COMMIT');
return result;
} catch (e) {
NeonClient.logger.error(`An error has occurred while attempting to query "${query}"... Rolling back changes`);
NeonClient.logger.info('DB: ROLLBACK');
await client.query('ROLLBACK');
throw e;
} finally {
client.release();
}
}
};

167
yarn.lock
View file

@ -222,11 +222,11 @@ __metadata:
linkType: hard
"@babel/parser@npm:^7.16.0":
version: 7.16.0
resolution: "@babel/parser@npm:7.16.0"
version: 7.16.2
resolution: "@babel/parser@npm:7.16.2"
bin:
parser: ./bin/babel-parser.js
checksum: dac5feeaf03ec9bc075af5c2bd1a401e4123b45a6e4ef7a123d242b16a5cf18cc4bc80b0579327ed1a6c8dcb0e078fff8d66e71e77260a3c0abc236cf2606e16
checksum: e8ceef8214adf55beaae80fff1647ae6535e17af592749a6f3fd3ed1081bbb1474fd88bf4d3470ec8bc0082843a6d23445e9e08b03e91831458acc6fd37d7bc9
languageName: node
linkType: hard
@ -585,18 +585,18 @@ __metadata:
languageName: node
linkType: hard
"@neonjs/framework@npm:^0.0.1-778b0e3813ed61dfb931f042511e1e1f681adefc.1635794341":
version: 0.0.1
resolution: "@neonjs/framework@npm:0.0.1"
"@neonjs/framework@npm:^1.0.0":
version: 1.0.0
resolution: "@neonjs/framework@npm:1.0.0"
dependencies:
"@discordjs/rest": ^0.1.0-canary.0
discord-api-types: ^0.23.1
discord.js: ^13.2.0
discord-api-types: ^0.24.0
discord.js: ^13.3.1
lodash: ^4.17.21
node-fetch: ^3.0.0
pino: ^6.13.3
pino-pretty: ^7.0.1
checksum: 643cd080e313febe4c1e916092e989461f6821467409be16d388189bdd8da81eb28a2f444e07da94f3bee56574cfc603b8188cbde0d8a48bfa367e8445eaa9da
pino: ^7.0.5
pino-pretty: ^7.1.0
checksum: 79d51bf9b429a9c69d3b97b292c806d183c172cdccc43c571b64361d100cea791a0c8569fa5e19947c319d0faf60e9a7d2cb91c3562870f0752c6502945c1c23
languageName: node
linkType: hard
@ -691,6 +691,13 @@ __metadata:
languageName: node
linkType: hard
"@types/config@npm:^0.0.40":
version: 0.0.40
resolution: "@types/config@npm:0.0.40"
checksum: 94e52288d26895c25c6a00cb553f6a19d71e9edd7ecfe0a3f0689c486f559be6cbf400362f14fa0daac5b12bd30e876b8c6aafc5fbd37580c2c3270464c4af59
languageName: node
linkType: hard
"@types/eslint@npm:^7.2.13":
version: 7.28.2
resolution: "@types/eslint@npm:7.28.2"
@ -734,6 +741,15 @@ __metadata:
languageName: node
linkType: hard
"@types/i18next-fs-backend@npm:^1.1.2":
version: 1.1.2
resolution: "@types/i18next-fs-backend@npm:1.1.2"
dependencies:
i18next: ^21.0.1
checksum: 567ab61f65231490b272d15311e7eb127cb069743284d3e0527e7cc75671038dcb70feb5da9964b218a3bb28435365179c18bab721c94ed765f28263662e6de0
languageName: node
linkType: hard
"@types/inquirer@npm:^8.1.3":
version: 8.1.3
resolution: "@types/inquirer@npm:8.1.3"
@ -823,6 +839,36 @@ __metadata:
languageName: node
linkType: hard
"@types/pino-pretty@npm:*":
version: 4.7.1
resolution: "@types/pino-pretty@npm:4.7.1"
dependencies:
"@types/pino": "*"
checksum: 5a8a80bd42eda30c4aa0a322cdc55e8dd88d768b84d2e549802a3f6a2d822824364598ba4e24b3db701d87b0478568b5fe4d036f4146d0a3691fb751ccc83d50
languageName: node
linkType: hard
"@types/pino-std-serializers@npm:*":
version: 2.4.1
resolution: "@types/pino-std-serializers@npm:2.4.1"
dependencies:
"@types/node": "*"
checksum: a156e25882db9aade2576dbe6414379efcdd4fad24211d3f22f20e0cd4bee569215799ee5cd9b2b15282f18461a8a54573ff42bf6bee5d35b72513be2f78bdec
languageName: node
linkType: hard
"@types/pino@npm:*, @types/pino@npm:^6.3.12":
version: 6.3.12
resolution: "@types/pino@npm:6.3.12"
dependencies:
"@types/node": "*"
"@types/pino-pretty": "*"
"@types/pino-std-serializers": "*"
sonic-boom: ^2.1.0
checksum: 801735146669312d02459781e5180220630eaef643da36dc5a9a97520e7ecc3da7270f31a86fcdcb1dc835073c9143fc628024ba5e3a0ea7cbb86aada4897709
languageName: node
linkType: hard
"@types/responselike@npm:^1.0.0":
version: 1.0.0
resolution: "@types/responselike@npm:1.0.0"
@ -1209,12 +1255,15 @@ __metadata:
"@commitlint/config-conventional": ^14.1.0
"@commitlint/cz-commitlint": ^14.1.0
"@discordjs/rest": ^0.1.0-canary.0
"@neonjs/framework": ^0.0.1-778b0e3813ed61dfb931f042511e1e1f681adefc.1635794341
"@neonjs/framework": ^1.0.0
"@types/config": ^0.0.40
"@types/figlet": ^1.5.4
"@types/gradient-string": ^1.1.2
"@types/i18next-fs-backend": ^1.1.2
"@types/inquirer": ^8.1.3
"@types/luxon": ^2.0.5
"@types/pg": ^8.6.1
"@types/pino": ^6.3.12
"@types/rimraf": ^3.0.2
"@types/source-map-support": ^0.5.4
"@typescript-eslint/eslint-plugin": ^5.3.0
@ -2309,20 +2358,13 @@ __metadata:
languageName: node
linkType: hard
"detect-indent@npm:6.0.0":
"detect-indent@npm:6.0.0, detect-indent@npm:^6.0.0":
version: 6.0.0
resolution: "detect-indent@npm:6.0.0"
checksum: 0c38f362016e2d07af1c65b1ecd6ad8a91f06bfdd11383887c867a495ad286d04be2ab44027ac42444704d523982013115bd748c1541df7c9396ad76b22aaf5d
languageName: node
linkType: hard
"detect-indent@npm:^6.0.0":
version: 6.1.0
resolution: "detect-indent@npm:6.1.0"
checksum: ab953a73c72dbd4e8fc68e4ed4bfd92c97eb6c43734af3900add963fd3a9316f3bc0578b018b24198d4c31a358571eff5f0656e81a1f3b9ad5c547d58b2d093d
languageName: node
linkType: hard
"detect-newline@npm:^3.1.0":
version: 3.1.0
resolution: "detect-newline@npm:3.1.0"
@ -2353,13 +2395,6 @@ __metadata:
languageName: node
linkType: hard
"discord-api-types@npm:^0.23.1":
version: 0.23.1
resolution: "discord-api-types@npm:0.23.1"
checksum: e2ea574d51bff9f74770194338ba2b981072c6814a4da4c1e9a6b09b5b6bcf4ecf9044397fd4d2d3bf245f7573f22f1c25ff3f57cfc23ed87dfee4629091c29d
languageName: node
linkType: hard
"discord-api-types@npm:^0.24.0":
version: 0.24.0
resolution: "discord-api-types@npm:0.24.0"
@ -2367,7 +2402,7 @@ __metadata:
languageName: node
linkType: hard
"discord.js@npm:^13.2.0":
"discord.js@npm:^13.3.1":
version: 13.3.1
resolution: "discord.js@npm:13.3.1"
dependencies:
@ -2842,7 +2877,7 @@ __metadata:
languageName: node
linkType: hard
"fast-safe-stringify@npm:^2.0.7, fast-safe-stringify@npm:^2.0.8":
"fast-safe-stringify@npm:^2.0.7":
version: 2.1.1
resolution: "fast-safe-stringify@npm:2.1.1"
checksum: a851cbddc451745662f8f00ddb622d6766f9bd97642dabfd9a405fb0d646d69fc0b9a1243cbf67f5f18a39f40f6fa821737651ff1bceeba06c9992ca2dc5bd3d
@ -2994,13 +3029,6 @@ __metadata:
languageName: node
linkType: hard
"flatstr@npm:^1.0.12":
version: 1.0.12
resolution: "flatstr@npm:1.0.12"
checksum: e1bb562c94b119e958bf37e55738b172b5f8aaae6532b9660ecd877779f8559dbbc89613ba6b29ccc13447e14c59277d41450f785cf75c30df9fce62f459e9a8
languageName: node
linkType: hard
"flatted@npm:^3.1.0":
version: 3.2.2
resolution: "flatted@npm:3.2.2"
@ -3250,7 +3278,7 @@ __metadata:
languageName: node
linkType: hard
"glob@npm:7.1.4":
"glob@npm:7.1.4, glob@npm:^7.1.3":
version: 7.1.4
resolution: "glob@npm:7.1.4"
dependencies:
@ -3264,7 +3292,7 @@ __metadata:
languageName: node
linkType: hard
"glob@npm:^7.1.3, glob@npm:^7.1.4":
"glob@npm:^7.1.4":
version: 7.2.0
resolution: "glob@npm:7.2.0"
dependencies:
@ -3542,7 +3570,7 @@ __metadata:
languageName: node
linkType: hard
"i18next@npm:^21.3.3":
"i18next@npm:^21.0.1, i18next@npm:^21.3.3":
version: 21.3.3
resolution: "i18next@npm:21.3.3"
dependencies:
@ -4606,14 +4634,14 @@ __metadata:
languageName: node
linkType: hard
"ms@npm:2.1.2":
"ms@npm:2.1.2, ms@npm:^2.1.1":
version: 2.1.2
resolution: "ms@npm:2.1.2"
checksum: 673cdb2c3133eb050c745908d8ce632ed2c02d85640e2edb3ace856a2266a813b30c613569bf3354fdf4ea7d1a1494add3bfa95e2713baa27d0c2c71fc44f58f
languageName: node
linkType: hard
"ms@npm:^2.0.0, ms@npm:^2.1.1":
"ms@npm:^2.0.0":
version: 2.1.3
resolution: "ms@npm:2.1.3"
checksum: aa92de608021b242401676e35cfa5aa42dd70cbdc082b916da7fb925c542173e36bce97ea3e804923fe92c0ad991434e4a38327e15a1b5b5f945d66df615ae6d
@ -4866,6 +4894,13 @@ __metadata:
languageName: node
linkType: hard
"on-exit-leak-free@npm:^0.2.0":
version: 0.2.0
resolution: "on-exit-leak-free@npm:0.2.0"
checksum: d22b0f0538069110626b578db6e68b6ee0e85b1ee9cc5ef9b4de1bba431431d6a8da91a61e09d2ad46f22a96f968e5237833cb9d0b69bc4d294f7ec82f609b05
languageName: node
linkType: hard
"once@npm:^1.3.0, once@npm:^1.3.1, once@npm:^1.4.0":
version: 1.4.0
resolution: "once@npm:1.4.0"
@ -5246,7 +5281,7 @@ __metadata:
languageName: node
linkType: hard
"pino-abstract-transport@npm:^0.4.0":
"pino-abstract-transport@npm:^0.4.0, pino-abstract-transport@npm:v0.4.0":
version: 0.4.0
resolution: "pino-abstract-transport@npm:0.4.0"
dependencies:
@ -5256,7 +5291,7 @@ __metadata:
languageName: node
linkType: hard
"pino-pretty@npm:^7.0.1":
"pino-pretty@npm:^7.1.0":
version: 7.1.0
resolution: "pino-pretty@npm:7.1.0"
dependencies:
@ -5278,27 +5313,30 @@ __metadata:
languageName: node
linkType: hard
"pino-std-serializers@npm:^3.1.0":
version: 3.2.0
resolution: "pino-std-serializers@npm:3.2.0"
checksum: 77e29675b116e42ae9fe6d4ef52ef3a082ffc54922b122d85935f93ddcc20277f0b0c873c5c6c5274a67b0409c672aaae3de6bcea10a2d84699718dda55ba95b
"pino-std-serializers@npm:^4.0.0":
version: 4.0.0
resolution: "pino-std-serializers@npm:4.0.0"
checksum: 89d487729b58c9d3273a0ee851ead068d6d2e2ccc1af8e1c1d28f1b3442423679bec7ec04d9a2aba36f94f335e82be9f4de19dc4fbc161e71c136aaa15b85ad3
languageName: node
linkType: hard
"pino@npm:^6.13.3":
version: 6.13.3
resolution: "pino@npm:6.13.3"
"pino@npm:^7.0.5":
version: 7.0.5
resolution: "pino@npm:7.0.5"
dependencies:
fast-redact: ^3.0.0
fast-safe-stringify: ^2.0.8
fastify-warning: ^0.2.0
flatstr: ^1.0.12
pino-std-serializers: ^3.1.0
get-caller-file: ^2.0.5
json-stringify-safe: ^5.0.1
on-exit-leak-free: ^0.2.0
pino-abstract-transport: v0.4.0
pino-std-serializers: ^4.0.0
quick-format-unescaped: ^4.0.3
sonic-boom: ^1.0.2
sonic-boom: ^2.2.1
thread-stream: ^0.11.1
bin:
pino: bin.js
checksum: a580decd47a1c8b32a846ba1cb478087b523636d697bd4c57833d10b3f2b35c7d06739715ad9a291b41caf002b8d1bbf98674bfb3e99989fd41b7d934cca861c
checksum: 569011202351e1ea00e56a776333194fd681608e5a8a908e3bc4f010da8c21c668d1e1cb20acde2575ae5031d0b73064b780c4078a3bcd72d395f313750ee7e8
languageName: node
linkType: hard
@ -5938,17 +5976,7 @@ __metadata:
languageName: node
linkType: hard
"sonic-boom@npm:^1.0.2":
version: 1.4.1
resolution: "sonic-boom@npm:1.4.1"
dependencies:
atomic-sleep: ^1.0.0
flatstr: ^1.0.12
checksum: 189fa8fe5c2dc05d3513fc1a4926a2f16f132fa6fa0b511745a436010cdcd9c1d3b3cb6a9d7c05bd32a965dc77673a5ac0eb0992e920bdedd16330d95323124f
languageName: node
linkType: hard
"sonic-boom@npm:^2.2.0":
"sonic-boom@npm:^2.1.0, sonic-boom@npm:^2.2.0, sonic-boom@npm:^2.2.1":
version: 2.3.0
resolution: "sonic-boom@npm:2.3.0"
dependencies:
@ -6325,6 +6353,13 @@ __metadata:
languageName: node
linkType: hard
"thread-stream@npm:^0.11.1":
version: 0.11.2
resolution: "thread-stream@npm:0.11.2"
checksum: ca5372b3029ffee24eb928c8d597c5ece114d07c1812a2e2c6a0fc00e4d5f3fad747b84c9a69e8805bfd3c930793378f0c4048a3f5d99ddbb9ee79576e623cdf
languageName: node
linkType: hard
"through2@npm:^2.0.0":
version: 2.0.5
resolution: "through2@npm:2.0.5"