mirror of
https://github.com/qwerinope/qweribot.git
synced 2025-12-19 08:41:39 +01:00
create stable command parser
This commit is contained in:
@@ -1,8 +1,9 @@
|
|||||||
import { Command, sendMessage } from ".";
|
import { Command, sendMessage } from ".";
|
||||||
import items from "../items";
|
import items from "../items";
|
||||||
|
import parseCommandArgs from "../lib/parseCommandArgs";
|
||||||
|
|
||||||
export default new Command('iteminfo', ['iteminfo', 'itemhelp', 'info'], [], async msg => {
|
export default new Command('iteminfo', ['iteminfo', 'itemhelp', 'info'], [], async msg => {
|
||||||
const messagequery = msg.messageText.trim().split(' ').slice(1).join(' ');
|
const messagequery = parseCommandArgs(msg.messageText).join(' ');
|
||||||
if (!messagequery) { await sendMessage('Please specify an item you would like to get info about', msg.messageId); return; };
|
if (!messagequery) { await sendMessage('Please specify an item you would like to get info about', msg.messageId); return; };
|
||||||
const selection = items.get(messagequery.toLowerCase());
|
const selection = items.get(messagequery.toLowerCase());
|
||||||
if (!selection) { await sendMessage(`'${messagequery}' is not an item`, msg.messageId); return; };
|
if (!selection) { await sendMessage(`'${messagequery}' is not an item`, msg.messageId); return; };
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Item } from ".";
|
import { Item } from ".";
|
||||||
import { sendMessage } from "../commands";
|
import { sendMessage } from "../commands";
|
||||||
|
import parseCommandArgs from "../lib/parseCommandArgs";
|
||||||
import { timeout } from "../lib/timeout";
|
import { timeout } from "../lib/timeout";
|
||||||
import { User } from "../user";
|
import { User } from "../user";
|
||||||
|
|
||||||
@@ -7,8 +8,7 @@ export default new Item('blaster', 'Blaster', 's',
|
|||||||
'Times a specific person out for 60 seconds',
|
'Times a specific person out for 60 seconds',
|
||||||
['blaster', 'blast'], ['moderator:manage:banned_users'],
|
['blaster', 'blast'], ['moderator:manage:banned_users'],
|
||||||
async (msg, user) => {
|
async (msg, user) => {
|
||||||
const slicecount = msg.messageText.startsWith('!use') ? 2 : 1;
|
const messagequery = parseCommandArgs(msg.messageText);
|
||||||
const messagequery = msg.messageText.trim().split(' ').slice(slicecount);
|
|
||||||
if (!messagequery[0]) { await sendMessage('Please specify a target'); return; };
|
if (!messagequery[0]) { await sendMessage('Please specify a target'); return; };
|
||||||
const target = await User.initUsername(messagequery[0].toLowerCase());
|
const target = await User.initUsername(messagequery[0].toLowerCase());
|
||||||
if (!target) { await sendMessage(`${messagequery[0]} doesn't exist`); return; };
|
if (!target) { await sendMessage(`${messagequery[0]} doesn't exist`); return; };
|
||||||
|
|||||||
9
bot/lib/parseCommandArgs.ts
Normal file
9
bot/lib/parseCommandArgs.ts
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
import { commandPrefix } from "..";
|
||||||
|
|
||||||
|
/** Helper function to extract arguments from commands */
|
||||||
|
export default function parseCommandArgs(input: string) {
|
||||||
|
const a = input.slice(commandPrefix.length);
|
||||||
|
const sliceLength = a.startsWith('use') ? 2 : 1;
|
||||||
|
const b = a.trim().split(' ').slice(sliceLength);
|
||||||
|
return b;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user