Skip to content

Commit

Permalink
feat: resolve typing issues with new guards
Browse files Browse the repository at this point in the history
  • Loading branch information
naomi-lgbt committed Sep 18, 2024
1 parent b7d3481 commit 2c6a41c
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 43 deletions.
9 changes: 1 addition & 8 deletions src/contexts/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ export const report: Context = {
});
return;
}
const { guild, options, user } = interaction;
if (!guild) {
await interaction.reply({
content: "You cannot report DM messages!",
ephemeral: true,
});
return;
}
const { options, user } = interaction;
const message = options.getMessage("message", true);

const { reportChannel } = camperChan;
Expand Down
21 changes: 14 additions & 7 deletions src/events/handlers/handleInteractionCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { closePrivateChannel } from "../../modules/closePrivateChannel.js";
import { reactionRoleClick } from "../../modules/reactionRoleClick.js";
import { closeTicket } from "../../modules/tickets/closeTicket.js";
import { openTicket } from "../../modules/tickets/openTicket.js";
import { isGuildCommandInteraction } from "../../utils/typeGuards.js";
import type { ExtendedClient } from "../../interfaces/extendedClient.js";

/**
Expand All @@ -18,6 +17,13 @@ export const handleInteractionCreate = async(
interaction: Interaction,
): Promise<void> => {
if (interaction.isChatInputCommand()) {
if (!interaction.inCachedGuild()) {
await interaction.reply({
content: "This command can only be used in a server, not a DM.",
ephemeral: true,
});
return;
}
const target = camperChan.commands.find((command) => {
return command.data.name === interaction.commandName;
});
Expand All @@ -27,16 +33,17 @@ export const handleInteractionCreate = async(
);
return;
}
if (!isGuildCommandInteraction(interaction)) {
await interaction.reply(
"This command can only be used in a server, not a DM.",
);
return;
}
await target.run(camperChan, interaction);
}

if (interaction.isContextMenuCommand()) {
if (!interaction.inCachedGuild()) {
await interaction.reply({
content: "This command can only be used in a server, not a DM.",
ephemeral: true,
});
return;
}
const target = camperChan.contexts.find((context) => {
return context.data.name === interaction.commandName;
});
Expand Down
4 changes: 2 additions & 2 deletions src/interfaces/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ExtendedClient } from "./extendedClient.js";
import type { GuildCommandInteraction } from "./guildCommandInteraction.js";
import type {
ChatInputCommandInteraction,
SlashCommandOptionsOnlyBuilder,
SlashCommandSubcommandsOnlyBuilder,
} from "discord.js";
Expand All @@ -9,6 +9,6 @@ export interface Command {
data: SlashCommandSubcommandsOnlyBuilder | SlashCommandOptionsOnlyBuilder;
run: (
camperChan: ExtendedClient,
interaction: GuildCommandInteraction
interaction: ChatInputCommandInteraction<"cached">
)=> Promise<void>;
}
2 changes: 1 addition & 1 deletion src/interfaces/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export interface Context {
};
run: (
camperChan: ExtendedClient,
interaction: ContextMenuCommandInteraction
interaction: ContextMenuCommandInteraction<"cached">
)=> Promise<void>;
}
4 changes: 2 additions & 2 deletions src/interfaces/extendedClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import type {
CategoryChannel,
Client,
Guild,
GuildTextBasedChannel,
Snowflake,
TextBasedChannel,
WebhookClient,
} from "discord.js";
import type { App } from "octokit";
Expand Down Expand Up @@ -39,7 +39,7 @@ export interface ExtendedClient extends Client {
privateLogs: Record<string, string>;
db: PrismaClient;
homeGuild: Guild;
reportChannel: TextBasedChannel;
reportChannel: GuildTextBasedChannel;
privateCategory: CategoryChannel;
learnAccounts: Record<string, UserRecord & { cacheTTL: Date }>;
event?: {
Expand Down
7 changes: 6 additions & 1 deletion src/modules/closePrivateChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ export const closePrivateChannel = async(
try {
await interaction.deferReply();
const { channel, member } = interaction;
if (!channel || !member || channel.type === ChannelType.DM) {
if (
!channel
|| !member
|| channel.type === ChannelType.DM
|| channel.type === ChannelType.GroupDM
) {
await interaction.editReply("Cannot find your member object.");
return;
}
Expand Down
22 changes: 0 additions & 22 deletions src/utils/typeGuards.ts

This file was deleted.

0 comments on commit 2c6a41c

Please sign in to comment.