sanitizing inputs and creating embeds

This commit is contained in:
= 2021-07-06 17:37:29 -04:00
parent 2c6d424baa
commit 5dc2a1ab53
13 changed files with 45 additions and 39 deletions

View File

@ -1,6 +1,9 @@
# NodBot # NodBot
A simple Discord bot created by @voidf1sh#0420 for retreiving gifs, saving copypastas, and more coming soon. A simple Discord bot created by @voidf1sh#0420 for retreiving gifs, saving copypastas, and more coming soon.
## Dependancies
NodBot depends on `fs`, `discord.js`, `dotenv`, and `giphy-api`.
## Features ## Features
Dynamic Help Message Dynamic Help Message
Ability to save favorite gifs and copypastas Ability to save favorite gifs and copypastas

View File

@ -1,3 +1,5 @@
const functions = require('../functions');
const giphy = require('giphy-api')(process.env.giphyAPIKey); const giphy = require('giphy-api')(process.env.giphyAPIKey);
module.exports = { module.exports = {
name: 'gif', name: 'gif',
@ -7,14 +9,26 @@ module.exports = {
if (!client.gifs.has(file.name)) { if (!client.gifs.has(file.name)) {
giphy.search(file.name, (err, res) => { giphy.search(file.name, (err, res) => {
if (res.data[0] != undefined) { 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); // message.channel.send(file.name + ' requested by ' + message.author.username + '\n' + res.data[0].embed_url).then().catch(console.error);
const gifInfo = {
'name': file.name,
'embed_url': res.data[0].images.original.url,
'requestor': '@' + message.author.username + '#' + message.author.discriminator,
};
message.channel.send(functions.createGifEmbed(gifInfo));
} else { } else {
message.channel.send('I was unable to find a gif of ' + file.name); message.channel.send('I was unable to find a gif of ' + file.name);
} }
if (err) console.error(err); if (err) console.error(err);
}); });
} else { } else {
message.channel.send(file.name + ' requested by ' + message.author.username + '\n' + client.gifs.get(file.name).embed_url); // message.channel.send(file.name + ' requested by ' + message.author.username + '\n' + client.gifs.get(file.name).embed_url);
const gifInfo = {
'name': file.name,
'embed_url': file.embed_url,
'requestor': message.author.username + '#' + message.author.discriminator,
};
message.channel.send(functions.createGifEmbed(gifInfo));
} }
} }
} }

View File

