Skip to content

Commit

Permalink
Add a /crq for command tab-completion (smogon#10468)
Browse files Browse the repository at this point in the history
  • Loading branch information
mia-pi-git authored Aug 3, 2024
1 parent 4765a5e commit cfefa23
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion server/chat-commands/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
/* eslint no-else-return: "error" */
import {Utils} from '../../lib';
import type {UserSettings} from '../users';
import type {GlobalPermission} from '../user-groups';
import type {GlobalPermission, RoomPermission} from '../user-groups';

export const crqHandlers: {[k: string]: Chat.CRQHandler} = {
userdetails(target, user, trustable) {
Expand Down Expand Up @@ -144,6 +144,37 @@ export const crqHandlers: {[k: string]: Chat.CRQHandler} = {

return targetRoom.battle.format;
},
cmdsearch(target, user, trustable) {
// in no world should ths be a thing. our longest command name is 37 chars
if (target.length > 40) return null;
const cmdPrefix = target.charAt(0);
if (!['/', '!'].includes(cmdPrefix)) return null;
target = toID(target.slice(1));

const results = [];
for (const command of Chat.allCommands()) {
if (cmdPrefix === '!' && !command.broadcastable) continue;
const req = command.requiredPermission as GlobalPermission;
if (req &&
!(command.hasRoomPermissions ? this.room && user.can(req as RoomPermission, null, this.room) : user.can(req))
) {
continue;
}
const cmds = [
command.fullCmd,
...command.aliases.map(x => command.fullCmd.replace(command.cmd, `${x}`)),
];
for (const cmd of cmds) {
if (toID(cmd).startsWith(target)) {
results.push(cmdPrefix + cmd);
break;
}
}
// limit number of results to prevent spam
if (results.length >= 20) break;
}
return results;
},
};

export const commands: Chat.ChatCommands = {
Expand Down

0 comments on commit cfefa23

Please sign in to comment.