Skip to content

Commit

Permalink
CommandContext: Make requireRoom support specifying a room
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git committed Oct 20, 2020
1 parent 5b387ae commit 68a2be8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 77 deletions.
3 changes: 1 addition & 2 deletions server/chat-plugins/tcgtabletop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
16 changes: 5 additions & 11 deletions server/chat-plugins/thecafe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -102,8 +100,7 @@ export const commands: ChatCommands = {
return this.sendReplyBox(`<div class="ladder"><table style="text-align:center;"><tr><th colspan="7" style="font-size:10pt;">Your dish is: <u>${newDish}</u></th></tr><tr><th>Team</th>${team.map(mon => `<td><psicon pokemon="${mon}"/> ${mon}</td>`).join('')}</tr><tr><th>Ingredients</th>${newIngredients.map(ingredient => `<td>${ingredient}</td>`).join('')}</tr>${importStr}</table></div>`);
},
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.`);
Expand All @@ -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(',');
Expand Down Expand Up @@ -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);
Expand All @@ -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`);
},
Expand Down
16 changes: 4 additions & 12 deletions server/chat-plugins/trivia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,6 @@ if (triviaData.questions.some(q => !('type' in q))) {
/** from:to Map */
export const pendingAltMerges = new Map<ID, ID>();

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.`);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
54 changes: 20 additions & 34 deletions server/chat-plugins/wifi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.");

Expand All @@ -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.");
Expand All @@ -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.");
Expand All @@ -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());
Expand Down Expand Up @@ -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;
Expand All @@ -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(
Expand All @@ -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.");
Expand All @@ -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.");
Expand All @@ -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.");
Expand All @@ -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);

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
27 changes: 10 additions & 17 deletions server/chat-plugins/youtube.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string[] | undefined> {
const raw = await Net(`${ROOT}search`).get({
query: {
Expand All @@ -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();
Expand All @@ -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.');
Expand All @@ -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.`);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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.`);
Expand All @@ -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`);
Expand All @@ -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`);
Expand All @@ -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());
Expand All @@ -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) {
Expand Down

0 comments on commit 68a2be8

Please sign in to comment.