Skip to content

Commit

Permalink
Merge pull request #70 from memochou1993/feature/command-aliases
Browse files Browse the repository at this point in the history
Fix commands
  • Loading branch information
memochou1993 authored Dec 23, 2022
2 parents 24a0f1d + 900694c commit b28ef2b
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
2 changes: 1 addition & 1 deletion app/commands/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { isContinue } from './continue.js';
* @param {Event} event
* @returns {boolean}
*/
const isChatCommand = (event) => event.isCommand(COMMAND_CHAT);
const isChatCommand = (event) => event.hasCommand(COMMAND_CHAT);

/**
* @param {Event} event
Expand Down
2 changes: 1 addition & 1 deletion app/commands/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Event from '../event.js';
* @param {Event} event
* @returns {boolean}
*/
const isDrawCommand = (event) => event.isCommand(COMMAND_DRAW);
const isDrawCommand = (event) => event.hasCommand(COMMAND_DRAW);

/**
* @param {Event} event
Expand Down
30 changes: 23 additions & 7 deletions app/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,29 @@ class Event {
text,
aliases,
}) {
if (this.isMessage && this.isText) {
const command = this.message.text.split(' ').shift().toLowerCase();
if (text.toLowerCase() === command) return true;
if (aliases.some((alias) => alias.toLowerCase() === command)) return true;
if (this.message.text.includes(text.toLowerCase())) return true;
if (aliases.some((alias) => this.message.text.startsWith(alias.toLowerCase()))) return true;
}
if (!this.isMessage || !this.isText) return false;
const input = this.message.text.trim().toLowerCase().replaceAll(' ', ' ');
if (input === text.toLowerCase()) return true;
if (aliases.some((alias) => input === alias.toLowerCase())) return true;
return false;
}

/**
* @param {Object} param
* @param {string} param.text
* @param {Array<string>} param.aliases
* @returns {boolean}
*/
hasCommand({
text,
aliases,
}) {
if (!this.isMessage || !this.isText) return false;
const input = this.message.text.trim().toLowerCase().replaceAll(' ', ' ');
if (input === text.toLowerCase()) return false;
if (aliases.some((alias) => input.split(' ').shift() === alias.toLowerCase())) return true;
if (input.startsWith(text.toLowerCase())) return true;
if (input.endsWith(text.toLowerCase())) return true;
return false;
}

Expand Down
2 changes: 1 addition & 1 deletion constants/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const COMMAND_CHAT = Object.freeze({
aliases: [
'AI',
'/ai',
'/chat',
],
});

Expand All @@ -33,7 +34,6 @@ export const COMMAND_SETTINGS = Object.freeze({
text: t('__COMMAND_SETTINGS_TEXT'),
reply: '',
aliases: [
'/',
'/settings',
],
});
Expand Down

0 comments on commit b28ef2b

Please sign in to comment.