Skip to content

Commit

Permalink
chore: marks
Browse files Browse the repository at this point in the history
  • Loading branch information
favna committed Oct 22, 2022
1 parent 8cbeee9 commit a89a7c9
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,10 +250,10 @@ The `chatInputRun` example above uses [interaction handlers][interaction-handler
[rac]: ../../../Documentation/sapphire-framework/class/Command#registerapplicationcommands
[rcic]: ../../../Documentation/sapphire-framework/class/ApplicationCommandRegistry#registerchatinputcommand
[what-is-it]: ./what-is-it
[idhints]: ../../../Documentation/sapphire-framework/interfaces/ApplicationCommandRegistry.RegisterOptions#idhints
[guildids]: ../../../Documentation/sapphire-framework/interfaces/ApplicationCommandRegistry.RegisterOptions#guildids
[idhints]: ../../../Documentation/sapphire-framework/interface/ApplicationCommandRegistry.RegisterOptions#idhints
[guildids]: ../../../Documentation/sapphire-framework/interface/ApplicationCommandRegistry.RegisterOptions#guildids
[behaviorwhennotidentical]:
../../../Documentation/sapphire-framework/interfaces/ApplicationCommandRegistry.RegisterOptions#behaviorwhennotidentical
../../../Documentation/sapphire-framework/interface/ApplicationCommandRegistry.RegisterOptions#behaviorwhennotidentical
[registercommandifmissing]:
../../../Documentation/sapphire-framework/interfaces/ApplicationCommandRegistry.RegisterOptions#registercommandifmissing
../../../Documentation/sapphire-framework/interface/ApplicationCommandRegistry.RegisterOptions#registercommandifmissing
[inner-ref-options]: #chat-input-command-registry-options
2 changes: 1 addition & 1 deletion docs/Guide/arguments/built-in-arguments.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ To facilitate creating commands and getting your bot running, Sapphire bundles a
[date]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date
[dmchannel]: https://discord.js.org/#/docs/main/v13/class/DMChannel
[guildchannel]: https://discord.js.org/#/docs/main/v13/class/GuildChannel
[emojiobject]: ../../Documentation/sapphire-framework/interfaces/EmojiObject
[emojiobject]: ../../Documentation/sapphire-framework/interface/EmojiObject
[guildmember]: https://discord.js.org/#/docs/main/v13/class/GuildMember
[mdn-date]:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date#several_ways_to_create_a_date_object
Expand Down
4 changes: 2 additions & 2 deletions docs/Guide/getting-started/creating-a-basic-command.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ For handling subcommands, please refer to the [Sapphire Plugin Subcommands][saph
:::

[command]: ../../Documentation/sapphire-framework/class/Command
[commandoptions]: ../../Documentation/sapphire-framework/interfaces/CommandOptions
[commandoptions]: ../../Documentation/sapphire-framework/interface/CommandOptions
[getting-started]: ./getting-started-with-sapphire
[load-message-commands-option]:
../../Documentation/sapphire-framework/interfaces/SapphireClientOptions#loadmessagecommandlisteners
../../Documentation/sapphire-framework/interface/SapphireClientOptions#loadmessagecommandlisteners
[saph-plug-subcom]: ../plugins/Subcommands/getting-started
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ And that's it for your `index.js` file! In the end, your directory should look l

[sapphire]: ../../Documentation/sapphire-framework/class/SapphireClient
[djs-client]: https://discord.js.org/#/docs/main/v13/class/Client
[baseuserdirectory]: ../../Documentation/sapphire-framework/interfaces/SapphireClientOptions#baseuserdirectory
[baseuserdirectory]: ../../Documentation/sapphire-framework/interface/SapphireClientOptions#baseuserdirectory
[securing-your-token]: https://discordjs.guide/preparations/setting-up-a-bot-application.html#your-token
20 changes: 10 additions & 10 deletions docs/Guide/getting-started/updating-from-v2-to-v3.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -340,31 +340,31 @@ uppercase `P` represents the [`Events`][events] object name)
- `[P]`CommandError (`[p]CommandError`) - emitted when the command run method throws an error.
- `[P]`CommandFinish (`[p]CommandFinish`) - emitted when the command run is finished.

[`ipreconditioncontainer`]: ../../Documentation/sapphire-framework/interfaces/IPreconditionContainer
[`ipreconditioncontainer`]: ../../Documentation/sapphire-framework/interface/IPreconditionContainer
[`preconditioncontainerarray`]: ../../Documentation/sapphire-framework/class/PreconditionContainerArray
[`preconditioncontainersingle`]: ../../Documentation/sapphire-framework/class/PreconditionContainerSingle
[acr]: ../application-commands/application-command-registry/what-is-it.mdx
[allflowsprecondition]: ../../Documentation/sapphire-framework/class/AllFlowsPrecondition
[applicationcommandregistry]: ../application-commands/application-command-registry/what-is-it.mdx
[autocomplete]: ../application-commands/interaction-handlers/autocomplete.mdx
[chat-input-parallel]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition#chatinputparallel
[chat-input-sequential]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition#chatinputsequential
[chat-input-parallel]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition#chatinputparallel
[chat-input-sequential]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition#chatinputsequential
[command-acr]: ../../Documentation/sapphire-framework/class/Command#applicationcommandregistry
[command-chat-input-run]: ../../Documentation/sapphire-framework/class/Command#chatinputrun
[command-context-menu-run]: ../../Documentation/sapphire-framework/class/Command#contextmenurun
[command-message-run]: ../../Documentation/sapphire-framework/class/Command#messagerun
[command-rac]: ../../Documentation/sapphire-framework/class/Command#registerapplicationcommands
[commands]: #commands
[context-menu-parallel]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition#contextmenuparallel
[context-menu-sequential]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition#contextmenusequential
[context-menu-parallel]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition#contextmenuparallel
[context-menu-sequential]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition#contextmenusequential
[events]: ../../Documentation/sapphire-framework/#events
[fetchchannelfrominteraction]: ../../Documentation/sapphire-framework/class/Precondition#fetchchannelfrominteraction
[idhints]: ../../Documentation/sapphire-framework/interfaces/ApplicationCommandRegistry.RegisterOptions#idhints
[ipreconditioncondition]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition
[idhints]: ../../Documentation/sapphire-framework/interface/ApplicationCommandRegistry.RegisterOptions#idhints
[ipreconditioncondition]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition
[loadmessagecommandlisteners]:
../../Documentation/asapphirepi-framework/interfaces/SapphireClientOptions#loadmessagecommandlisteners
[message-parallel]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition#messageparallel
[message-sequential]: ../../Documentation/sapphire-framework/interfaces/IPreconditionCondition#messagesequential
../../Documentation/asapphirepi-framework/interface/SapphireClientOptions#loadmessagecommandlisteners
[message-parallel]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition#messageparallel
[message-sequential]: ../../Documentation/sapphire-framework/interface/IPreconditionCondition#messagesequential
[precondition-chat-input-run]: ../../Documentation/sapphire-framework/class/Precondition#chatinputrun
[precondition-context-menu-run]: ../../Documentation/sapphire-framework/class/Precondition#contextmenurun
[precondition-message-run]: ../../Documentation/sapphire-framework/class/Precondition#messagerun
Expand Down
18 changes: 9 additions & 9 deletions docs/Guide/plugins/i18next/getting-started.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -297,28 +297,28 @@ export class PingCommand extends Command {
}
```

[`saphclientoptions`]: ../../../Documentation/api-framework/interfaces/SapphireClientOptions
[`internationalizationcontext`]: ../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationContext
[`saphclientoptions`]: ../../../Documentation/api-framework/interface/SapphireClientOptions
[`internationalizationcontext`]: ../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationContext
[resolve-key]: ../../../Documentation/sapphire-plugin-i18next/function/resolvekey
[fs-backend]: https://github.com/skyra-project/archid-components/tree/main/packages/i18next-backend
[fetchlanguage]: ../../../Documentation/sapphire-plugin-i18next/function/fetchlanguage
[i18n-options]: ../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationOptions
[i18n-options]: ../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationOptions
[sapphireclient]: ../../../Documentation/sapphire-framework/class/SapphireClient.md
[i18next]: https://i18next.com
[plugin]: https://github.com/sapphiredev/plugins/tree/main/packages/i18next
[using-container]: ../../additional-information/using-and-extending-container.mdx
[internationalizationcontext.guild]:
../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationContext#guild
../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationContext#guild
[internationalizationcontext.channel]:
../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationContext#channel
../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationContext#channel
[internationalizationcontext.user]:
../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationContext#user
../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationContext#user
[internationalizationcontext.interactionguildlocale]:
../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationContext#interactionguildlocale
../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationContext#interactionguildlocale
[internationalizationcontext.interactionlocale]:
../../../Documentation/sapphire-plugin-i18next/interfaces/InternationalizationContext#interactionlocale
../../../Documentation/sapphire-plugin-i18next/interface/InternationalizationContext#interactionlocale
[messagerun]: ../../../Documentation/sapphire-framework/class/Command.md#messagerun
[chatinputrun]: ../../../Documentation/sapphire-framework/class/Command.md#chatinputrun
[contextmenurun]: ../../../Documentation/sapphire-framework/class/Command.md#contextmenurun
[fetcht]: ../../../Documentation/sapphire-plugin-i18next/function/fetcht
[tfunction]: ../../../Documentation/sapphire-plugin-i18next/interfaces/TFunction
[tfunction]: ../../../Documentation/sapphire-plugin-i18next/interface/TFunction
2 changes: 1 addition & 1 deletion docs/Guide/preconditions/channel-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ Failure][reporting-precondition-failure].

:::

[runin]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#runin
[runin]: ../../Documentation/sapphire-framework/interface/CommandOptions#runin
[runintypes]: ../../Documentation/sapphire-framework/enum/CommandOptionsRunTypeEnum
[reporting-precondition-failure]: ./reporting-precondition-failure
10 changes: 5 additions & 5 deletions docs/Guide/preconditions/command-cooldown.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ Failure][reporting-precondition-failure].

:::

[cooldowndelay]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#cooldowndelay
[cooldownfilteredusers]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#cooldownfilteredusers
[cooldownlimit]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#cooldownlimit
[cooldownscope]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#cooldownscope
[defaultcooldown]: ../../Documentation/sapphire-framework/interfaces/SapphireClientOptions#defaultcooldown
[cooldowndelay]: ../../Documentation/sapphire-framework/interface/CommandOptions#cooldowndelay
[cooldownfilteredusers]: ../../Documentation/sapphire-framework/interface/CommandOptions#cooldownfilteredusers
[cooldownlimit]: ../../Documentation/sapphire-framework/interface/CommandOptions#cooldownlimit
[cooldownscope]: ../../Documentation/sapphire-framework/interface/CommandOptions#cooldownscope
[defaultcooldown]: ../../Documentation/sapphire-framework/interface/SapphireClientOptions#defaultcooldown
[sapphire]: ../../Documentation/sapphire-framework/class/SapphireClient
[scopes]: ../../Documentation/sapphire-framework/enum/BucketScope
[reporting-precondition-failure]: ./reporting-precondition-failure
Expand Down
96 changes: 69 additions & 27 deletions docs/Guide/preconditions/creating-your-own-preconditions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,84 @@ import { Precondition } from '@sapphire/framework';
export class OwnerOnlyPrecondition extends Precondition {}
```

Next, we can create a [`run`][preconditionrun] function to execute our logic. This function should either return
Next, we can create a [`messageRun`][preconditionmessagerun], [`chatInputRun`][preconditionchatinputrun], and/or
[`contextMenuRun`][preconditioncontextmenurun] function to execute our logic. These functions should either return
[`this.ok()`][preconditionok] to signify the condition has passed, or [`this.error(...)`][preconditionerror] to signify
the command should be denied.

:::danger
:::tip

Clicking on [`run`][preconditionrun] above will take you to a method instead called `messageRun`. This is because this
documentation reflects the latest commit to the `main` branch of `@sapphire/framework` which is has a breaking change
included that changed the name of the method. For the purposes of this guide however, you can use `run` as long as you
are using the latest NPM published version of `@sapphire/framework`.
If your precondition is set to implement all three of [`messageRun`][preconditionmessagerun],
[`chatInputRun`][preconditionchatinputrun], and [`contextMenuRun`][preconditioncontextmenurun] then we recommend
extending [`AllFlowsPrecondition`][all-flows-precondition] which will add TypeScript checks to ensure that you have
implemented all 3 methods.

:::

```typescript ts2esm2cjs|{4-8}|{4-8}
```typescript ts2esm2cjs|{4-17}|{5-17}
import { Precondition } from '@sapphire/framework';
import type { Message } from 'discord.js';
import type { CommandInteraction, ContextMenuInteraction, Message } from 'discord.js';

export class OwnerOnlyPrecondition extends Precondition {
public run(message: Message) {
return message.author.id === 'YOUR_ID'
public override async messageRun(message: Message) {
// for message command
return this.checkOwner(message.author.id);
}

public override async chatInputRun(interaction: CommandInteraction) {
// for slash command
return this.checkOwner(interaction.user.id);
}

public override async contextMenuRun(interaction: ContextMenuInteraction) {
// for Context Menu Command
return this.checkOwner(interaction.user.id);
}

private async checkOwner(userId: string) {
return Config.bot.owners!.includes(userId)
? this.ok()
: this.error({ message: 'Only the bot owner can use this command!' });
}
}
```

:::info

Adding a check method i.e. `messageRun` or `chatInputRun` or `contextMenuRun` is optional, you can add add any of them
that's needed for your bot. For example, if your bot has no `Slash` or `Context menu` command then `chatInputRun` and
`contextMenuRun` can be omitted from the example above.

:::

:::caution

<!--
TODO: Once we have a dedicated TypeScript page this should link to that instead of a code example.
Including why augmenting with `never` works and the difference between that and augmenting with an object.
-->

Typescript users must augment Sapphire's [`Preconditions`][preconditions-interface] interface, which is needed to
increase the security of Sapphire's types. Otherwise, you will run into type errors in the next section.

```typescript {7-11}
import { Precondition } from '@sapphire/framework';

export class OwnerOnlyPrecondition extends Precondition {
// ...
}

declare module '@sapphire/framework' {
interface Preconditions {
OwnerOnly: never;
}
}
```

Please see an official example [here][preconditions-augment].

:::

## Using Preconditions in Commands

To attach a precondition to a command, you simply have to input its name in an array in the command's
Expand All @@ -81,19 +133,6 @@ export class PingCommand extends Command {

Now, if someone who is not the bot owner executes the `ping` command, nothing will happen!

:::caution

For TypeScript users, there's an extra step to make this work. To increase the security of Sapphire's types, you'll need
to augment Sapphire's [`Preconditions`][preconditions-interface] interface. Please see an official example
[here][preconditions-augment].

<!--
TODO: Once we have a dedicated TypeScript page this should link to that instead of a code example.
Including why augmenting with `never` works and the difference between that and augmenting with an object.
-->

:::

By default, no error message will be sent or logged when a command is denied because of a precondition. To learn how to
configure this, please read [Reporting Precondition Failures][reporting-precondition-failure].

Expand Down Expand Up @@ -121,13 +160,16 @@ None of the following preconditions are bundled with Sapphire; as such you'd hav
For a command with these preconditions to pass the denial checks, the `InVoiceChannel` precondition must pass, as well
as `AdminOnly` _or_ both `OwnerOnly` and `ModOnly`.

[all-flows-precondition]: ../../Documentation/sapphire-framework/class/AllFlowsPrecondition
[creating-commands]: ../getting-started/creating-a-basic-command
[precondition]: ../../Documentation/sapphire-framework/class/Precondition
[preconditionrun]: ../../Documentation/sapphire-framework/class/Precondition#messagerun
[preconditionmessagerun]: ../../Documentation/sapphire-framework/class/Precondition#messagerun
[preconditionchatinputrun]: ../../Documentation/sapphire-framework/class/Precondition#chatinputrun
[preconditioncontextmenurun]: ../../Documentation/sapphire-framework/class/Precondition#contextmenurun
[preconditionok]: ../../Documentation/sapphire-framework/class/Precondition#ok
[preconditionerror]: ../../Documentation/sapphire-framework/class/Precondition#error
[preconditions-option]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#preconditions
[preconditions-interface]: ../../Documentation/sapphire-framework/interfaces/Preconditions
[preconditions-option]: ../../Documentation/sapphire-framework/interface/CommandOptions#preconditions
[preconditions-interface]: ../../Documentation/sapphire-framework/interface/Preconditions
[preconditions-augment]:
https://github.com/sapphiredev/examples/blob/main/examples/with-typescript-recommended/src/preconditions/OwnerOnly.ts#L13-L17
https://github.com/sapphiredev/examples/blob/main/examples/with-typescript-recommended/src/preconditions/OwnerOnly.ts#L27-L31
[reporting-precondition-failure]: ./reporting-precondition-failure
4 changes: 2 additions & 2 deletions docs/Guide/preconditions/handling-permissions.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ Failure][reporting-precondition-failure].

:::

[requireduserpermissions]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#requireduserpermissions
[requiredclientpermissions]: ../../Documentation/sapphire-framework/interfaces/CommandOptions#requiredclientpermissions
[requireduserpermissions]: ../../Documentation/sapphire-framework/interface/CommandOptions#requireduserpermissions
[requiredclientpermissions]: ../../Documentation/sapphire-framework/interface/CommandOptions#requiredclientpermissions
[reporting-precondition-failure]: ./reporting-precondition-failure
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ throw a TypeScript error for trying to read property `silent` of type `unknown`.

[listeners]: ../listeners/creating-your-own-listeners
[error]: ../../Documentation/sapphire-framework/class/UserError
[payload]: ../../Documentation/sapphire-framework/interfaces/MessageCommandDeniedPayload
[payload]: ../../Documentation/sapphire-framework/interface/MessageCommandDeniedPayload
[context]: ../../Documentation/sapphire-framework/class/UserError#context
[preconditionerror]: ../../Documentation/sapphire-framework/class/Precondition#error
[creating-preconditions]: ./creating-your-own-preconditions
2 changes: 1 addition & 1 deletion docs/Guide/utilities/time-utilities.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,4 @@ console.log(Timestamp.utc('2022-04-25'));

<!-- TODO: Check proper URL -->

[time-enum]: ../../Documentation/sapphire-utilities/enum/time_utilities_src.Time
[time-enum]: ../../Documentation/sapphire-time-utilities/enum/Time

0 comments on commit a89a7c9

Please sign in to comment.