From d2d6454cb65adfb9d2c4b35764a8152b3c9af870 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Fri, 4 Dec 2020 11:14:26 +0000 Subject: [PATCH 1/2] Add pasta commands --- data/pasta.json | 1 + package.json | 2 +- src/commands/command_handler.js | 4 ++ src/commands/pasta_command_handler.js | 94 +++++++++++++++++++++++++++ src/events/message_sent_handler.js | 4 +- 5 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 data/pasta.json create mode 100644 src/commands/pasta_command_handler.js diff --git a/data/pasta.json b/data/pasta.json new file mode 100644 index 0000000..a85cca0 --- /dev/null +++ b/data/pasta.json @@ -0,0 +1 @@ +[{"name":"ahhh","value":"ah","dateAdded":"2020-12-04 11:08:12"},{"name":"matt","value":"yeet","dateAdded":"2020-12-04 11:10:32","author":"Hopson"}] 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..3837539 --- /dev/null +++ b/src/commands/pasta_command_handler.js @@ -0,0 +1,94 @@ +const Config = require('../../data/config.json'); +const CommandHandler = require('./command_handler'); +const Discord = require('discord.js'); +const JsonFile = require("jsonfile") + +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; + } + } + console.log(message) + // 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) {} + +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) { From 6e5bceb9edc7b7bda376cf6e0dd5b79e7989d033 Mon Sep 17 00:00:00 2001 From: Hopson97 Date: Fri, 4 Dec 2020 11:30:00 +0000 Subject: [PATCH 2/2] add removing pasta --- data/pasta.json | 2 +- src/commands/pasta_command_handler.js | 33 +++++++++++++++++++++++---- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/data/pasta.json b/data/pasta.json index a85cca0..fe51488 100644 --- a/data/pasta.json +++ b/data/pasta.json @@ -1 +1 @@ -[{"name":"ahhh","value":"ah","dateAdded":"2020-12-04 11:08:12"},{"name":"matt","value":"yeet","dateAdded":"2020-12-04 11:10:32","author":"Hopson"}] +[] diff --git a/src/commands/pasta_command_handler.js b/src/commands/pasta_command_handler.js index 3837539..72338b0 100644 --- a/src/commands/pasta_command_handler.js +++ b/src/commands/pasta_command_handler.js @@ -1,7 +1,10 @@ const Config = require('../../data/config.json'); const CommandHandler = require('./command_handler'); const Discord = require('discord.js'); -const JsonFile = require("jsonfile") +const JsonFile = require("jsonfile"); +const { + del +} = require('request-promise'); const PASTA_FILE = "data/pasta.json" @@ -57,14 +60,13 @@ function addPasta(message, args) { return; } } - console.log(message) // 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(/\..+/, ''), + replace(/T/, ' '). + replace(/\..+/, ''), author: message.author.username }); @@ -77,7 +79,28 @@ function addPasta(message, args) { } } -function removePasta(message, args) {} +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) {