Skip to content
This repository has been archived by the owner on Sep 29, 2022. It is now read-only.

Escape suggestion ids #88

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/commands/Bot Owner/gsid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light');
const { stripIndent } = require('common-tags');
const Command = require('../../structures/Command');
const Logger = require('../../utils/logger');
const { buildErrorEmbed } = require('../../utils/functions');
const { buildErrorEmbed, escapeSuggestionId } = require('../../utils/functions');

module.exports = class GSIDCommand extends Command {
constructor(client) {
Expand All @@ -25,9 +25,10 @@ module.exports = class GSIDCommand extends Command {

if (!args[0]) return this.client.errors.noUsage(message.channel, this, settings);

const escapedId = escapeSuggestionId(args[0]);
let suggestion;
try {
suggestion = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(args[0]);
suggestion = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId);
} catch (err) {
Logger.errorCmd(this, err.stack);
return message.channel.send(buildErrorEmbed(err));
Expand Down
5 changes: 3 additions & 2 deletions src/commands/Staff/approve.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light');
const { stripIndent } = require('common-tags');
const Command = require('../../structures/Command');
const Logger = require('../../utils/logger');
const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions');
const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel, escapeSuggestionId } = require('../../utils/functions');

module.exports = class ApproveCommand extends Command {
constructor(client) {
Expand All @@ -29,10 +29,11 @@ module.exports = class ApproveCommand extends Command {
if (!id) return this.client.errors.noUsage(message.channel, this, settings);

const reply = args.slice(1).join(' ');
const escapedId = escapeSuggestionId(args[0]);

const errMessage = new Error(`\`${id}\` does not resolve to or return a valid suggestion!`);
try {
if ([7, 8].includes(id.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(id);
if ([7, 8].includes(escapedId.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId);
else if (validateSnowflake(id)) document = await this.client.mongodb.helpers.suggestions.getGuildSuggestionViaMessageID(message.guild, id);
else return message.channel.send(buildErrorEmbed(errMessage, false));
} catch (error) {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/Staff/note.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { MessageEmbed } = require('discord.js-light');
const { oneLine } = require('common-tags');
const Command = require('../../structures/Command');
const { validateSnowflake, buildErrorEmbed } = require('../../utils/functions');
const { validateSnowflake, buildErrorEmbed, escapeSuggestionId } = require('../../utils/functions');
const Logger = require('../../utils/logger');

module.exports = class NoteCommand extends Command {
Expand All @@ -25,11 +25,11 @@ module.exports = class NoteCommand extends Command {
const id = args[0];
const note = args.slice(1).join(' ');
if (!note) return this.client.errors.noUsage(message.channel, this, settings);

const escapedId = escapeSuggestionId(args[0]);
let document;
const errMessage = new Error(`\`${id}\` does not resolve to or return a valid suggestion!`);
try {
if ([7, 8].includes(id.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(id);
if ([7, 8].includes(escapedId.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId);
else if (validateSnowflake(id)) document = await this.client.mongodb.helpers.suggestions.getGuildSuggestionViaMessageID(message.guild, id);
else return message.channel.send(buildErrorEmbed(errMessage, false));
} catch (err) {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/Staff/reject.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light');
const { stripIndent } = require('common-tags');
const Command = require('../../structures/Command');
const Logger = require('../../utils/logger');
const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions');
const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel, escapeSuggestionId } = require('../../utils/functions');

module.exports = class RejectCommand extends Command {
constructor(client) {
Expand Down Expand Up @@ -33,9 +33,10 @@ module.exports = class RejectCommand extends Command {
const reply = args.slice(1).join(' ');
if (!reply && settings.responseRequired) return this.client.errors.noRejectedResponse(message.channel);

const escapedId = escapeSuggestionId(args[0]);
const errMessage = new Error(`\`${id}\` does not resolve to or return a valid suggestion!`);
try {
if ([7, 8].includes(id.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(id);
if ([7, 8].includes(escapedId.length)) document = await this.client.mongodb.helpers.suggestions.getGlobalSuggestion(escapedId);
else if (validateSnowflake(id)) document = await this.client.mongodb.helpers.suggestions.getGuildSuggestionViaMessageID(message.guild, id);
else return message.channel.send(buildErrorEmbed(errMessage, false));
} catch (error) {
Expand Down
5 changes: 3 additions & 2 deletions src/commands/Suggestions/sid.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { MessageEmbed, Util: { escapeMarkdown } } = require('discord.js-light');
const { stripIndent } = require('common-tags');
const Command = require('../../structures/Command');
const Logger = require('../../utils/logger');
const { buildErrorEmbed } = require('../../utils/functions');
const { buildErrorEmbed, escapeSuggestionId } = require('../../utils/functions');

module.exports = class SIDCommand extends Command {
constructor(client) {
Expand All @@ -23,9 +23,10 @@ module.exports = class SIDCommand extends Command {

if (!args[0]) return this.client.errors.noUsage(message.channel, this, settings);

const escapedId = escapeSuggestionId(args[0]);
let suggestion;
try {
suggestion = await this.client.mongodb.helpers.suggestions.getGuildSuggestion(message.guild, args[0]);
suggestion = await this.client.mongodb.helpers.suggestions.getGuildSuggestion(message.guild, escapedId);
} catch (err) {
Logger.errorCmd(this, err.stack);
return message.channel.send(buildErrorEmbed(err));
Expand Down
11 changes: 11 additions & 0 deletions src/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,16 @@ const getDefaultSuggestionsChannel = (guild) => {
return guild.channels.fetch({ cache: false }).then(res => res.filter(c => c.type === 'text').find(c => c.name === config.suggestionsChannel)) ?? null;
};

/**
* Escape all non-alphanumeric characters from a suggestion id.
* @param {id} The raw suggestion id.
alaninnovates marked this conversation as resolved.
Show resolved Hide resolved
* @return {String} The escaped suggestion id.
*/
const escapeSuggestionId = (id) => {
const idRegex = new RegExp(/[^0-9a-z]/g);
return id.replace(idRegex, '');
};

/**
* Return a new array of parsed array of arguments removing brackets.
* @param {Array<String>} args The command arguments.
Expand Down Expand Up @@ -299,6 +309,7 @@ module.exports = {
displayUptime,
getRandomGiphyImage,
getDefaultSuggestionsChannel,
escapeSuggestionId,
parseCommandArguments,
postStatsCronJob,
suggestionMessageReactionFilter,
Expand Down