Skip to content

Commit

Permalink
refactor: improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
samarmeena committed Jun 4, 2024
1 parent ff1cc8e commit 5cd7014
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
9 changes: 7 additions & 2 deletions 1-starter/src/commands/simple command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ import {
export class Example {
@SimpleCommand({ aliases: ["hi"] })
async hello(command: SimpleCommandMessage): Promise<void> {
await command.message.reply(`👋 ${command.message.member?.toString()}`);
const member = command.message.member;
if (member) {
await command.message.reply(`👋 ${member.toString()}`);
} else {
await command.message.reply("👋 hello");
}
}

@SimpleCommand({ argSplitter: "+" })
Expand All @@ -28,7 +33,7 @@ export class Example {
return;
}

await command.message.reply(`total = ${num1 + num2}`);
await command.message.reply(`total = ${String(num1 + num2)}`);
}

// make single handler for simple and slash command
Expand Down
14 changes: 11 additions & 3 deletions 1-starter/src/commands/slash + button.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type {
ButtonInteraction,
CommandInteraction,
GuildMember,
MessageActionRowComponentBuilder,
User,
} from "discord.js";
Expand All @@ -10,6 +9,7 @@ import {
ApplicationCommandOptionType,
ButtonBuilder,
ButtonStyle,
GuildMember,
} from "discord.js";
import { ButtonComponent, Discord, Slash, SlashOption } from "discordx";

Expand All @@ -26,6 +26,10 @@ export class Example {
user: User | GuildMember | undefined,
interaction: CommandInteraction,
): Promise<void> {
if (!user) {
return;
}

await interaction.deferReply();

const helloBtn = new ButtonBuilder()
Expand All @@ -41,12 +45,16 @@ export class Example {

await interaction.editReply({
components: [row],
content: `${user?.toString()}, Say hello to bot`,
content: `${user.toString()}, Say hello to bot`,
});
}

@ButtonComponent({ id: "hello-btn" })
async helloBtn(interaction: ButtonInteraction): Promise<void> {
await interaction.reply(`👋 ${interaction.member?.toString()}`);
if (!(interaction.member instanceof GuildMember)) {
return;
}

await interaction.reply(`👋 ${interaction.member.toString()}`);
}
}
10 changes: 4 additions & 6 deletions 1-starter/src/commands/slashes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ export class SlashExample {

const pages = commands.map((cmd, i) => {
const embed = new EmbedBuilder()
.setFooter({ text: `Page ${i + 1} of ${commands.length}` })
.setFooter({
text: `Page ${String(i + 1)} of ${String(commands.length)}`,
})
.setTitle("**Slash command info**")
.addFields({ name: "Name", value: cmd.name })
.addFields({
name: "Description",
value: `${
cmd.description.length > 0
? cmd.description
: "Description unavailable"
}`,
value: cmd.description,
});

return { embeds: [embed] };
Expand Down
2 changes: 1 addition & 1 deletion 3-hot-module-reload/src/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const importPattern = `${dirname(
export async function LoadFiles(src: string): Promise<void> {
const files = await resolve(src);
await Promise.all(
files.map((file) => import(`${file}?version=${Date.now()}`)),
files.map((file) => import(`${file}?version=${Date.now().toString()}`)),
);
}

Expand Down

0 comments on commit 5cd7014

Please sign in to comment.