Skip to content

[FAQ] command prefix

Housa Hitomi edited this page May 29, 2022 · 6 revisions

TL; DR

  1. change prefix from '/'(this is default prefix) -> '.' & '。'
@bot.command(prefixes=['.', '。'])
async def foo(msg: Message):
    ...
  1. change prefix globally:
bot.command.update_prefixes('.', '。')

insert this snippet after all command were registered(typically before bot.run())

besides, update_prefixes() can be called in command


prefixes in khl.py command are specific to those Commands with DefaultLexer

Lexer

what's lexer?

lexer is a component to tear down Message content into tokens, for convenience in making powerful command matching and parsing

lexer is designed according to lexical analysis(wikipedia)

Lexer in khl.py

now, there are two kinds of lexer provided by official khl.py:

  • DefaultLexer
    • implemented via python package shlex, acts like shell
  • RELexer
    • implemented via regex, acts as what user defines

Why prefixes only available in DefaultLexer

due to current design.

since using a prefix to invoke a command is ubiquitous in games/bots/shells, khl.py introduces prefix into Command to invoke command as well

but commands using other lexer(such as RELexer) control invoking themselves (e.g. RELexer uses regexp defined by user to invoke)

so prefixes only available in DefaultLexer