diff --git a/data/pasta.json b/data/pasta.json new file mode 100644 index 0000000..fe51488 --- /dev/null +++ b/data/pasta.json @@ -0,0 +1 @@ +[] diff --git a/package.json b/package.json index 01ecaf2..e918cb2 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,7 @@ "dateformat": "^3.0.3", "discord.js": "^11.2.1", "jsonfile": "^4.0.0", - "node-fetch": "^2.3.0", + "node-fetch": "^2.6.1", "request": "^2.88.0", "request-promise": "^4.2.4", "sqlite3": "^4.1.0" diff --git a/src/commands/command_handler.js b/src/commands/command_handler.js index 02b0ac3..75feb6b 100644 --- a/src/commands/command_handler.js +++ b/src/commands/command_handler.js @@ -39,6 +39,10 @@ module.exports = class CommandHandlerBase { const cmd = this.commands.get(command); cmd.action(message, args, client); } + else if (this.commands.has("ANY_COMMAND")) { + const cmd = this.commands.get("ANY_COMMAND"); + cmd.action(message, args, client); + } } /** diff --git a/src/commands/pasta_command_handler.js b/src/commands/pasta_command_handler.js new file mode 100644 index 0000000..72338b0 --- /dev/null +++ b/src/commands/pasta_command_handler.js @@ -0,0 +1,117 @@ +const Config = require('../../data/config.json'); +const CommandHandler = require('./command_handler'); +const Discord = require('discord.js'); +const JsonFile = require("jsonfile"); +const { + del +} = require('request-promise'); + +const PASTA_FILE = "data/pasta.json" + +module.exports = class RoleEventHandler extends CommandHandler { + constructor() { + super('pasta'); + super.addCommand( + "list", + "Gets a list of added pasta", + ">pasta list", + listPasta + ) + .addCommand( + "add", + "Counts how many people have a certain role", + '>pasta add < ', + addPasta + ) + .addCommand( + "remove", + "Remove a pasta by name", + ">pasta remove ", + removePasta + ) + .addCommand( + "ANY_COMMAND", + "Spits out a pasta", + ">pasta ", + spitPasta + ) + } +} + +function listPasta(message, args) { + const pastas = JsonFile.readFileSync(PASTA_FILE); + + let output = "PASTA LIST\n= = = = = = = \n" + for (const pasta of pastas) { + output += `**>pasta ${pasta["name"]}** - Added on _${pasta["dateAdded"]}_ by _${pasta["author"]}_\n` + } + message.channel.send(output) +} + +function addPasta(message, args) { + if (args.length == 2) { + const name = args[0] + const value = args[1] + const pastas = JsonFile.readFileSync(PASTA_FILE); + + for (const pasta of pastas) { + if (pasta["name"] == name) { + message.channel.send(`I was unable to add pasta ${name}, as it already exists!`); + return; + } + } + // https://stackoverflow.com/questions/10645994/how-to-format-a-utc-date-as-a-yyyy-mm-dd-hhmmss-string-using-nodejs + pastas.push({ + name: name, + value: value, + dateAdded: new Date().toISOString(). + replace(/T/, ' '). + replace(/\..+/, ''), + author: message.author.username + }); + + JsonFile.writeFile(PASTA_FILE, pastas, function (err) { + if (err) { + console.error(err) + } + }); + message.channel.send(`Pasta ${name} added!`); + } +} + +function removePasta(message, args) { + if (args.length == 1) { + const name = args[0] + const pastas = JsonFile.readFileSync(PASTA_FILE); + const filtered = pastas.filter(pasta => { + pasta["name"] === name + }); + + console.log(filtered); + if (filtered.length < pastas.length) { + message.channel.send(`Pasta ${name} removed!`); + + } else { + message.channel.send(`I couldn't find pasta ${name}, so nothing was removed.`); + } + JsonFile.writeFile(PASTA_FILE, filtered, function (err) { + if (err) { + console.error(err) + } + }); + } +} + +function spitPasta(message, args) { + if (args.length == 0) { + return + } + const pastas = JsonFile.readFileSync(PASTA_FILE); + + for (const pasta of pastas) { + if (pasta["name"] == args[0]) { + message.channel.send(pasta["value"]); + return; + } + } +} \ No newline at end of file diff --git a/src/events/message_sent_handler.js b/src/events/message_sent_handler.js index 9dcff58..c831763 100644 --- a/src/events/message_sent_handler.js +++ b/src/events/message_sent_handler.js @@ -2,6 +2,7 @@ const PollCommandHandler = require('../commands/poll_command_handler'); const RoleCommandHandler = require('../commands/role_command_handler'); const DefaultCommandHandler = require('../commands/default_command_handler'); const RefCommandHandler = require('../commands/ref_command_handler'); +const PastaCommandHandler = require('../commands/pasta_command_handler'); const Config = require('../../data/config.json'); const Discord = require('discord.js') @@ -18,6 +19,7 @@ module.exports = class MessageSentHandler { new PollCommandHandler(), new RoleCommandHandler(), new RefCommandHandler(), + new PastaCommandHandler(), ] } /** @@ -27,7 +29,7 @@ module.exports = class MessageSentHandler { */ handleMessageSent(message, client) { logMessageInfo(message); - handleMessageSentWithoutLog(message, client); + this.handleMessageSentWithoutLog(message, client); } handleMessageSentWithoutLog(message, client) {