Skip to content

Commit

Permalink
Slash commands - added registerApplicationCommands (#221)
Browse files Browse the repository at this point in the history
* Added ping slash command

* Fixed linting

* addressed comments

* Added slash + regular command

* Added abstraction for slash commands

* Added error checking, fixed linting

* addressed comments

* Slight changes, added coin flip slash command

* Added registerApplicationCommands method

* Addressed comments, fixed linting

* fixed linting
  • Loading branch information
mcpenguin authored Jul 7, 2022
1 parent 1e94234 commit 25a40ba
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^3.4.0",
"nodemon": "^2.0.7",
"prettier": "^2.2.1",
"prettier": "^2.7.1",
"tsc-watch": "^4.4.0"
}
}
7 changes: 6 additions & 1 deletion src/codeyCommand.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Command as SapphireCommand, container, SapphireClient } from '@sapphire/framework';
import { ChatInputCommand, Command as SapphireCommand, container, SapphireClient } from '@sapphire/framework';
import { Message, MessagePayload, WebhookEditMessageOptions } from 'discord.js';
import { APIMessage } from 'discord-api-types/v9';
import { isMessageInstance } from '@sapphire/discord.js-utilities';
Expand Down Expand Up @@ -31,6 +31,11 @@ export class CodeyCommand extends SapphireCommand {

commandOptions!: CodeyCommandOptions;

// Register application commands
public override registerApplicationCommands(registry: ChatInputCommand.Registry): void {
registry.registerChatInputCommand((builder) => builder.setName(this.name).setDescription(this.description));
}

// Regular command
public async messageRun(message: Message): Promise<Message<boolean>> {
const { client } = container;
Expand Down
33 changes: 24 additions & 9 deletions src/commands/fun/flipCoin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ApplyOptions } from '@sapphire/decorators';
import { Command, CommandOptions, container } from '@sapphire/framework';
import { Message } from 'discord.js';
import { Command, container } from '@sapphire/framework';

@ApplyOptions<CommandOptions>({
import { CodeyCommand, SapphireMessageExecuteType, SapphireMessageResponse } from '../../codeyCommand';

const commandOptions: Command.Options = {
aliases: ['fc', 'flip', 'flip-coin', 'coin-flip', 'coinflip'],
description: 'Flip a coin! In making decisions, if it is not great, at least it is fair!',
detailedDescription: `**Examples:**
Expand All @@ -12,10 +12,25 @@ import { Message } from 'discord.js';
\`${container.botPrefix}coin-flip\`
\`${container.botPrefix}coinflip\`
\`${container.botPrefix}flipcoin\``
})
export class FunFlipCoinCommand extends Command {
async messageRun(message: Message): Promise<Message> {
const onHeads = Math.random() < 0.5;
return message.reply(`The coin landed on **${onHeads ? 'heads' : 'tails'}**!`);
};

const executeCommand: SapphireMessageExecuteType = (
_client,
_messageFromUser,
_initialMessageFromBot
): SapphireMessageResponse => {
const onHeads = Math.random() < 0.5;
return `The coin landed on **${onHeads ? 'heads' : 'tails'}**!`;
};

export class FunFlipCoinCommand extends CodeyCommand {
messageWhenExecutingCommand = 'Flipping a coin...';
executeCommand: SapphireMessageExecuteType = executeCommand;

public constructor(context: Command.Context, options: Command.Options) {
super(context, {
...options,
...commandOptions
});
}
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2184,10 +2184,10 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

prettier@^2.2.1:
version "2.2.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.2.1.tgz#795a1a78dd52f073da0cd42b21f9c91381923ff5"
integrity sha512-PqyhM2yCjg/oKkFPtTGUojv7gnZAoG80ttl45O6x2Ug/rMJw4wcc9k6aaf2hibP7BGVCCM33gZoGjyvt9mm16Q==
prettier@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==

process-nextick-args@~2.0.0:
version "2.0.1"
Expand Down

0 comments on commit 25a40ba

Please sign in to comment.