From 4a716f145617d00d2463ca28a292e05aa47e72ca Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Sun, 23 May 2021 16:30:22 -0700 Subject: [PATCH] Use aliases for !formathelp broadcast cooldown (This makes it so if someone uses `!om sketch` it will prevent use of `!om sketchmons` for a while.) Suggestion thread: https://www.smogon.com/forums/threads/make-the-broadcast-cooldown-for-commands-apply-to-aliases-of-that-command.3684569/ --- server/chat-commands/info.ts | 39 +++++++++++++++++-------------- server/chat-plugins/othermetas.ts | 29 ++++++++++------------- server/chat.ts | 17 ++++++++------ 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/server/chat-commands/info.ts b/server/chat-commands/info.ts index cb9c8aeb1f..ea025243da 100644 --- a/server/chat-commands/info.ts +++ b/server/chat-commands/info.ts @@ -1792,40 +1792,43 @@ export const commands: Chat.ChatCommands = { const {totalMatches, sections} = findFormats(targetId, isOMSearch); if (!totalMatches) return this.errorReply("No matched formats found."); - if (!this.runBroadcast()) return; - if (totalMatches === 1) { + + const format = totalMatches === 1 ? Dex.formats.get(Object.values(sections)[0].formats[0]) : null; + + if (!this.runBroadcast(`!formathelp ${format ? format.id : target}`)) return; + + if (format) { const rules: string[] = []; let rulesetHtml = ''; - const subformat = Dex.formats.get(Object.values(sections)[0].formats[0]); - if (['Format', 'Rule', 'ValidatorRule'].includes(subformat.effectType)) { - if (subformat.ruleset?.length) { - rules.push(`Ruleset - ${Utils.escapeHTML(subformat.ruleset.join(", "))}`); + if (['Format', 'Rule', 'ValidatorRule'].includes(format.effectType)) { + if (format.ruleset?.length) { + rules.push(`Ruleset - ${Utils.escapeHTML(format.ruleset.join(", "))}`); } - if (subformat.banlist?.length) { - rules.push(`Bans - ${Utils.escapeHTML(subformat.banlist.join(", "))}`); + if (format.banlist?.length) { + rules.push(`Bans - ${Utils.escapeHTML(format.banlist.join(", "))}`); } - if (subformat.unbanlist?.length) { - rules.push(`Unbans - ${Utils.escapeHTML(subformat.unbanlist.join(", "))}`); + if (format.unbanlist?.length) { + rules.push(`Unbans - ${Utils.escapeHTML(format.unbanlist.join(", "))}`); } - if (subformat.restricted?.length) { - rules.push(`Restricted - ${Utils.escapeHTML(subformat.restricted.join(", "))}`); + if (format.restricted?.length) { + rules.push(`Restricted - ${Utils.escapeHTML(format.restricted.join(", "))}`); } if (rules.length > 0) { rulesetHtml = `
Banlist/Ruleset${rules.join("
")}
`; } else { - rulesetHtml = `No ruleset found for ${subformat.name}`; + rulesetHtml = `No ruleset found for ${format.name}`; } } - let formatType: string = (subformat.gameType || "singles"); + let formatType: string = (format.gameType || "singles"); formatType = formatType.charAt(0).toUpperCase() + formatType.slice(1).toLowerCase(); - if (!subformat.desc && !subformat.threads) { - if (subformat.effectType === 'Format') { - return this.sendReplyBox(`No description found for this ${formatType} ${subformat.section} format.
${rulesetHtml}`); + if (!format.desc && !format.threads) { + if (format.effectType === 'Format') { + return this.sendReplyBox(`No description found for this ${formatType} ${format.section} format.
${rulesetHtml}`); } else { return this.sendReplyBox(`No description found for this rule.
${rulesetHtml}`); } } - const descHtml = [...(subformat.desc ? [subformat.desc] : []), ...(subformat.threads || [])]; + const descHtml = [...(format.desc ? [format.desc] : []), ...(format.threads || [])]; return this.sendReplyBox(`${descHtml.join("
")}
${rulesetHtml}`); } diff --git a/server/chat-plugins/othermetas.ts b/server/chat-plugins/othermetas.ts index f51d37a447..9fbbe0f2d3 100644 --- a/server/chat-plugins/othermetas.ts +++ b/server/chat-plugins/othermetas.ts @@ -47,34 +47,29 @@ function getMegaStone(stone: string, mod = 'gen8'): Item | null { export const commands: Chat.ChatCommands = { om: 'othermetas', othermetas(target, room, user) { - this.runBroadcast(); target = toID(target); - let buffer = ``; + const omLink = `- Other Metagames Forum
`; - if (target === 'all' && this.broadcasting) { - throw new Chat.ErrorMessage(`You cannot broadcast information about all Other Metagames at once.`); + if (!target) { + this.runBroadcast(); + return this.sendReplyBox(omLink); } - if (!target || target === 'all') { - buffer += `- Other Metagames Forum
`; - if (!target) return this.sendReplyBox(buffer); - } - const showMonthly = (target === 'all' || target === 'omofthemonth' || target === 'month'); - if (target === 'all') { + this.runBroadcast(); + if (this.broadcasting) { + throw new Chat.ErrorMessage(`You cannot broadcast information about all Other Metagames at once.`); + } // Display OMotM formats, with forum thread links as caption this.parse(`/formathelp omofthemonth`); // Display the rest of OM formats, with OM hub/index forum links as caption this.parse(`/formathelp othermetagames`); - return this.sendReply(`|raw|
${buffer}
`); - } - if (showMonthly) { - this.target = 'omofthemonth'; - this.run('formathelp'); - } else { - this.run('formathelp'); + return this.sendReply(`|raw|
${omLink}
`); } + + if (target === 'month') this.target = 'omofthemonth'; + this.run('formathelp'); }, othermetashelp: [ `/om - Provides links to information on the Other Metagames.`, diff --git a/server/chat.ts b/server/chat.ts index da79869ae7..72f5956dee 100644 --- a/server/chat.ts +++ b/server/chat.ts @@ -956,7 +956,7 @@ export class CommandContext extends MessageContext { shouldBroadcast() { return this.cmdToken === BROADCAST_TOKEN; } - checkBroadcast(ignoreCooldown?: boolean, suppressMessage?: string | null) { + checkBroadcast(overrideCooldown?: boolean | string, suppressMessage?: string | null) { if (this.broadcasting || !this.shouldBroadcast()) { return true; } @@ -973,10 +973,13 @@ export class CommandContext extends MessageContext { // broadcast cooldown const broadcastMessage = (suppressMessage || this.message).toLowerCase().replace(/[^a-z0-9\s!,]/g, ''); + const cooldownMessage = overrideCooldown === true ? null : (overrideCooldown || broadcastMessage); - if (!ignoreCooldown && this.room && this.room.lastBroadcast === broadcastMessage && + if ( + cooldownMessage && this.room && this.room.lastBroadcast === cooldownMessage && this.room.lastBroadcastTime >= Date.now() - BROADCAST_COOLDOWN && - !this.user.can('bypassall')) { + !this.user.can('bypassall') + ) { throw new Chat.ErrorMessage("You can't broadcast this because it was just broadcasted."); } @@ -990,7 +993,7 @@ export class CommandContext extends MessageContext { this.broadcastMessage = broadcastMessage; return true; } - runBroadcast(ignoreCooldown = false, suppressMessage: string | null = null) { + runBroadcast(overrideCooldown?: boolean | string, suppressMessage: string | null = null) { if (this.broadcasting || !this.shouldBroadcast()) { // Already being broadcast, or the user doesn't intend to broadcast. return true; @@ -998,7 +1001,7 @@ export class CommandContext extends MessageContext { if (!this.broadcastMessage) { // Permission hasn't been checked yet. Do it now. - this.checkBroadcast(ignoreCooldown, suppressMessage); + this.checkBroadcast(overrideCooldown, suppressMessage); } this.broadcasting = true; @@ -1012,8 +1015,8 @@ export class CommandContext extends MessageContext { // We don't want broadcasted messages in a room to be translated // according to a user's personal language setting. this.language = this.room.settings.language || null; - if (!ignoreCooldown) { - this.room.lastBroadcast = this.broadcastMessage; + if (overrideCooldown !== true) { + this.room.lastBroadcast = overrideCooldown || this.broadcastMessage; this.room.lastBroadcastTime = Date.now(); } }