@ -31,7 +31,7 @@ module.exports = {
if (command.aliases) data.push(`**Aliases:** ${command.aliases.join(', ')}`); if (command.aliases) data.push(`**Aliases:** ${command.aliases.join(', ')}`);
if (command.description) data.push(`**Description:** ${command.description}`); if (command.description) data.push(`**Description:** ${command.description}`);
if (command.usage) data.push(`**Usage:** ${prefix}${command.name} ${command.usage}`); if (command.usage) data.push(`**Usage:** \`${command.usage}.${command.name}\``);
data.push(`**Cooldown:** ${command.cooldown || 3} second(s)`); data.push(`**Cooldown:** ${command.cooldown || 3} second(s)`);

9
commands/joint.js Normal file
View File

@ -0,0 +1,9 @@
const { emoji } = require('../config.json');
module.exports = {
name: 'joint',
description: 'Pass the joint!',
execute(message, args) {
message.channel.send('It\'s dangerous to go alone... take this: ' + emoji.joint);
}
}

View File

@ -1,7 +0,0 @@
module.exports = {
name: "log-message",
description: "Logs the message to console for debugging purposes.",
execute(message, args) {
console.log(message);
}
}

View File

@ -1,7 +0,0 @@
module.exports = {
name: 'ping',
description: 'Ping!',
execute(message, args) {
message.channel.send('Pong.');
}
}

View File

@ -1,9 +0,0 @@
/* eslint-disable quotes */
module.exports = {
name: "reload-gifs",
description: "Refresh the hardcoded gif library.",
execute(message, args) {
const functions = require('../functions.js');
functions.getGifFiles(message.client);
}
}

View File

@ -1,6 +1,7 @@
module.exports = { module.exports = {
name: 'request', name: 'request',
description: 'Submit a request to the bot developer.', description: 'Submit a request to the bot developer.',
usage: '<request or feedback>',
execute(message, file) { execute(message, file) {
const request = file.name; const request = file.name;
message.channel.send('Your request has been submitted:\n```\n' + request + '\n```'); message.channel.send('Your request has been submitted:\n```\n' + request + '\n```');

View File

@ -1,6 +1,7 @@
module.exports = { module.exports = {
name: 'savegif', name: 'savegif',
description: 'Adds a given gif to the hardcoded list.', description: 'Adds a given gif to the hardcoded list.',
usage: '<https://link.to.gif> <gif_name>',
execute(message, file) { execute(message, file) {
const tempArray = file.name.split(' '); const tempArray = file.name.split(' ');
const embedURL = tempArray.shift(); const embedURL = tempArray.shift();

View File

@ -3,12 +3,13 @@ const functions = require('../functions.js');
module.exports = { module.exports = {
name: 'savepasta', name: 'savepasta',
description: 'Adds a given copypasta to the hardcoded list.', description: 'Adds a given copypasta to the hardcoded list.',
usage: '<Copy Pasta Text> <pasta_name>',
execute(message, file) { execute(message, file) {
const fs = require('fs'); const fs = require('fs');
const pastaTextArray = message.content.split(' '); const pastaTextArray = message.content.split(' ');
const pastaFile = functions.getFileInfo(pastaTextArray.pop()); const pastaFile = functions.getFileInfo(pastaTextArray.pop());
const pastaText = pastaTextArray.join(' '); const pastaText = pastaTextArray.join(' ');
const pastaTextEscaped = pastaText.replace(/'/g, '\\\'').replace(/\n/g, '\\n'); const pastaTextEscaped = functions.cleanInput(pastaText);
fs.appendFile(`./pastas/${pastaFile.name}.js`, `module.exports = {\n\tname: '${pastaFile.name}',\n\tcontent: '${pastaTextEscaped}'\n}`, function(err) { fs.appendFile(`./pastas/${pastaFile.name}.js`, `module.exports = {\n\tname: '${pastaFile.name}',\n\tcontent: '${pastaTextEscaped}'\n}`, function(err) {
if (err) throw err; if (err) throw err;
console.log('Saved file!'); console.log('Saved file!');

View File

@ -1,6 +1,7 @@
module.exports = { module.exports = {
name: 'spongebob', name: 'spongebob',
description: 'SpOnGeBoB-iFy AnYtHiNg AuToMaTiCaLly', description: 'SpOnGeBoB-iFy AnYtHiNg AuToMaTiCaLly',
usage: '<text you want spongebob-ified',
execute(message, file) { execute(message, file) {
const replyHeader = `Requested by: ${message.author.username}\n`; const replyHeader = `Requested by: ${message.author.username}\n`;
let flipper = 0; let flipper = 0;

View File

@ -1,12 +0,0 @@
module.exports = {
name: "test-textconv",
description: "Give the bot some text to convert and send back.",
execute(message, args) {
console.log(args);
const textPre = args.join(' ');
const textPost1 = textPre.replace(/\n/,'\n');
const textPost2 = textPost1.replace(/\'/,'\'');
console.log(textPost1);
console.log(textPost2);
}
}

View File

@ -45,5 +45,16 @@ module.exports = {
extIsValid(extension) { extIsValid(extension) {
const extensions = require('./config.json').validExtensions; const extensions = require('./config.json').validExtensions;
return extensions.includes(extension); return extensions.includes(extension);
},
cleanInput(input) {
return input.replace(/'/g, '\\\'').replace(/\n/g, '\\n');
},
createGifEmbed(data) {
return new Discord.MessageEmbed()
.setAuthor('NodBot v2 - GIF')
.setTitle(data.name)
.setImage(data.embed_url)
.setTimestamp()
.setFooter(data.requestor);
} }
} }