Have you ever wanted to manage your Discord guilds in a SQL-like fashion? No? Oh well, anyway...
This is a bot which enables you to use a custom, domain-specific query language designed for no-coding management purposes. Wanna add nicknames to all users in a given channel? Just type /map member in get "members" from #channel into rename member to "New nickname!"
(command syntax is WIP and a subject to change)!
This project uses Node 14 and npm 6.
- Clone this repository.
- Run
npm i
to install packages. - After installation, you will be prompted to enter your bot API token. This can be found on Discord dev portal in your selected app under "Bot" tab.
- Run
npm run start
to initialize the bot.
I use something which is called a parser generator, more precisely pegjs, which takes in a special input format called a grammar and outputs a code of a parser. A parser is a special type of function which takes some string input, tokenizes, recognizes patters defined in grammar and runs code applicable to those patterns.
There's a file called grammar.pegjs with the grammar definition inside and a script generateParser.ts, which loads this grammar file, uses the parser generator and outputs the created grammar as just another project file, from which the results are imported into the rest of the code.
When a message comes in, it's parsed by the parser function and outputs a command, which is then executed and its result sent back to the user.
- A message comes in.
- Parser builds an AST (abstract syntax tree) from the message contents, which consists of nested
Commands
,Values
and their arguments. It initializes anExecutionContext
which holds any command-runtime variable references. Classes used in AST are defined in parser subdirectories. - The root
Command
is executed. It callsresolve
on any of itsValue
-type arguments, which leads to its child being executed and so on. The result values go up in the execution tree, carrying information about the query, as well as the errors.