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

Commit

Permalink
🎨 Refactor checking of suggestion-based channels
Browse files Browse the repository at this point in the history
- Handle default logs channel if it exists in approve, reject and config logs commands
- Properly handle news channel type
  • Loading branch information
acollierr17 committed Mar 6, 2022
1 parent 29636b8 commit 34ff143
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 29 deletions.
10 changes: 4 additions & 6 deletions src/commands/Admin/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { MessageEmbed } = require('discord.js-light');
const { stripIndent } = require('common-tags');
const Command = require('../../structures/Command');
const Logger = require('../../utils/logger');
const { buildErrorEmbed, getDefaultSuggestionsChannel, getChannel, validateChannel } = require('../../utils/functions');
const { buildErrorEmbed, getDefaultSuggestionsChannel, getChannel, validateChannel, getLogsChannel } = require('../../utils/functions');

module.exports = class ConfigCommand extends Command {
constructor(client) {
Expand Down Expand Up @@ -96,7 +96,8 @@ module.exports = class ConfigCommand extends Command {

const isDefault = suggestionsChannel === 'suggestions'
suggestionsChannel = isDefault
? await message.guild.channels.fetch({ cache: false }).then(res => res.find(c => c.name === 'suggestions'))
? await message.guild.channels.fetch({ cache: false })
.then(res => res.filter(c => ['text', 'news'].includes(c.type)).find(c => c.name === 'suggestions'))
: message.guild.channels.forge(suggestionsChannel)
if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel);
configEmbed.setDescription(`Current suggestions channel: ${suggestionsChannel}`);
Expand All @@ -123,10 +124,7 @@ module.exports = class ConfigCommand extends Command {
}
}

const isDefault = [this.client.config.defaultSettings.suggestionsLogs, "suggestions-logs"].includes(suggestionsLogs);
suggestionsLogs = isDefault
? await message.guild.channels.fetch({ cache: false }).then(res => res.find(c => c.name === this.client.config.suggestionLogs))
: message.guild.channels.forge(suggestionsLogs);
suggestionsLogs = await getLogsChannel(message, suggestionsLogs)
if (!suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel);
configEmbed.setDescription(`Current suggestions logs channel: ${suggestionsLogs}`);
configEmbed.addField('More Information', `[Link](${confDocs}#suggestions-logs-channel)`);
Expand Down
15 changes: 6 additions & 9 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 } = require('../../utils/functions');
const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions');

