Skip to content

Commit

Permalink
feat: some message command
Browse files Browse the repository at this point in the history
  • Loading branch information
JirayuSrisawat-Github committed Oct 3, 2024
1 parent f0ca6de commit 6ded404
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 11 deletions.
40 changes: 36 additions & 4 deletions src/commands/general/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { RandomLoadingMessage } from "../../lib/constants";
import { pickRandom } from "../../lib/utils";
import { Node } from "sakulink";
import { stripIndent } from "common-tags";
import { Message } from "discord.js";
import { Message, OmitPartialGroupDMChannel } from "discord.js";
import ms from "ms";

/**
Expand All @@ -15,6 +15,7 @@ import ms from "ms";
@ApplyOptions<Command.Options>({
// The description of the command.
description: "Display nodes information",
aliases: ["nodes"],
})
export class UserCommand extends Command {
/**
Expand All @@ -37,8 +38,38 @@ export class UserCommand extends Command {
* @returns The response to send to the user.
* @since 1.0.0
*/
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction): Promise<Message<boolean>> {
await interaction.reply({ content: pickRandom(RandomLoadingMessage) });
public override async chatInputRun(
interaction: Command.ChatInputCommandInteraction,
): Promise<Message<boolean> | OmitPartialGroupDMChannel<Message<boolean>> | undefined> {
return await this.run(interaction);
}

/**
* Runs the command when it is invoked.
*
* @param message The message that invoked the command.
* @returns The response to send to the user.
* @since 1.0.0
*/
public override async messageRun(message: Message) {
return await this.run(message);
}

/**
* Runs the command when it is invoked.
*
* @param interactionOrMessage The message that invoked the command.
* @returns The response to send to the user.
* @since 1.0.0
*/
private async run(
interactionOrMessage: Command.ChatInputCommandInteraction | Message<boolean>,
): Promise<Message<boolean> | OmitPartialGroupDMChannel<Message<boolean>> | undefined> {
const isMessage = interactionOrMessage instanceof Message;
let message: Message<boolean> | null = null;

if (isMessage) message = await (<Message>interactionOrMessage).reply({ content: pickRandom(RandomLoadingMessage) });
else await interactionOrMessage.reply({ content: pickRandom(RandomLoadingMessage) });

// Get the list of nodes from the manager
const nodes = this.container.client.manager.nodes.map((node: Node) => ({
Expand Down Expand Up @@ -72,6 +103,7 @@ export class UserCommand extends Command {
};

// Edit the previous message with the embed
return await interaction.editReply({ embeds: [embed], content: null });
if (isMessage) return await message?.edit({ embeds: [embed], content: null });
return await interactionOrMessage.editReply({ embeds: [embed], content: null });
}
}
21 changes: 18 additions & 3 deletions src/commands/general/ping.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApplyOptions } from "@sapphire/decorators";
import { Command } from "@sapphire/framework";
import { InteractionResponse } from "discord.js";
import { InteractionResponse, Message } from "discord.js";

/**
* A command that pings the bot and returns the latency.
Expand Down Expand Up @@ -31,9 +31,24 @@ export class UserCommand extends Command {
* @returns The response to send to the user.
* @since 1.0.0
*/
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction): Promise<InteractionResponse<boolean>> {
public override async chatInputRun(interaction: Command.ChatInputCommandInteraction): Promise<InteractionResponse<boolean> | Message<boolean>> {
return await this.run(interaction);
}

/**
* Runs the command when it is invoked.
*
* @param message The message that invoked the command.
* @returns The response to send to the user.
* @since 1.0.0
*/
public override async messageRun(message: Message) {
return await this.run(message);
}

private async run(interactionOrMessage: Command.ChatInputCommandInteraction | Message<boolean>) {
const content = `Pong! ${Math.round(this.container.client.ws.ping)}ms`; // The content of the response.

return interaction.reply({ content });
return await interactionOrMessage.reply({ content });
}
}
1 change: 1 addition & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NodeOptions, SearchPlatform } from "sakulink";

