Skip to content

Commit

Permalink
fix: check emoji/sticker upload errors (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
killbasa authored Jun 19, 2023
1 parent 7a9b146 commit 77ccae4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
22 changes: 20 additions & 2 deletions apps/bot/src/interaction-handlers/modals/addEmote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EmbedColors } from '#utils/constants';
import { buildCustomId, calculateEmoteSlots, parseCustomId } from '#utils/discord';
import { ApplyOptions } from '@sapphire/decorators';
import { InteractionHandler, InteractionHandlerTypes } from '@sapphire/framework';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, ModalSubmitInteraction } from 'discord.js';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, DiscordAPIError, EmbedBuilder, ModalSubmitInteraction } from 'discord.js';
import type { AddResourceModal, Credit, EmojiData } from '#types/CustomIds';

@ApplyOptions<InteractionHandler.Options>({
Expand All @@ -24,7 +24,25 @@ export class ModalHandler extends InteractionHandler {
const embed = new EmbedBuilder();
const { url, animated } = emoteData;

const newEmoji = await interaction.guild.emojis.create({ attachment: url, name: emoteName });
const newEmoji = await interaction.guild.emojis
.create({
attachment: url,
name: emoteName
})
.catch((e) => (e instanceof Error ? e : null));
if (!newEmoji || newEmoji instanceof Error) {
if (newEmoji instanceof DiscordAPIError && newEmoji.code === 50045) {
await interaction.errorReply('The file size of that emoji is too big to upload.', {
tryEphemeral: true
});
} else {
await interaction.errorReply('Something went wrong when uploading the emoji.', {
tryEphemeral: true
});
}

return;
}

if (url.startsWith('https')) {
embed.setThumbnail(url);
Expand Down
27 changes: 21 additions & 6 deletions apps/bot/src/interaction-handlers/modals/addSticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EmbedColors } from '#utils/constants';
import { buildCustomId, calculateStickerSlots, parseCustomId } from '#utils/discord';
import { ApplyOptions } from '@sapphire/decorators';
import { InteractionHandler, InteractionHandlerTypes } from '@sapphire/framework';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, EmbedBuilder, ModalSubmitInteraction } from 'discord.js';
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, DiscordAPIError, EmbedBuilder, ModalSubmitInteraction } from 'discord.js';
import type { AddResourceModal, Credit, StickerData } from '#types/CustomIds';

@ApplyOptions<InteractionHandler.Options>({
Expand All @@ -17,11 +17,26 @@ export class ModalHandler extends InteractionHandler {
const { url } = stickerData;

const stickerName = interaction.fields.getTextInputValue(ResourceFields.Name);
const newSticker = await interaction.guild.stickers.create({
file: url,
name: stickerName,
tags: stickerName
});
const newSticker = await interaction.guild.stickers
.create({
file: url,
name: stickerName,
tags: stickerName
})
.catch((e) => (e instanceof Error ? e : null));
if (!newSticker || newSticker instanceof Error) {
if (newSticker instanceof DiscordAPIError && newSticker.code === 50045) {
await interaction.errorReply('The file size of that sticker is too big to upload.', {
tryEphemeral: true
});
} else {
await interaction.errorReply('Something went wrong when uploading the sticker.', {
tryEphemeral: true
});
}

return;
}

if (url.startsWith('https')) {
embed.setThumbnail(url);
Expand Down

0 comments on commit 77ccae4

Please sign in to comment.