From 3bdfbf0e443ab25224d47a70fc4d2ce9a90b540e Mon Sep 17 00:00:00 2001 From: = Date: Wed, 30 Jun 2021 18:59:24 -0400 Subject: [PATCH] Removing all /commands --- commands/gif.js | 20 ++++++++++ commands/pasta.js | 15 ++++++++ commands/spongebob.js | 19 ++++++++++ functions.js | 34 +++++++++++------ index.js | 85 +++++++++++-------------------------------- 5 files changed, 98 insertions(+), 75 deletions(-) create mode 100644 commands/gif.js create mode 100644 commands/pasta.js create mode 100644 commands/spongebob.js diff --git a/commands/gif.js b/commands/gif.js new file mode 100644 index 0000000..000b231 --- /dev/null +++ b/commands/gif.js @@ -0,0 +1,20 @@ +const giphy = require('giphy-api')(process.env.giphyAPIKey); +module.exports = { + name: 'gif', + description: 'Send a GIF', + execute(message, file) { + const client = message.client; + if (!client.gifs.has(file.name)) { + giphy.search(file.name, (err, res) => { + if (res.data[0] != undefined) { + message.channel.send(file.name + ' requested by ' + message.author.username + '\n' + res.data[0].embed_url).then().catch(console.error); + } else { + message.channel.send('I was unable to find a gif of ' + file.name); + } + if (err) console.error(err); + }); + } else { + message.channel.send(file.name + ' requested by ' + message.author.username + '\n' + client.gifs.get(file.name).embed_url); + } + } +} \ No newline at end of file diff --git a/commands/pasta.js b/commands/pasta.js new file mode 100644 index 0000000..002bde7 --- /dev/null +++ b/commands/pasta.js @@ -0,0 +1,15 @@ +module.exports = { + name: 'pasta', + description: 'Send a copypasta.', + execute(message, file) { + const client = message.client; + const replyHeader = `\'${file.name}\' requested by: ${message.author.username}\n`; + let replyBody = ''; + if (!client.pastas.has(file.name)) { + replyBody = 'Sorry I couldn\'t find that pasta.'; + } else { + replyBody = client.pastas.get(file.name).content; + } + message.channel.send(replyHeader + replyBody); + } +} \ No newline at end of file diff --git a/commands/spongebob.js b/commands/spongebob.js new file mode 100644 index 0000000..9fc64da --- /dev/null +++ b/commands/spongebob.js @@ -0,0 +1,19 @@ +module.exports = { + name: 'spongebob', + description: 'SpOnGeBoB-iFy AnYtHiNg AuToMaTiCaLly', + execute(message, file) { + const replyHeader = `Requested by: ${message.author.username}\n`; + let flipper = 0; + let newText = ''; + for (const letter of file.name) { + if (flipper == 0) { + newText = newText + letter.toUpperCase(); + flipper = 1; + } else { + newText = newText + letter; + flipper = 0; + } + } + message.channel.send(replyHeader + newText); + } +} \ No newline at end of file diff --git a/functions.js b/functions.js index f85eb75..fda5162 100644 --- a/functions.js +++ b/functions.js @@ -30,20 +30,30 @@ module.exports = { } if (debug) console.log(client.pastas); }, - getExtension(args) { - const finalWord = args[args.length - 1]; - const file = finalWord.split('.'); + getFileInfo(content) { + const finalPeriod = content.search(/\.(?:.(?!\\))+$/gim); + if (finalPeriod < 0) return false; + const extension = content.slice(finalPeriod).replace('.','').toLowerCase(); + const filename = content.slice(0,finalPeriod).toLowerCase(); + const file = { + 'name': filename, + 'extension': extension + }; + console.log(finalPeriod, file); return file; }, - extCheck(content) { - const lastFour = content.slice(-4); - switch (lastFour) { - case '.gif': - return 'gif'; - case '.jpg': - return 'jpg'; - case '.pst': - return 'pst'; + extIsValid(extension) { + switch (extension) { + case 'gif': + return true; + case 'jpg': + return false; + case 'pasta': + return true; + case 'admin': + return true; + case 'spongebob': + return true; default: return false; } diff --git a/index.js b/index.js index 2ccc98b..095c6ab 100644 --- a/index.js +++ b/index.js @@ -20,76 +20,35 @@ client.once('ready', () => { functions.getCommandFiles(client); functions.getGifFiles(client); functions.getPastaFiles(client); - client.channels.fetch(logChannel) - .then(channel => { - channel.send(bootMessage) - .then() - .catch(err => console.error(err)); - }) - .catch(err => console.error(err)); + // client.channels.fetch(logChannel) + // .then(channel => { + // channel.send(bootMessage) + // .then() + // .catch(err => console.error(err)); + // }) + // .catch(err => console.error(err)); }); client.login(process.env.TOKEN); client.on('message', message => { - const args = message.content.trim().split(/ +/); - // TODO this will surely break something when trying to use non-filename commands - const file = functions.getExtension(args); - // If the message is from a bot, or doesn't have the prefix or a file extension, stop here. - if ((!message.content.startsWith(prefix) && file[1] == undefined) || message.author.bot) return; + // Get the filename and extension as an array + // TODO Rename this function to something more appropriate + const file = functions.getFileInfo(message.content); + if (!file) return; + // If the message is from a bot, or doesn't have a valid file extension, stop here. + if (functions.extIsValid(file.extension) == false || message.author.bot) return; - // If the message starts with the prefix, - if (message.content.startsWith(prefix)) { - // Extract the command - const command = args.shift().toLowerCase().slice(prefix.length); + // If the command collection doesn't contain the given command, stop here. + if (!client.commands.has(file.extension)) return; - if (debug) console.log(args); - // If the command collection doesn't contain the given command, stop here. - if (!client.commands.has(command)) return; - - try { - // Attempt to execute the command - client.commands.get(command).execute(message, args); - } catch (error) { - // Log errors and let the user know something went wrong. - console.error(error); - message.channel.send('There was an error trying to execute that command.'); - } - } - - // If there is a file extension - if (file[1] != undefined) { - const query = message.content.slice(0, -4); - switch (file[1]) { - case 'gif': - if (debug) console.log(query); - - if (!client.gifs.has(file[0])) { - giphy.search(query, (err, res) => { - if (res.data[0] != undefined) { - message.channel.send(query + ' requested by ' + message.author.username + '\n' + res.data[0].embed_url).then().catch(console.error); - } else { - message.channel.send('I was unable to find a gif of ' + query); - } - if (err) console.error(err); - }); - } else { - message.channel.send(query + ' requested by ' + message.author.username + '\n' + client.gifs.get(query).embed_url); - } - break; - case 'pasta': - // const pastaName = args[0].splice(args[0].search(/\.(?:.(?!\\))+$/gim)) - if (debug) console.log(file[0]); - - if (!client.pastas.has(file[0])) { - message.reply('Sorry I couldn\'t find that gif.'); - } else { - message.channel.send(client.pastas.get(file[0]).content, { split: { char: ' ' } }); - } - break; - default: - break; - } + try { + // Attempt to execute the command + client.commands.get(file.extension).execute(message, file); + } catch (error) { + // Log errors and let the user know something went wrong. + console.error(error); + message.channel.send('There was an error trying to execute that command.'); } // Try to delete the requester's message