Skip to content

Commit

Permalink
chore: move common regex to common captures
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiTenno committed Nov 21, 2018
1 parent 41db06c commit 63fdbef
Show file tree
Hide file tree
Showing 19 changed files with 79 additions and 47 deletions.
2 changes: 1 addition & 1 deletion commands.json

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions src/CommonFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ const wikiBase = process.env.WIKIA_BASE_PATH || 'https://warframe.fandom.com/wik

const isVulgarCheck = new RegExp('(n[i!1]gg[e3]r|n[i!1]gg[ua]|h[i!1]tl[e3]r|n[a@]z[i!1]|[©ck]un[t7]|fu[©c]k|[©ck]umm?|f[a@4]g|d[i!1]ck|c[o0]ck|boner|sperm|gay|gooch|jizz|pussy|penis|r[i!1]mjob|schlong|slut|wank|whore|sh[i!1]t|sex|fuk|heil|p[o0]rn|pronz|suck|rape|scrotum)', 'ig');

const trackablesCapture = `(solaris\\.warm\\.[0-9]?[0-9]|solaris\\.cold\\.[0-9]?[0-9]|cetus\\.day\\.[0-1]?[0-9]?[0-9]?|cetus\\.night\\.[0-1]?[0-9]?[0-9]?|${eventTypes.join('|')}|${rewardTypes.join('|')}|${opts.join('|')})`;
const captures = {
channel: '(?:(?:<#)?(\\d+)(?:>)?)',
role: '(?:(?:<@&)?(\\d+)(?:>)?)',
user: '(?:(?:<@!?)?(\\d+)(?:>)?)',
trackables: `(solaris\\.warm\\.[0-9]?[0-9]|solaris\\.cold\\.[0-9]?[0-9]|cetus\\.day\\.[0-1]?[0-9]?[0-9]?|cetus\\.night\\.[0-1]?[0-9]?[0-9]?|${eventTypes.join('|')}|${rewardTypes.join('|')}|${opts.join('|')})`
}