module.exports = class ApproveCommand extends Command {
constructor(client) {
Expand Down Expand Up @@ -69,21 +69,18 @@ module.exports = class ApproveCommand extends Command {
Please contact the developer via the Support Discord: ${discord}`);
}

const isDefault = [this.client.config.defaultSettings.suggestionsLogs, 'suggestions-logs'].includes(settings.suggestionsLogs);
const willIgnoreLogs = isDefault && !settings.keepLogs;

let suggestionsChannel,
suggestionsLogs;
try {
suggestionsChannel = settings.suggestionsChannel && await message.guild.channels.fetch(settings.suggestionsChannel);
if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel);
if (!willIgnoreLogs) {
suggestionsLogs = settings.suggestionsLogs && await message.guild.channels.fetch(settings.suggestionsLogs);
if (!settings.keepLogs) {
suggestionsLogs = settings.suggestionsLogs && await getLogsChannel(message, settings.suggestionsLogs);
if (!suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel);
}
} catch (error) {
if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel);
if (!willIgnoreLogs) return this.client.errors.noSuggestionsLogs(message.channel);
if (!settings.keepLogs && !suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel);
Logger.errorCmd(this, error.stack);
return message.channel.send(buildErrorEmbed(error));
}
Expand Down Expand Up @@ -191,7 +188,7 @@ module.exports = class ApproveCommand extends Command {
`);
}

if (!willIgnoreLogs) {
if (suggestionsLogs && !settings.keepLogs) {
const missingPermissions = suggestionsLogs.permissionsFor(message.guild.me).missing(logsPermissions);
if (missingPermissions.length > 0) return this.client.errors.noChannelPerms(message, suggestionsLogs, missingPermissions);
}
Expand All @@ -212,7 +209,7 @@ module.exports = class ApproveCommand extends Command {

try {
// Will handle suggestion if there's no logs channel set/default value is set OR keepLogs is enabled and a valid logs channel is set
if (sMessage && (willIgnoreLogs || (settings.keepLogs && suggestionsLogs))) {
if (sMessage && settings.keepLogs) {
const logEmbed = new MessageEmbed(logsEmbed)
.setTitle('Suggestion Approved');
await sMessage.edit(logEmbed);
Expand Down
15 changes: 6 additions & 9 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 } = require('../../utils/functions');
const { validateSnowflake, suggestionMessageReactionFilter, buildErrorEmbed, getLogsChannel } = require('../../utils/functions');

module.exports = class RejectCommand extends Command {
constructor(client) {
Expand Down Expand Up @@ -72,21 +72,18 @@ module.exports = class RejectCommand extends Command {
Please contact the developer via the Support Discord: ${discord}`);
}

const isDefault = [this.client.config.defaultSettings.suggestionsLogs, 'suggestions-logs'].includes(settings.suggestionsLogs);
const willIgnoreLogs = isDefault && !settings.keepLogs;

let suggestionsChannel,
suggestionsLogs;
try {
suggestionsChannel = settings.suggestionsChannel && await message.guild.channels.fetch(settings.suggestionsChannel);
if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel);
if (!willIgnoreLogs) {
suggestionsLogs = settings.suggestionsLogs && await message.guild.channels.fetch(settings.suggestionsLogs);
if (!settings.keepLogs) {
suggestionsLogs = settings.suggestionsLogs && await getLogsChannel(message, settings.suggestionsLogs);
if (!suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel);
}
} catch (error) {
if (!suggestionsChannel) return this.client.errors.noSuggestions(message.channel);
if (!willIgnoreLogs) return this.client.errors.noSuggestionsLogs(message.channel);
if (!settings.keepLogs && !suggestionsLogs) return this.client.errors.noSuggestionsLogs(message.channel);
Logger.errorCmd(this, error.stack);
return message.channel.send(buildErrorEmbed(error));
}
Expand Down Expand Up @@ -194,7 +191,7 @@ module.exports = class RejectCommand extends Command {
`);
}

if (!willIgnoreLogs) {
if (suggestionsLogs && !settings.keepLogs) {
const missingPermissions = suggestionsLogs.permissionsFor(message.guild.me).missing(logsPermissions);
if (missingPermissions.length > 0) return this.client.errors.noChannelPerms(message, suggestionsLogs, missingPermissions);
}
Expand All @@ -215,7 +212,7 @@ module.exports = class RejectCommand extends Command {

try {
// Will handle suggestion if there's no logs channel set/default value is set OR keepLogs is enabled and a valid logs channel is set
if (sMessage && (willIgnoreLogs || (settings.keepLogs && suggestionsLogs))) {
if (sMessage && settings.keepLogs) {
const logEmbed = new MessageEmbed(logsEmbed)
.setTitle('Suggestion Rejected');
await sMessage.edit(logEmbed);
Expand Down
2 changes: 1 addition & 1 deletion src/commands/Suggestions/suggest.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = class SuggestCommand extends Command {
} else {
sChannel = suggestionsChannel && await message.guild.channels.fetch(suggestionsChannel);
if (!sChannel) return this.client.errors.noSuggestions(message.channel);
if (['text', 'news'].includes(sChannel.type)) return this.client.errors.channelNotFound(sChannel, message.channel);
if (!['text', 'news'].includes(sChannel.type)) return this.client.errors.channelNotFound(sChannel, message.channel);
}
} catch (e) {
return this.client.errors.noSuggestions(message.channel);
Expand Down
30 changes: 26 additions & 4 deletions src/utils/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const fs = require('fs');
const path = require('path');
const petitio = require('petitio');
const { CronJob } = require('cron');
const { Client, MessageMentions, MessageEmbed, GuildChannel } = require('discord.js-light');
const { Client, MessageMentions, MessageEmbed, GuildChannel, Message, TextChannel } = require('discord.js-light');
const { execSync } = require('child_process');
const sentry = require('@sentry/node');

Expand Down Expand Up @@ -261,13 +261,34 @@ const postToHastebin = (content) => {
* @return {Promise<*|GuildChannel|null>}
*/
const getChannel = async (message, channel) => {
const acceptedTypes = ['text', 'news'];
const channels = await message.guild.channels.fetch({ cache: false })
.then(res => res.filter(c => ['text', 'news'].includes(c.type)));
.then(res => res.filter(c => acceptedTypes.includes(c.type)));

return channels.find(c => c.name === channel) ||
channels.get(channel) ||
await message.mentions.channels.first()?.fetch()
.then(c => c?.type === 'text' ? c : null);
.then(c => c?.type ? acceptedTypes.includes(c.type) && c : null);
};

/**
* Returns the degault logs channel if it exists in the server.
* @param {Message} message - The associated message.
* @param {String} channel - The channel name (if default) or id..
* @return {Promise<TextChannel>} The channel object.
*/
const getLogsChannel = async (message, channel) => {
const acceptedTypes = ['text', 'news'];
const defaultNames = [config.defaultSettings.suggestionsLogs, 'suggestions-logs'];
const isDefault = defaultNames.includes(channel);

const channels = await message.guild.channels.fetch({ cache: false })
.then(res => res.filter(c => acceptedTypes.includes(c.type)));

if (isDefault) return channels.find(c => defaultNames.includes(c.name));

return channels.get(channel) || await message.mentions.channels.first()?.fetch()
.then(c => c?.type ? acceptedTypes.includes(c.type) && c : null);
};

module.exports = {
Expand All @@ -285,5 +306,6 @@ module.exports = {
reportToSentry,
buildErrorEmbed,
postToHastebin,
getChannel
getChannel,
getLogsChannel
};

0 comments on commit 34ff143

Please sign in to comment.