From de5fec707926d8be3fbbae01abb2ea9c6540e212 Mon Sep 17 00:00:00 2001 From: AnxietyisReal <96593068+AnxietyisReal@users.noreply.github.com> Date: Sat, 20 Jan 2024 23:13:18 +1100 Subject: [PATCH] Improvements --- src/commands/dev.ts | 5 +++-- src/commands/{prohibitedWords.ts => pw.ts} | 14 -------------- src/models/prohibitedWords.ts | 20 -------------------- 3 files changed, 3 insertions(+), 36 deletions(-) rename src/commands/{prohibitedWords.ts => pw.ts} (81%) diff --git a/src/commands/dev.ts b/src/commands/dev.ts index fd0226b..8c3be21 100644 --- a/src/commands/dev.ts +++ b/src/commands/dev.ts @@ -6,7 +6,7 @@ import Formatters from '../helpers/Formatters.js'; import GitHub from '../helpers/GitHub.js'; import TClient from '../client.js'; import util from 'node:util'; -import fs from 'node:fs'; +import fs, { writeFileSync } from 'node:fs'; export default class Developer { static run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>) { if (!client.config.whitelist.includes(interaction.user.id)) return MessageTool.youNeedRole(interaction, 'bottech'); @@ -102,6 +102,7 @@ export default class Developer { if (name) currentActivities[0].name = name; if (url) currentActivities[0].url = url; client.user.setPresence(client.config.botPresence); + writeFileSync(process.argv[2] ?? 'src/config.json', JSON.stringify(client.config, null, 2)); interaction.reply(MessageTool.concatMessage( 'Presence updated:', `Status: **${client.config.botPresence.status}**`, @@ -157,7 +158,7 @@ export default class Developer { .setRequired(false))) .addSubcommand(x=>x .setName('restart') - .setDescription('Restart the bot for technical reasons')) + .setDescription('Restart the bot for manual changes/technical difficulties')) .addSubcommand(x=>x .setName('update') .setDescription('Pull from repository and restart') diff --git a/src/commands/prohibitedWords.ts b/src/commands/pw.ts similarity index 81% rename from src/commands/prohibitedWords.ts rename to src/commands/pw.ts index f8b647e..2630fb2 100644 --- a/src/commands/prohibitedWords.ts +++ b/src/commands/pw.ts @@ -47,13 +47,6 @@ export default class ProhibitedWords { ] }); } - }, - import: async()=>{ - const file = interaction.options.getAttachment('file', true); - if (!file.contentType.match(/application\/json/)) return interaction.reply({ephemeral: true, content: 'This file is not a JSON file!'}); - const success = await client.prohibitedWords.importWords(file.url); - if (success) interaction.reply({ephemeral: true, content: `Successfully imported the list from \`${file.name}\` into the database`}); - else interaction.reply({ephemeral: true, content: `Failed to import the list from \`${file.name}\` into the database`}); } } as any)[interaction.options.getSubcommand()](); } @@ -80,11 +73,4 @@ export default class ProhibitedWords { .setName('word') .setDescription('Remove the specific word from automod\'s prohibitedWords database') .setRequired(true))) - .addSubcommand(x=>x - .setName('import') - .setDescription('Import a JSON file of words into the database') - .addAttachmentOption(x=>x - .setName('file') - .setDescription('The JSON file to import') - .setRequired(true))) } diff --git a/src/models/prohibitedWords.ts b/src/models/prohibitedWords.ts index c984d20..eb460bc 100644 --- a/src/models/prohibitedWords.ts +++ b/src/models/prohibitedWords.ts @@ -1,6 +1,5 @@ import DatabaseServer from '../components/DatabaseServer.js'; import {Model, DataTypes} from 'sequelize'; -import {get} from 'node:https'; class prohibitedWords extends Model { declare public word: string; @@ -27,25 +26,6 @@ export class ProhibitedWordsSvc { this.model.sync(); } findWord = async(word:string)=>await this.model.findByPk(word); - async importWords(file:string) { - const jsonData = await new Promise((resolve, reject)=>{ - get(file, res=>{ - let data = ''; - res.on('data', chunk=>data += chunk); - res.on('end', ()=>resolve(data)); - res.on('error', reject); - }) - }); - - const data = JSON.parse(jsonData); - const dataMapping = data.map((x:string)=>({word: x})); - try { - await this.model.bulkCreate(dataMapping, {ignoreDuplicates: true}); - return true; - } catch(err) { - throw new Error(`Failed to insert words into Postgres database: ${err.message}`) - } - } getAllWords = async()=>await this.model.findAll(); insertWord = async(word:string)=>await this.model.create({word: word}); removeWord = async(word:string)=>await this.model.destroy({where: {word: word}})