From 41f1876bfc6297902e9bee974212f2396e48ac8f Mon Sep 17 00:00:00 2001 From: BluDood Date: Thu, 3 Nov 2022 20:50:02 +0100 Subject: [PATCH] Refactoring, moved/removed some files --- .gitignore | 1 - autocomplete/tag.js | 11 ++--- commands/avatar.js | 2 +- commands/checkPerms.js | 26 ------------ commands/define.js | 4 +- commands/yourmom.js | 2 +- index.js | 10 ++--- package.json | 2 +- utils/config.js | 2 +- console.js => utils/console.js | 7 ++-- utils/deploy.js | 5 +-- utils/moderation.js | 76 ---------------------------------- utils/reactionroles.js | 3 +- setup.js => utils/setup.js | 0 14 files changed, 21 insertions(+), 130 deletions(-) delete mode 100644 commands/checkPerms.js rename console.js => utils/console.js (93%) delete mode 100644 utils/moderation.js rename setup.js => utils/setup.js (100%) diff --git a/.gitignore b/.gitignore index 21078b3..f4bbf3e 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,4 @@ package-lock.json old/ databases/* config.json - .vscode/ \ No newline at end of file diff --git a/autocomplete/tag.js b/autocomplete/tag.js index c4f7ae1..a25fb65 100644 --- a/autocomplete/tag.js +++ b/autocomplete/tag.js @@ -3,12 +3,9 @@ const fs = require('fs') module.exports = { id: 'tag', async execute(interaction) { - // Auto complete the tag command - if (interaction.options.getSubcommand() === 'remove' || interaction.options.getSubcommand() === 'get') { - const tags = JSON.parse(fs.readFileSync('./databases/tags.json')) - let tagNames = Object.keys(tags) - tagNames = tagNames.filter(name => name.startsWith(interaction.options.getFocused())) - return interaction.respond(tagNames.map(tag => ({ name: tag, value: tag }))) - } + if (interaction.options.getSubcommand() !== 'remove' || interaction.options.getSubcommand() !== 'get') return + const tags = JSON.parse(fs.readFileSync('./databases/tags.json')) + const tagNames = Object.keys(tags).filter(name => name.startsWith(interaction.options.getFocused())) + return interaction.respond(tagNames.map(tag => ({ name: tag, value: tag }))) } } diff --git a/commands/avatar.js b/commands/avatar.js index a46c530..e5b36a9 100644 --- a/commands/avatar.js +++ b/commands/avatar.js @@ -5,7 +5,7 @@ module.exports = { data: new SlashCommandBuilder() .setName('avatar') .setDescription("Get your or another user's avatar") - .addUserOption(option => option.setName('target').setDescription('User to show avatar for')), + .addUserOption(option => option.setName('target').setDescription('User to get avatar for')), async execute(interaction) { const user = interaction.options.getUser('target') || interaction.user const avatar = format => user.avatarURL({ format }) diff --git a/commands/checkPerms.js b/commands/checkPerms.js deleted file mode 100644 index 59829d6..0000000 --- a/commands/checkPerms.js +++ /dev/null @@ -1,26 +0,0 @@ -const { SlashCommandBuilder } = require('discord.js') -const config = require('../utils/config') -const checkUserPerms = require('../utils/checkUserPerms') - -module.exports = { - data: new SlashCommandBuilder().setName('check').setDescription('Check if are allowed to moderate using this bot.'), - async execute(interaction) { - if (checkUserPerms(interaction)) - return interaction.reply({ - embeds: [ - { - title: `You are allowed to moderate using this bot!`, - color: config.getColor('accent') - } - ] - }) - return interaction.reply({ - embeds: [ - { - title: `You are not allowed to moderate using this bot.`, - color: config.getColor('accent') - } - ] - }) - } -} diff --git a/commands/define.js b/commands/define.js index cf91a48..0db85d7 100644 --- a/commands/define.js +++ b/commands/define.js @@ -1,6 +1,6 @@ const { SlashCommandBuilder } = require('discord.js') const config = require('../utils/config') -const axios = require('axios').default +const { default: axios } = require('axios') module.exports = { data: new SlashCommandBuilder() @@ -8,7 +8,7 @@ module.exports = { .setDescription('Define a word with Urban Dictionary!') .addStringOption(option => option.setName('query').setDescription('Word to search for').setRequired(true)), async execute(interaction) { - // my api ;) + // https://guides.bludood.com/apis/urban-dictionary-api const api = 'https://urbanapi.up.railway.app' const query = interaction.options.getString('query') interaction.deferReply() diff --git a/commands/yourmom.js b/commands/yourmom.js index c9264b2..a9a7367 100644 --- a/commands/yourmom.js +++ b/commands/yourmom.js @@ -1,5 +1,5 @@ const { SlashCommandBuilder } = require('discord.js') -const axios = require('axios').default +const { default: axios } = require('axios') const config = require('../utils/config') module.exports = { diff --git a/index.js b/index.js index 4605206..a4fc228 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const { Client, Collection, GatewayIntentBits, Events } = require('discord.js') const fs = require('fs') const deploy = require('./utils/deploy') -const bconsole = require('./console') +const con = require('./utils/console') const { cacheAll } = require('./utils/reactionroles') const config = require('./utils/config') const { token } = config.get() @@ -40,16 +40,14 @@ for (const eventFile of fs.readdirSync('./events').filter(file => file.endsWith( client.on(event.event, event.listener) } -bconsole.init(process.argv[2]) +con.init() client.once(Events.ClientReady, async c => { - bconsole.motd(c.user.tag) + con.motd(c.user.tag) deploy(c.user.id) cacheAll(client) }) -client.on(Events.Error, error => { - console.log(error) -}) +client.on(Events.Error, err => console.error(err)) client.on(Events.InteractionCreate, async interaction => { if (interaction.isCommand()) { diff --git a/package.json b/package.json index 54d9b2f..fbe3531 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "start": "node index.js", - "setup": "node setup.js" + "setup": "node utils/setup.js" }, "author": "BluDood", "license": "ISC", diff --git a/utils/config.js b/utils/config.js index 965d8f0..9c300d2 100644 --- a/utils/config.js +++ b/utils/config.js @@ -7,7 +7,7 @@ function checkConfig() { process.exit() } try { - JSON.parse(fs.readFileSync('./databases/reactionroles.json')) + JSON.parse(fs.readFileSync('./config.json')) } catch { console.log("Looks like your config file has been corrupted. Please regenerate it with 'npm run setup' and try again.") process.exit() diff --git a/console.js b/utils/console.js similarity index 93% rename from console.js rename to utils/console.js index 93aa781..0c42551 100644 --- a/console.js +++ b/utils/console.js @@ -1,5 +1,5 @@ const chalk = require('chalk') -const sleep = require('./utils/sleep') +const sleep = require('./sleep') let minimal = false @@ -19,6 +19,7 @@ async function motd(tag) { } tag && console.log(`Welcome to BluBot! Your bot (${tag}) is now running.`) console.log(minimal ? 'Press h and hit Enter for help.' : 'Press h for help.') + console.log(process.argv) } const commands = { @@ -46,8 +47,8 @@ const commands = { } module.exports = { - init: arg => { - if (arg === '--minimal') minimal = true + init: () => { + if (process.argv[2] === '--minimal') minimal = true !minimal && console.clear() console.log(chalk.yellow('Starting BluBot...')) if (!minimal) { diff --git a/utils/deploy.js b/utils/deploy.js index 4cb9929..1163216 100644 --- a/utils/deploy.js +++ b/utils/deploy.js @@ -23,9 +23,6 @@ module.exports = id => { rest .put(Routes.applicationGuildCommands(id, guildId), { body: commands }) - .then(() => - // so it doesn't interfere with the console animation - sleep(1000).then(() => console.log('Successfully updated guild commands.')) - ) + .then(() => sleep(1000).then(() => console.log('Successfully updated guild commands.'))) .catch(console.error) } diff --git a/utils/moderation.js b/utils/moderation.js deleted file mode 100644 index 769dc8a..0000000 --- a/utils/moderation.js +++ /dev/null @@ -1,76 +0,0 @@ -const log = require('./log') - -// unused for now, don't look - -async function ban(target, reason, deleteMessages) { - if (!target.bannable) return 'that user is not bannable!' - const ban = await target.ban(target, { days: deleteMessages, reason: reason }).catch(() => false) - if (ban == false) return 'I could not ban that user!' - return true -} - -async function unban(guild, target, reason) { - const bans = await guild.bans.fetch() - if (!bans.has(target.id)) return 'that user is not banned!' - const unban = await guild.members.unban(target, reason).catch(() => false) - if (unban == false) return 'an unknown error occurred!' - return true -} - -async function kick(target, reason) { - if (!target.kickable) return 'that user is not kickable!' - const kick = await target.kick(reason).catch(() => false) - if (kick == false) return 'an unknown error occurred!' - return true -} - -async function timeout(target, reason, duration) { - if (!target.moderatable) return 'that user is not moderatable!' - const timeout = await target.timeout(duration, reason).catch(() => false) - if (timeout == false) return 'an unknown error occurred!' - return true -} - -async function untimeout(target, reason) { - if (!target.moderatable) return 'that user is not moderatable!' - const untimeout = await target.timeout(null, reason).catch(() => false) - if (untimeout == false) return 'an unknown error occurred!' - return true -} - -async function purge(channel, amount, reason, target) { - if (!channel.manageable) return 'that channel is not manageable!' - let messages = await channel.messages.fetch({ limit: amount }, { cache: false, force: true }).catch(() => false) - if (target) messages = messages.filter(m => m.author.id == target.id) - if (messages == false) return 'an unknown error occurred!' - if (messages.size == 0) return 'there are no messages to purge!' - const purge = channel.bulkDelete(messages, reason).catch(() => false) - if (purge == false) return 'an unknown error occurred!' - return true -} - -module.exports = { - checkBotPerms: interaction => { - return require('./checkBotPerms')(interaction) - }, - checkUserPerms: interaction => { - return require('./checkUserPerms')(interaction) - }, - moderate: (moderator, target, reason, type, options) => { - let = true - if (type == 'ban') { - const result = ban(target, reason, options.deleteMessages) - if (result !== true) su - } else if (type == 'kick') { - const result = kick(target, reason) - } else if (type == 'timeout') { - const result = timeout(target, reason, options.duration) - } - log(moderator.guild, type, { - moderator, - reason, - target - }) - }, - unmoderate: (moderator, target, reason, type) => {} -} diff --git a/utils/reactionroles.js b/utils/reactionroles.js index 1d35589..3003248 100644 --- a/utils/reactionroles.js +++ b/utils/reactionroles.js @@ -6,7 +6,8 @@ function ensureDatabase() { try { JSON.parse(fs.readFileSync('./databases/reactionroles.json')) } catch { - // show a warning first? + fs.writeFileSync('./databases/reactionroles.bak.json', fs.readFileSync('./databases/reactionroles.json')) + console.log('Your reaction role database was corrupted, so we had to reset it. You can find a backup in ./databases/reactionroles.bak.json') fs.writeFileSync('./databases/reactionroles.json', '[]') } } diff --git a/setup.js b/utils/setup.js similarity index 100% rename from setup.js rename to utils/setup.js