diff --git a/server/chat-plugins/tcgtabletop.ts b/server/chat-plugins/tcgtabletop.ts index 027a3cc829478..968ef18038582 100644 --- a/server/chat-plugins/tcgtabletop.ts +++ b/server/chat-plugins/tcgtabletop.ts @@ -46,8 +46,7 @@ export const commands: ChatCommands = { ygo: 'yugioh', yugioh(target, room, user) { this.checkBroadcast(); - room = this.requireRoom(); - if (room.roomid !== 'tcgtabletop') return this.errorReply("This command can only be used in the TCG & Tabletop room."); + room = this.requireRoom('tcgtabletop' as RoomID); const subdomain = 'yugioh'; const query = target.trim(); if (!query) return this.parse('/help yugioh'); diff --git a/server/chat-plugins/thecafe.ts b/server/chat-plugins/thecafe.ts index ba0d03ec2af8b..11ecdb226f565 100644 --- a/server/chat-plugins/thecafe.ts +++ b/server/chat-plugins/thecafe.ts @@ -74,9 +74,7 @@ function generateDish(): [string, string[]] { export const commands: ChatCommands = { foodfight(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== thecafe.roomid) return this.errorReply("This command is only available in The Café."); - + room = this.requireRoom(thecafe.roomid); if (!Object.keys(dishes).length) return this.errorReply("No dishes found. Add some dishes first."); if (user.foodfight && user.foodfight.timestamp + FOODFIGHT_COOLDOWN > Date.now()) { @@ -102,8 +100,7 @@ export const commands: ChatCommands = { return this.sendReplyBox(`
${team.map(mon => ``).join('')}${newIngredients.map(ingredient => ``).join('')}${importStr}
Your dish is: ${newDish}
Team ${mon}
Ingredients${ingredient}
`); }, checkfoodfight(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== thecafe.roomid) return this.errorReply("This command is only available in The Café."); + room = this.requireRoom(thecafe.roomid); const targetUser = this.targetUserOrSelf(target, false); if (!targetUser) return this.errorReply(`User ${this.targetUsername} not found.`); @@ -116,8 +113,7 @@ export const commands: ChatCommands = { }, addingredients: 'adddish', adddish(target, room, user, connection, cmd) { - room = this.requireRoom(); - if (room.roomid !== thecafe.roomid) return this.errorReply("This command is only available in The Café."); + room = this.requireRoom(thecafe.roomid); this.checkCan('mute', null, room); let [dish, ...ingredients] = target.split(','); @@ -151,8 +147,7 @@ export const commands: ChatCommands = { this.sendReply(`${cmd.slice(3)} '${dish}: ${ingredients.join(', ')}' added successfully.`); }, removedish(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== thecafe.roomid) return this.errorReply("This command is only available in The Café."); + room = this.requireRoom(thecafe.roomid); this.checkCan('mute', null, room); const id = toID(target); @@ -164,8 +159,7 @@ export const commands: ChatCommands = { this.sendReply(`Dish '${target}' deleted successfully.`); }, viewdishes(target, room, user, connection) { - room = this.requireRoom(); - if (room.roomid !== thecafe.roomid) return this.errorReply("This command is only available in The Café."); + room = this.requireRoom(thecafe.roomid); return this.parse(`/join view-foodfight`); }, diff --git a/server/chat-plugins/trivia.ts b/server/chat-plugins/trivia.ts index 9c633d17b598b..6dd972c3e8b2a 100644 --- a/server/chat-plugins/trivia.ts +++ b/server/chat-plugins/trivia.ts @@ -144,10 +144,6 @@ if (triviaData.questions.some(q => !('type' in q))) { /** from:to Map */ export const pendingAltMerges = new Map(); -function isTriviaRoom(room: Room) { - return room.roomid === 'trivia'; -} - function getTriviaGame(room: Room | null) { if (!room) { throw new Chat.ErrorMessage(`This command can only be used in the Trivia room.`); @@ -1490,8 +1486,7 @@ const triviaCommands: ChatCommands = { new(target, room, user, connection, cmd) { const randomizeQuestionOrder = !cmd.includes('sorted'); - room = this.requireRoom(); - if (!isTriviaRoom(room)) return this.errorReply(this.tr`This command can only be used in the Trivia room.`); + room = this.requireRoom(`trivia` as RoomID); this.checkCan('show', null, room); this.checkChat(); if (room.game) { @@ -2102,8 +2097,7 @@ const triviaCommands: ChatCommands = { ], rank(target, room, user) { - room = this.requireRoom(); - if (!isTriviaRoom(room)) return this.errorReply(this.tr("This command can only be used in Trivia.")); + room = this.requireRoom('trivia' as RoomID); let name; let userid; @@ -2135,8 +2129,7 @@ const triviaCommands: ChatCommands = { alltimeladder: 'ladder', ladder(target, room, user, connection, cmd) { - room = this.requireRoom(); - if (!isTriviaRoom(room)) return this.errorReply('This command can only be used in Trivia.'); + room = this.requireRoom('trivia' as RoomID); if (!this.runBroadcast()) return false; const cache = cmd === 'ladder' ? cachedAltLadder : cachedLadder; const {ladder} = cache.get(); @@ -2368,8 +2361,7 @@ const mastermindCommands: ChatCommands = { end: triviaCommands.end, new(target, room, user) { - room = this.requireRoom(); - if (!isTriviaRoom(room)) return this.errorReply(this.tr`This command can only be used in the Trivia room.`); + room = this.requireRoom('trivia' as RoomID); this.checkCan('show', null, room); const finalists = parseInt(target); diff --git a/server/chat-plugins/wifi.ts b/server/chat-plugins/wifi.ts index 95b35d647faf0..c56ed54db50d7 100644 --- a/server/chat-plugins/wifi.ts +++ b/server/chat-plugins/wifi.ts @@ -632,8 +632,8 @@ const cmds: ChatCommands = { quiz: 'question', qg: 'question', question(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi' || !target) return false; + room = this.requireRoom('wifi' as RoomID); + if (!target) return false; if (room.giveaway) return this.errorReply("There is already a giveaway going on!"); let [giver, ot, tid, prize, question, ...answers] = target.split(target.includes('|') ? '|' : ',').map( @@ -661,8 +661,7 @@ const cmds: ChatCommands = { }, changeanswer: 'changequestion', changequestion(target, room, user, conn, cmd) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return false; + room = this.requireRoom('wifi' as RoomID); if (!room.giveaway) return this.errorReply("There is no giveaway going on at the moment."); if (room.giveaway.type !== 'question') return this.errorReply("This is not a question giveaway."); @@ -672,8 +671,7 @@ const cmds: ChatCommands = { }, showanswer: 'viewanswer', viewanswer(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return false; + room = this.requireRoom('wifi' as RoomID); const giveaway = room.giveaway as QuestionGiveaway; if (!giveaway) return this.errorReply("There is no giveaway going on at the moment."); if (giveaway.type !== 'question') return this.errorReply("This is not a question giveaway."); @@ -684,8 +682,7 @@ const cmds: ChatCommands = { }, guessanswer: 'guess', guess(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); this.checkChat(); if (!room.giveaway) return this.errorReply("There is no giveaway going on at the moment."); if (room.giveaway.type !== 'question') return this.errorReply("This is not a question giveaway."); @@ -696,8 +693,8 @@ const cmds: ChatCommands = { lg: 'lottery', lotto: 'lottery', lottery(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi' || !target) return false; + room = this.requireRoom('wifi' as RoomID); + if (!target) return false; if (room.giveaway) return this.errorReply("There is already a giveaway going on!"); let [giver, ot, tid, prize, winners] = target.split(target.includes('|') ? '|' : ',').map(param => param.trim()); @@ -735,8 +732,7 @@ const cmds: ChatCommands = { joinlotto: 'join', joinlottery: 'join', join(target, room, user, conn, cmd) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); this.checkChat(); if (user.semilocked) return; const giveaway = room.giveaway as LotteryGiveaway; @@ -760,8 +756,8 @@ const cmds: ChatCommands = { gts: { new: 'start', start(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi' || !target) return false; + room = this.requireRoom('wifi' as RoomID); + if (!target) return false; if (room.gtsga) return this.errorReply("There is already a GTS giveaway going on!"); const [giver, amountStr, summary, deposit, lookfor] = target.split(target.includes('|') ? '|' : ',').map( @@ -788,8 +784,7 @@ const cmds: ChatCommands = { this.modlog('GTS GIVEAWAY', null, `for ${targetUser.getLastId()} with ${amount} Pokémon`); }, left(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return false; + room = this.requireRoom('wifi' as RoomID); if (!room.gtsga) return this.errorReply("There is no GTS giveaway going on!"); if (!user.can('warn', null, room) && user !== room.gtsga.giver) { return this.errorReply("Only the host or a staff member can update GTS giveaways."); @@ -810,8 +805,7 @@ const cmds: ChatCommands = { room.gtsga.updateLeft(newamount); }, sent(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return false; + room = this.requireRoom('wifi' as RoomID); if (!room.gtsga) return this.errorReply("There is no GTS giveaway going on!"); if (!user.can('warn', null, room) && user !== room.gtsga.giver) { return this.errorReply("Only the host or a staff member can update GTS giveaways."); @@ -822,8 +816,7 @@ const cmds: ChatCommands = { room.gtsga.updateSent(target); }, full(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return false; + room = this.requireRoom('wifi' as RoomID); if (!room.gtsga) return this.errorReply("There is no GTS giveaway going on!"); if (!user.can('warn', null, room) && user !== room.gtsga.giver) { return this.errorReply("Only the host or a staff member can update GTS giveaways."); @@ -833,8 +826,7 @@ const cmds: ChatCommands = { room.gtsga.stopDeposits(); }, end(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); if (!room.gtsga) return this.errorReply("There is no GTS giveaway going on at the moment."); this.checkCan('warn', null, room); @@ -850,8 +842,7 @@ const cmds: ChatCommands = { // general. ban(target, room, user) { if (!target) return false; - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); this.checkCan('warn', null, room); target = this.splitTarget(target, false); @@ -872,8 +863,7 @@ const cmds: ChatCommands = { }, unban(target, room, user) { if (!target) return false; - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); this.checkCan('warn', null, room); this.splitTarget(target, false); @@ -889,8 +879,7 @@ const cmds: ChatCommands = { }, stop: 'end', end(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); if (!room.giveaway) return this.errorReply("There is no giveaway going on at the moment."); if (user.id !== room.giveaway.host.id) this.checkCan('warn', null, room); @@ -904,8 +893,7 @@ const cmds: ChatCommands = { }, rm: 'remind', remind(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); const giveaway = room.giveaway; if (!giveaway) return this.errorReply("There is no giveaway going on at the moment."); if (!this.runBroadcast()) return; @@ -917,8 +905,7 @@ const cmds: ChatCommands = { } }, count(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); target = [...Giveaway.getSprite(target)[0]][0]; if (!target) return this.errorReply("No mon entered - /giveaway count pokemon."); if (!this.runBroadcast()) return; @@ -932,8 +919,7 @@ const cmds: ChatCommands = { }, '': 'help', help(target, room, user) { - room = this.requireRoom(); - if (room.roomid !== 'wifi') return this.errorReply("This command can only be used in the Wi-Fi room."); + room = this.requireRoom('wifi' as RoomID); let reply = ''; switch (target) { diff --git a/server/chat-plugins/youtube.ts b/server/chat-plugins/youtube.ts index 98ee8a86e458c..93d2211bdc1fc 100644 --- a/server/chat-plugins/youtube.ts +++ b/server/chat-plugins/youtube.ts @@ -225,13 +225,6 @@ export class YoutubeInterface { save() { return FS(STORAGE_PATH).writeUpdate(() => JSON.stringify(this.data)); } - getRoom(context: CommandContext) { - const room = context.requireRoom(); - if (room.roomid !== 'youtube') { - throw new Chat.ErrorMessage(`This command can only be used in the YouTube room.`); - } - return room; - } async searchVideo(name: string, limit?: number): Promise { const raw = await Net(`${ROOT}search`).get({ query: { @@ -258,7 +251,7 @@ export const YouTube = new YoutubeInterface(channelData); export const commands: ChatCommands = { async randchannel(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); if (Object.keys(YouTube.data.channels).length < 1) return this.errorReply(`No channels in the database.`); target = toID(target); this.runBroadcast(); @@ -270,7 +263,7 @@ export const commands: ChatCommands = { yt: 'youtube', youtube: { async addchannel(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); let [id, name] = target.split(','); if (name) name = name.trim(); if (!id) return this.errorReply('Specify a channel ID.'); @@ -283,7 +276,7 @@ export const commands: ChatCommands = { addchannelhelp: [`/addchannel - Add channel data to the YouTube database. Requires: % @ #`], removechannel(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('mute', null, room); const id = YouTube.channelSearch(target); if (!id) return this.errorReply(`Channel with ID or name ${target} not found.`); @@ -295,7 +288,7 @@ export const commands: ChatCommands = { removechannelhelp: [`/youtube removechannel - Delete channel data from the YouTube database. Requires: % @ #`], async channel(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); const channel = YouTube.channelSearch(target); if (!channel) return this.errorReply(`No channels with ID or name ${target} found.`); const data = await YouTube.generateChannelDisplay(channel); @@ -322,7 +315,7 @@ export const commands: ChatCommands = { }, update(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('mute', null, room); const [channel, name] = target.split(','); const id = YouTube.channelSearch(channel); @@ -334,7 +327,7 @@ export const commands: ChatCommands = { }, interval: 'repeat', repeat(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('declare', null, room); if (!target) return this.sendReply(`Interval is currently set to ${Chat.toDurationString(YouTube.intervalTime)}.`); if (Object.keys(channelData).length < 1) return this.errorReply(`No channels in the database.`); @@ -356,7 +349,7 @@ export const commands: ChatCommands = { return this.modlog(`CHANNELINTERVAL`, null, `${target} minutes`); }, addcategory(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('ban', null, room); const categoryID = toID(target); if (!categoryID) return this.parse(`/help youtube`); @@ -369,7 +362,7 @@ export const commands: ChatCommands = { YouTube.save(); }, removecategory(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('ban', null, room); const categoryID = toID(target); if (!categoryID) return this.parse(`/help youtube`); @@ -386,7 +379,7 @@ export const commands: ChatCommands = { this.modlog(`YOUTUBE REMOVECATEGORY`, null, target); }, setcategory(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('ban', null, room); target = target.trim(); const [category, id] = Utils.splitFirst(target, ',').map(item => item.trim()); @@ -405,7 +398,7 @@ export const commands: ChatCommands = { this.privateModAction(`${user.name} set the channel ${channel.name}'s category to '${category}'.`); }, decategorize(target, room, user) { - room = YouTube.getRoom(this); + room = this.requireRoom('youtube' as RoomID); this.checkCan('ban', null, room); target = target.trim(); if (!target) { diff --git a/server/chat.ts b/server/chat.ts index 9e9bd6b4dcbbc..306daa05d135c 100644 --- a/server/chat.ts +++ b/server/chat.ts @@ -1338,10 +1338,17 @@ export class CommandContext extends MessageContext { return rest; } - requireRoom() { + requireRoom(id?: RoomID) { if (!this.room) { throw new Chat.ErrorMessage(`/${this.cmd} - must be used in a chat room, not a ${this.pmTarget ? "PM" : "console"}`); } + if (id && this.room.roomid !== id) { + const targetRoom = Rooms.get(id); + if (!targetRoom) { + throw new Chat.ErrorMessage(`This command can only be used in the ${id} room, but that room does not exist.`); + } + throw new Chat.ErrorMessage(`This command can only be used in the ${targetRoom.title} room.`); + } return this.room; } commandDoesNotExist(): never {