const duration = {
minute: 60,
Expand Down Expand Up @@ -153,7 +158,7 @@ const trackablesFromParameters = (params) => {
return trackables;
};

const eventsOrItems = new RegExp(trackablesCapture, 'ig');
const eventsOrItems = new RegExp(captures.trackables, 'ig');

const getRandomWelcome = () => welcomes[Math.floor(Math.random() * welcomes.length)];

Expand Down Expand Up @@ -590,5 +595,5 @@ module.exports = {
apiBase,
assetBase,
wikiBase,
trackablesCapture,
captures,
};
3 changes: 2 additions & 1 deletion src/commands/LFG/SetLFGChannel.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetLFGChannel extends Command {
constructor(bot) {
super(bot, 'settings.lfgChannel', 'set lfg channel', 'Sets the LFG Channel.');
this.usages = [
{ description: 'Set the LFG channel', parameters: ['channel id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<#)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.channel}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Logging/BanLog.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetBanLog extends Command {
constructor(bot) {
super(bot, 'settings.banLog', 'set ban log', 'Sets the log channel for bans.');
this.usages = [
{ description: 'Set the ban log channel', parameters: ['channel id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<#)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.channel}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Logging/MemberRemoveLog.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetMemRemoveLog extends Command {
constructor(bot) {
super(bot, 'settings.memberRemoveLog', 'set member remove log', 'Sets the log channel for member removals.');
this.usages = [
{ description: 'Set the member removal log channel', parameters: ['channel id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<#)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.channel}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
5 changes: 3 additions & 2 deletions src/commands/Logging/SetMessageDeleteLog.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetMessageDeleteLog extends Command {
constructor(bot) {
super(bot, 'settings.msgDeleteLog', 'set message delete log', 'Sets the log channel for message deletions.');
this.usages = [
{ description: 'Set the message dlete log channel', parameters: ['channel id'] },
{ description: 'Set the message delete log channel', parameters: ['channel id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<#)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.channel}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Logging/SetModRole.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetModRole extends Command {
constructor(bot) {
super(bot, 'settings.setModRole', 'set mod', 'Sets the server\'s mod role.');
this.usages = [
{ description: 'Change the configured mod role', parameters: ['mod role id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<@&)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.role}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Logging/SetVulgarLog.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetVulgarLog extends Command {
constructor(bot) {
super(bot, 'settings.vulgarLog', 'set vulgar log', 'Sets the log channel for vulgar names of new members.');
this.usages = [
{ description: 'Set the vulgarity log channel', parameters: ['channel id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<#)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.channel}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
3 changes: 2 additions & 1 deletion src/commands/Logging/UnbanLog.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { captures } = require('../../CommonFunctions');

class SetUnBanLog extends Command {
constructor(bot) {
super(bot, 'settings.unbanLog', 'set unban log', 'Sets the log channel for unbans.');
this.usages = [
{ description: 'Set the unban log channel', parameters: ['channel id'] },
];
this.regex = new RegExp(`^${this.call}\\s?(?:(?:<#)(\\d+)(?:>))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?${captures.channel}?$`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/Settings/Disable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
const Command = require('../../models/Command.js');
const EnableUsageEmbed = require('../../embeds/EnableUsageEmbed.js');
const EnableInfoEmbed = require('../../embeds/EnableInfoEmbed.js');
const { getChannels, getTarget } = require('../../CommonFunctions');
const { getChannels, getTarget, captures } = require('../../CommonFunctions');

const commandIdRegex = new RegExp('(\\w*\\.*\\w*\\.*\\w*\\*?)', 'ig');
const locationRegex = new RegExp('(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here|\\*))', 'ig');
const appliesToRegex = new RegExp('(?:\\s+for\\s((?:\\<\\@\\&?)?\\d+(?:\\>)?|\\*))?', 'ig');
const locationRegex = new RegExp(`(?:\\s+in\\s+(${captures.channel}|here|\\*))`, 'ig');
const appliesToRegex = new RegExp(`(?:\\s+for\\s(${captures.user}|${captures.role}|\\*))?`, 'ig');

class Disable extends Command {
constructor(bot) {
Expand Down
8 changes: 4 additions & 4 deletions src/commands/Settings/Enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
const Command = require('../../models/Command.js');
const EnableUsageEmbed = require('../../embeds/EnableUsageEmbed.js');
const EnableInfoEmbed = require('../../embeds/EnableInfoEmbed.js');
const { getTarget, getChannels } = require('../../CommonFunctions.js');
const { getTarget, getChannels, captures } = require('../../CommonFunctions.js');

const commandIdRegex = new RegExp('(\\w*\\.*\\w*\\.*\\w*\\*?)', 'ig');
const locationRegex = new RegExp('(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here|\\*))', 'ig');
const appliesToRegex = new RegExp('(?:\\s+for\\s((?:\\<\\@\\&?)?\\d+(?:\\>)?|\\*))?', 'ig');
const locationRegex = new RegExp(`(?:\\s+in\\s+(${captures.channel}|here|\\*))`, 'ig');
const appliesToRegex = new RegExp(`(?:\\s+for\\s(${captures.user}|${captures.role}|\\*))?`, 'ig');

class Enable extends Command {
constructor(bot) {
Expand All @@ -16,7 +16,7 @@ class Enable extends Command {
{ description: 'Enable a command for a role in a channel or channels', parameters: ['command id> in <channel> for <role|user'] },
];
this.regex = new RegExp(
`^${this.call}(?:\\s+(\\w*\\.*\\w*\\.*\\w*\\*?)(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here|\\*))?(?:\\s+for\\s((?:\\<\\@\\&?)?\\d+(?:\\>)?|\\*))?)?`,
`^${this.call}`,
'i',
);
this.requiresAuth = true;
Expand Down
4 changes: 2 additions & 2 deletions src/commands/Settings/Language.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const Command = require('../../models/Command.js');
const { getChannel } = require('../../CommonFunctions');
const { getChannel, captures } = require('../../CommonFunctions');

const languages = ['en-us'];

Expand All @@ -11,7 +11,7 @@ class Language extends Command {
this.usages = [
{ description: 'Change this channel\'s language', parameters: ['language'] },
];
this.regex = new RegExp(`^${this.call}\\s?(${languages.join('|')})?(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here))?$`, 'i');
this.regex = new RegExp(`^${this.call}\\s?(${languages.join('|')})?(?:\\s+in\\s+(${captures.channel}|here))?$`, 'i');
this.requiresAuth = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/Settings/Platform.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { getChannel } = require('../../CommonFunctions');
const { getChannel, captures } = require('../../CommonFunctions');

class Platform extends Command {
constructor(bot) {
super(bot, 'settings.platform', 'platform', 'Change a channel\'s platform');
this.usages = [
{ description: 'Change this channel\'s platform', parameters: ['platform'] },
];
this.regex = new RegExp(`${this.call}(?:\\s+(pc|ps4|xb1|swi))?(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here))?$`, 'i');
this.regex = new RegExp(`${this.call}(?:\\s+(pc|ps4|xb1|swi))?(?:\\s+in\\s+(${captures.channel}|here))?$`, 'i');
this.requiresAuth = true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/commands/Settings/Prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Prefix extends Command {
constructor(bot) {
super(bot, 'settings.prefix', 'prefix');
this.usages = [
{ description: 'Change this channel\'s platform', parameters: ['prefix (up to 3 characters)'] },
{ description: 'Change this channel\'s prefix', parameters: ['prefix (up to 3 characters)'] },
];
this.regex = new RegExp(
`^${this.call}(?:\\s+(.+))?`,
Expand Down
4 changes: 2 additions & 2 deletions src/commands/Settings/RespondToSettings.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
'use strict';

const Command = require('../../models/Command.js');
const { getChannel } = require('../../CommonFunctions');
const { getChannel, captures } = require('../../CommonFunctions');

class RespondToSettings extends Command {
constructor(bot) {
super(bot, 'settings.respondSettings', 'respond to settings', 'Toggle whether or not the bot tells you when settings change from your command.');
this.usages = [
{ description: 'Change if this channel has settings changes responded in it', parameters: ['response enabled'] },
];
this.regex = new RegExp('^respond(?:\\sto)?\\s?settings\\s?(on|off)?(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here))?$', 'i');
this.regex = new RegExp(`^respond(?:\\sto)?\\s?settings\\s?(on|off)?(?:\\s+in\\s+(${captures.channel}|here))?$`, 'i');
this.requiresAuth = true;
}

Expand Down
48 changes: 34 additions & 14 deletions src/commands/Settings/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ const { RichEmbed } = require('discord.js');
const Command = require('../../models/Command.js');
const { getChannels, createPageCollector } = require('../../CommonFunctions');

const negate = '✘';
const affirm = '✓';

const wrapChannelValue = (val) => {
if (val !== negate) {
return `<#${val}>`;
}
return val;
}

const wrapRoleValue = (val) => {
if (val !== negate) {
return `<@&${val}>`;
}
return val;
}

const chunkify = ({
string, newStrings = [], breakChar = '; ', maxLength = 1000,
}) => {
Expand Down Expand Up @@ -66,6 +83,8 @@ class Settings extends Command {
this.requiresAuth = true;
}



async composeChannelSettings(channel, message) {
const page = new RichEmbed(embedDefaults);
const settings = await this.settings.getChannelSettings(channel, [
Expand Down Expand Up @@ -108,8 +127,8 @@ class Settings extends Command {
page.addField('No Text Channel', await this.resolveBoolean(channel, 'defaultNoText', settings), true);
page.addField('Hidden Channel', await this.resolveBoolean(channel, 'defaultShown', settings), true);
page.addField('Respond to Settings', await this.resolveBoolean(channel, 'respond_to_settings', settings), true);
page.addField('Delete Message Post-Respond', await this.resolveBoolean(channel, 'delete_after_respond', settings), true);
page.addField('Delete Response Post-Respond', await this.resolveBoolean(channel, 'delete_response', settings), true);
page.addField('Delete Message', await this.resolveBoolean(channel, 'delete_after_respond', settings), true);
page.addField('Delete Response', await this.resolveBoolean(channel, 'delete_response', settings), true);

const defaultRoles = JSON.parse(settings.defaultRoles || '[]')
.map(roleId => channel.guild.roles.get(roleId) || undefined)
Expand All @@ -118,17 +137,18 @@ class Settings extends Command {
.join(', ');

if (message.guild) {
page.addField('Temp Channel Category', settings.tempCategory !== '0'
const tempCategory = settings.tempCategory !== '0'
&& typeof settings.tempCategory !== 'undefined'
? settings.tempCategory : '✘', true);
page.addField('LFG', settings.lfgChannel || '✘', true);
page.addField('Default Roles', defaultRoles.length ? defaultRoles : '✘', true);
page.addField('Vulgar Log', settings.vulgarLog || '✘', true);
page.addField('Delete Log', settings.msgDeleteLog || '✘', true);
page.addField('Leave Log', settings.memberRemoveLog || '✘', true);
page.addField('Ban Log', settings.banLog || '✘', true);
page.addField('Unban Log', settings.unbanLog || '✘', true);
page.addField('Mod Role', settings.modRole || '✘', true);
? settings.tempCategory : negate;
page.addField('Temp Channel Category', wrapChannelValue(tempCategory), true);
page.addField('LFG', wrapChannelValue(settings.lfgChannel || negate), true);
page.addField('Default Roles', defaultRoles.length ? defaultRoles : negate, true);
page.addField('Vulgar Log', wrapChannelValue(settings.vulgarLog || negate), true);
page.addField('Delete Log', wrapChannelValue(settings.msgDeleteLog || negate), true);
page.addField('Leave Log', wrapChannelValue(settings.memberRemoveLog || negate), true);
page.addField('Ban Log', wrapChannelValue(settings.banLog || negate), true);
page.addField('Unban Log', wrapChannelValue(settings.unbanLog || negate), true);
page.addField('Mod Role', wrapRoleValue(settings.modRole || negate), true);
}

const items = await this.settings.getTrackedItems(channel);
Expand All @@ -148,9 +168,9 @@ class Settings extends Command {

async resolveBoolean(channel, setting, settings) {
if (settings) {
return settings.setting === '1' ? '✓' : '✘';
return settings.setting === '1' ? affirm : negate;
}
return ((await this.settings.getChannelSetting(channel, setting)) === '1' ? '✓' : '✘');
return ((await this.settings.getChannelSetting(channel, setting)) === '1' ? affirm : negate);
}

async run(message) {
Expand Down
6 changes: 3 additions & 3 deletions src/commands/Tracking/SetPing.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const Command = require('../../models/Command.js');
const {
trackablesFromParameters, getTrackInstructionEmbed, trackablesCapture,
trackablesFromParameters, getTrackInstructionEmbed, captures,
} = require('../../CommonFunctions');

class SetPing extends Command {
Expand All @@ -11,13 +11,13 @@ class SetPing extends Command {
this.usages = [
{ description: 'Set ping for an event or item', parameters: ['event or reward', '@role or user mention'] },
];
this.regex = new RegExp(`^${this.call}\\s*(${trackablesCapture}(.+)?)?`, 'i');
this.regex = new RegExp(`^${this.call}\\s*(${captures.trackables}(.+)?)?`, 'i');
this.requiresAuth = true;
this.allowDM = false;
}

async run(message) {
const regex = new RegExp(`${trackablesCapture}(.+)?`, 'i');
const regex = new RegExp(`${captures.trackables}(.+)?`, 'i');
const match = message.content.match(regex);
if (message.channel.type === 'dm') {
this.messagemanager.reply(message, 'Operator, you can\'t do that privately, it\'s the same as directly messaging you anyway!');
Expand Down
4 changes: 2 additions & 2 deletions src/commands/Tracking/Track.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
trackablesFromParameters,
getChannel,
getTrackInstructionEmbed,
trackablesCapture,
captures,
} = require('../../CommonFunctions');

/**
Expand All @@ -19,7 +19,7 @@ class Track extends Command {
{ description: 'Show tracking command for tracking events', parameters: [] },
{ description: 'Track an event or events', parameters: ['event(s) to track'] },
];
this.regex = new RegExp(`^${this.call}(?:\\s+(${trackablesCapture})*)?(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here))?`, 'i');
this.regex = new RegExp(`^${this.call}(?:\\s+(${captures.trackables})*)?(?:\\s+in\\s+((?:\\<\\#)?\\d+(?:\\>)?|here))?`, 'i');
this.requiresAuth = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/Tracking/Untrack.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const {
trackablesFromParameters,
getChannel,
getTrackInstructionEmbed,
trackablesCapture,
captures
} = require('../../CommonFunctions');

/**
Expand All @@ -19,7 +19,7 @@ class Untrack extends Command {
{ description: 'Show tracking command for untracking events', parameters: [] },
{ description: 'Untrack an event or events', parameters: ['event(s) to untrack'] },
];
this.regex = new RegExp(`^${this.call}\\s*${trackablesCapture}*(?:\\s+in\\s+)?((?:\\<\\#)?\\d+(?:\\>)?|here)?`, 'i');
this.regex = new RegExp(`^${this.call}\\s*${captures.trackables}*(?:\\s+in\\s+)?((?:\\<\\#)?\\d+(?:\\>)?|here)?`, 'i');
this.requiresAuth = true;
}

Expand Down

0 comments on commit 63fdbef

Please sign in to comment.