TravBot-v3/docs/Documentation.md

118 lines
6.8 KiB
Markdown
Raw Normal View History

# Table of Contents
2020-10-15 09:23:24 +00:00
2021-04-07 10:58:09 +00:00
- [Structure](#structure)
- [Version Numbers](#version-numbers)
- [Message Subcommand Type](#message-subcommand-type)
- [Command Menu](#command-menu)
- [Command Metadata](#command-metadata)
- [Command Var String](#command-var-string)
- [Utility Functions](#utility-functions)
2020-08-30 21:26:18 +00:00
# Structure
2020-10-15 09:23:24 +00:00
- `src`: Contains all the code for the bot itself. Code directly in this folder is for the starting index file as well as commonly accessed utility files.
- `core`: This is currently where the command handler is. Try to keep it as isolated as possible, it might split off to become its own module.
- `commands`: Where all the dynamically loaded commands are stored. You can use a subfolder to specify the command category. Specify a `modules` folder to create files that are ignored by the command loader.
- `modules`: This is where mostly single-purpose blocks of code go. (This is **not** the same as a `modules` folder under `commands`.)
- `defs`: Contains static definitions.
- `dist`: This is where the runnable code in `src` compiles to. (The directory structure mirrors `src`.)
- `data`: Holds all the dynamic/private data used by the bot. This folder is not meant to hold definitions.
- `docs`: Information for developers who want to contribute.
2020-10-15 09:23:24 +00:00
2021-04-07 06:00:57 +00:00
# Version Numbers
When a new version is ready to be declared...
- ...update the [changelog](../CHANGELOG.md).
- ...update the version numbers in [package.json](../package.json) and [package-lock.json](../package-lock.json).
## Naming Versions
Because versions are assigned to batches of changes rather than single changes (or even single commits), versioning is used a bit differently in order to avoid wasting version numbers.
`<prototype>.<major>.<minor>-<patch>`
- `<prototype>` is a defined as the overarching version group of TravBot. TravBot-v2 went by `2.x.x` and all versions of TravBot-v3 will go by `3.x.x`.
- `<major>` includes any big overhauls or revisions of the entire codebase.
- `<minor>` includes any feature additions in a specific area of the codebase.
- `<patch>` will be pretty much for any very small changes like a quick bug fix or typos. *Note: Normally, these would probably get grouped up, but if there hasn't been a proper version in a while, this will get pushed as a patch.*
*Note: This system doesn't retroactively apply to TravBot-v2, which is why this version naming system won't make sense for v2's changelog.*
2021-04-07 09:58:13 +00:00
# Message Subcommand Type
- `https://discord.com/channels/<id>/<id>/<id>` comes from the `Copy Message Link` button.
- `<id>-<id>` comes from holding `Shift` on the desktop application and clicking on the `Copy ID` button.
2021-04-07 10:58:09 +00:00
# Command Menu
2021-04-07 09:58:13 +00:00
2021-04-07 10:58:09 +00:00
- `args`: A list of arguments in the command. It's relative to the subcommand, so if you do `$test this 5`, `5` becomes `$.args[0]` if `this` is a subcommand. Args are already converted, so a `number` subcommand would return a number rather than a string.
- `client`: `message.client`
- `message`: `message`
- `channel`: `message.channel`
- `guild`: `message.guild`
- `author`: `message.author`
- `member`: `message.member`
2021-04-07 09:58:13 +00:00
2021-04-07 10:58:09 +00:00
# Command Metadata
2020-08-30 21:26:18 +00:00
2021-04-07 10:58:09 +00:00
- `description`: The command description that'll appear in the help menu.
- `endpoint`: Whether or not any arguments are allowed after the command.
- `usage`: Defines a custom usage when showing the command in the help menu.
- `permission`: *(Inherits)* -1 (default) indicates to inherit, 0 is the lowest rank, 1 is second lowest rank, and so on.
- `nsfw`: *(Inherits)* Whether or not the command is restricted to NSFW channels and DM channels.
- `channelType`: *(Inherits)* Whether the command is restricted to guild channels, DM channels, or has no restriction. Uses the `CHANNEL_TYPE` enum provided by the command handler.
2020-10-15 09:23:24 +00:00
2021-04-07 10:58:09 +00:00
# Command Var String
2020-10-15 09:23:24 +00:00
2021-04-07 10:58:09 +00:00
- `%author%` - A user mention of the person who called the command.
- `%prefix%` - The prefix of the current guild.
- `%command%` - The command's execution path up to the current subcommand.
2020-10-15 09:23:24 +00:00
2021-04-07 10:58:09 +00:00
# Utility Functions
## [src/core (libd)](../src/core/libd.ts) - Utility functions specific for working with Discord
`paginate()`
```ts
const pages = ["one", "two", "three"];
2020-08-30 21:26:18 +00:00
paginate(send, author.id, pages.length, page => {
2021-04-11 08:02:56 +00:00
return {content: pages[page]};
});
```
`poll()`
```ts
const results = await poll(await send("Do you agree with this decision?"), ["✅", "❌"]);
results["✅"]; // number
results["❌"]; // number
2020-08-30 21:26:18 +00:00
```
2020-10-15 09:23:24 +00:00
2021-04-11 08:02:56 +00:00
`confirm()`
2021-04-07 10:58:09 +00:00
```ts
2021-04-11 08:02:56 +00:00
const result = await confirm(await send("Are you sure you want to delete this?"), author.id); // boolean | null
```
2020-08-30 21:26:18 +00:00
2021-04-11 08:02:56 +00:00
`askMultipleChoice()`
```ts
const result = await askMultipleChoice(await send("Which of the following numbers is your favorite?"), author.id, 4, 10000); // number (0 to 3) | null
2020-08-30 21:26:18 +00:00
```
2020-10-15 09:23:24 +00:00
2021-04-11 08:02:56 +00:00
`askForReply()`
2021-04-07 10:58:09 +00:00
```ts
2021-04-11 08:02:56 +00:00
const reply = await askForReply(await send("What is your favorite thing to do?"), author.id, 10000); // Message | null
2020-08-30 21:26:18 +00:00
```
2021-04-07 10:58:09 +00:00
## [src/lib](../src/lib.ts) - General utility functions
2020-08-30 21:26:18 +00:00
- `parseArgs()`: Turns `call test "args with spaces" "even more spaces"` into `["call", "test", "args with spaces", "even more spaces"]`, inspired by the command line.
- `parseVars()`: Replaces all `%` args in a string with stuff you specify. For example, you can replace all `nop` with `asm`, and `register %nop%` will turn into `register asm`. Useful for storing strings with variables in one place them accessing them in another place.
- `isType()`: Used for type-checking. Useful for testing `any` types.
- `select()`: Checks if a variable matches a certain type and uses the fallback value if not. (Warning: Type checking is based on the fallback's type. Be sure that the "type" parameter is accurate to this!)
- `Random`: An object of functions containing stuff related to randomness. `Random.num` is a random decimal, `Random.int` is a random integer, `Random.chance` takes a number ranging from `0` to `1` as a percentage. `Random.sign` takes a number and has a 50-50 chance to be negative or positive. `Random.deviation` takes a number and a magnitude and produces a random number within those confines. `(5, 2)` would produce any number between `3` and `7`.
2021-04-07 10:58:09 +00:00
- `pluralise()`: A substitute for not having to do `amount === 1 ? "singular" : "plural"`. For example, `pluralise(x, "credit", "s")` will return `"1 credit"` and/or `"5 credits"` respectively.
- `pluraliseSigned()`: This builds on `pluralise()` and adds a sign at the beginning for marking changes/differences. `pluraliseSigned(0, "credit", "s")` will return `"+0 credits"`.
- `replaceAll()`: A non-regex alternative to replacing everything in a string. `replaceAll("test", "t", "z")` = `"zesz"`.
- `toTitleCase()`: Capitalizes the first letter of each word. `toTitleCase("this is some text")` = `"This Is Some Text"`.
- `random()`: Returns a random element from an array. `random([1,2,3])` could be any one of those elements.
- `split()`: Splits an array into different arrays by a specified length. `split([1,2,3,4,5,6,7,8,9,10], 3)` = `[[1,2,3],[4,5,6],[7,8,9],[10]]`.