export const token: string = "";
export const prefix: string = "*";
export const defaultSearchPlatform: SearchPlatform = "youtube music";
export const defaultVolume: number = 75;
export const nodes: NodeOptions[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Listener, UserError } from "@sapphire/framework";
import { InteractionResponse, Message } from "discord.js";

/**
* {@link ChatInputCommandDenied} event
* The "chatInputCommandDenied" event listener class.
*
* @since 1.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ClientEvents } from "discord.js";
import { Logger } from "@sapphire/plugin-logger";

/**
* {@link ChatInputCommandSuccess} event
* The "chatInputCommandSuccess" event listener class.
*
* @since 1.0.0
*/
Expand Down
25 changes: 25 additions & 0 deletions src/listeners/commands/messageCommands/messageCommandDenied.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Events, MessageCommandDeniedPayload } from "@sapphire/framework";
import { Listener, UserError } from "@sapphire/framework";

/**
* The "messageCommandDenied" event listener class.
*
* @since 1.0.0
*/
export class UserEvent extends Listener<typeof Events.MessageCommandDenied> {
/**
* Runs once the event is fired
*
* @param context The context that contains the user error
* @param message The message that contains the payload
* @since 1.0.0
*/
public override async run({ context, message: content }: UserError, { message }: MessageCommandDeniedPayload) {
// If the error was marked as silent, do not reply
if (Reflect.get(Object(context), "silent")) return;

// Reply to the message with the user error
return message.reply({ content, allowedMentions: { users: [message.author.id], roles: [] } });
}
}

30 changes: 30 additions & 0 deletions src/listeners/commands/messageCommands/messageCommandSuccess.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { MessageCommandSuccessPayload } from "@sapphire/framework";
import { Listener, LogLevel } from "@sapphire/framework";
import { Logger } from "@sapphire/plugin-logger";
import { logSuccessCommand } from "../../../lib/utils";

/**
* The "messageCommandSuccess" event listener class.
*
* @since 1.0.0
*/
export class UserEvent extends Listener {
/**
* Runs once the event is fired.
*
* @param payload - The payload of the event.
* @since 1.0.0
*/
public override run(payload: MessageCommandSuccessPayload): void {
logSuccessCommand(payload);
}

/**
* Runs when the listener is being loaded, it enables the listener only if the logger is set to debug level
* @since 1.0.0
*/
public override onLoad(): unknown {
this.enabled = (<Logger>this.container.logger).level <= LogLevel.Debug;
return super.onLoad();
}
}
20 changes: 18 additions & 2 deletions src/structures/Client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LogLevel, SapphireClient } from "@sapphire/framework";
import { token } from "../config";
import { prefix, token } from "../config";
import { Lavalink } from "./Lavalink";

/**
Expand Down Expand Up @@ -28,8 +28,24 @@ export class Client extends SapphireClient {
* @remarks
* We use the `GUILDS` and `GUILD_VOICE_STATES` intents because we need to listen to guild events
* and to access the voice states of users.
* We use the `GUILD_MESSAGES` and `MESSAGE_CONTENT` intents because we need to listen to message events.
*/
intents: 129, // GUILDS, GUILD_VOICE_STATES
intents: 33409, // GUILDS, GUILD_VOICE_STATES, GUILD_MESSAGES, MESSAGE_CONTENT
/**
* The default prefix for the client.
*/
defaultPrefix: prefix,
/**
* Whether or not to load message command listeners when the client starts.
* This is set to `true` by default, so message command listeners will be loaded when the client starts.
*/
loadMessageCommandListeners: true,

/**
* Whether or not to make command matching case-insensitive.
* This is set to `true` by default, so command matching will be case-insensitive.
*/
caseInsensitiveCommands: true,
});
}

Expand Down

0 comments on commit 6ded404

Please sign in to comment.