Skip to content

Commit

Permalink
update discord file limit, fix some stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
TiltedToast committed Mar 7, 2024
1 parent 846e50a commit ecf1b92
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/commands/database.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LibsqlError } from "@libsql/client";
import { ChatInputCommandInteraction, Message, PermissionFlagsBits, codeBlock } from "discord.js";
import { fromZodError } from "zod-validation-error";
import { BOT_OWNERS } from "../config.ts";
import { dbClient, db, updatePrefix as updatePrefixDB } from "../db/index.ts";
import { db, dbClient, updatePrefix as updatePrefixDB } from "../db/index.ts";
import { statuses } from "../db/schema.ts";
import { InsertStatusSchema, type NewStatus } from "../db/types.ts";
import { prefixMap } from "../handlers/prefixes.ts";
Expand All @@ -14,7 +14,6 @@ import {
isDev,
sendOrReply,
} from "../helpers/utils.ts";
import { LibsqlError } from "@libsql/client";

export async function runSQL(message: Message) {
if (!isBotOwner(message.author)) return;
Expand Down Expand Up @@ -127,14 +126,14 @@ export async function updatePrefix(input: Message | ChatInputCommandInteraction)
if (isChatInputCommandInteraction(input)) {
if (
!input.memberPermissions?.has(PermissionFlagsBits.ManageGuild) &&
!BOT_OWNERS.includes(input.user.id)
!isBotOwner(input.user)
) {
return await sendOrReply(input, "Insufficient permissions!", true);
}
} else {
if (
!hasPermission(input.member, PermissionFlagsBits.ManageGuild) &&
!BOT_OWNERS.includes(input.author.id)
!isBotOwner(input.author)
) {
return await input.channel.send("Insufficient permissions!");
}
Expand Down
17 changes: 10 additions & 7 deletions src/commands/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,16 +295,19 @@ export async function addEmoji(message: Message, prefix: string) {
const imgType = getImgType(url);
if (!imgType) return await message.channel.send("Invalid image type!");

const errorMsg = await downloadURL(url, `${temp}/unknown.${imgType}`);
const fileLocation = path.join(temp, `unknown.${imgType}`);
const resizedLocation = path.join(temp, `unknown_resized.${imgType}`);

const errorMsg = await downloadURL(url, fileLocation);
if (errorMsg) return await message.channel.send(errorMsg);

// Resizes image, checks size again and creates emoji
try {
if (!isValidSize(`${temp}/unknown.${imgType}`, FileSizeLimit.DiscordEmoji)) {
if (!isValidSize(fileLocation, FileSizeLimit.DiscordEmoji)) {
const code = await resize({
fileLocation: `${temp}/unknown.${imgType}`,
fileLocation: fileLocation,
width: 128,
saveLocation: `${temp}/unknown_resized.${imgType}`,
saveLocation: resizedLocation,
animated: imgType === "gif",
});

Expand All @@ -314,13 +317,13 @@ export async function addEmoji(message: Message, prefix: string) {
);
}

if (!isValidSize(`${temp}/unknown_resized.${imgType}`, FileSizeLimit.DiscordEmoji)) {
if (!isValidSize(resizedLocation, FileSizeLimit.DiscordEmoji)) {
return message.channel.send("File too large for Discord, even after resizing!");
}
if (message.guild === null) {
return message.channel.send("You have to be in a server to do this!");
}
const base64 = await readFile(`${temp}/unknown_resized.${imgType}`, {
const base64 = await readFile(resizedLocation, {
encoding: "base64",
});
emoji = await message.guild.emojis.create({
Expand All @@ -331,7 +334,7 @@ export async function addEmoji(message: Message, prefix: string) {
if (message.guild === null) {
return message.channel.send("You have to be in a server to do this!");
}
const base64 = await readFile(`${temp}/unknown.${imgType}`, {
const base64 = await readFile(fileLocation, {
encoding: "base64",
});
emoji = await message.guild.emojis.create({
Expand Down
3 changes: 1 addition & 2 deletions src/commands/miscellaneous.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
ButtonBuilder,
ButtonStyle,
ChatInputCommandInteraction,
CommandInteraction,
EmbedBuilder,
Message,
PermissionFlagsBits,
Expand Down Expand Up @@ -242,7 +241,7 @@ export async function leet(input: Message | ChatInputCommandInteraction) {
await sendOrReply(input, leetOutput.substring(0, 2000), false);
}

export async function helpCmd(input: Message | CommandInteraction, prefix?: string) {
export async function helpCmd(input: Message | ChatInputCommandInteraction, prefix?: string) {
const helpMsgArray = await db.select().from(helpMessages).execute();

if (helpMsgArray.length === 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export type ErrorLogOptions = {
};

export const FileSizeLimit = {
DiscordFile: 8388608,
DiscordFile: 26214400,
DiscordEmoji: 262144,
ImgurFile: 10485760,
} as const;
Expand Down
10 changes: 4 additions & 6 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import dedent from "dedent";
import {
ChatInputCommandInteraction,
CommandInteraction,
GuildMember,
Message,
MessageType,
Expand Down Expand Up @@ -67,7 +66,7 @@ export function isChatInputCommandInteraction(
return input instanceof ChatInputCommandInteraction;
}

export function isMessage(input: Message | CommandInteraction): input is Message {
export function isMessage(input: Message | ChatInputCommandInteraction): input is Message {
return input instanceof Message;
}

Expand All @@ -78,7 +77,7 @@ export function isMessage(input: Message | CommandInteraction): input is Message
* @param ephemeral Whether or not the message should be ephemeral (only visible to the user who invoked the command, this is true by default and only for command interactions)
*/
export async function sendOrReply(
input: Message | CommandInteraction,
input: Message | ChatInputCommandInteraction,
message: string | BaseMessageOptions,
ephemeral = true
) {
Expand All @@ -94,7 +93,6 @@ export async function sendOrReply(
if (input.isRepliable()) {
return await input.reply({ ...(message as BaseMessageOptions), ephemeral });
}
return await input.channel?.send(message);
}

export function splitMessage(content: string, maxLength = 2000, delim = " "): string[] {
Expand Down Expand Up @@ -192,7 +190,7 @@ export async function updateEmbed(options: UpdateEmbedOptions) {
const step = { [prevButtonId]: -1, [nextButtonId]: 1 }[interaction.customId];
if (!step) {
await interaction.reply({
content: `Invalid button for some reason. Something must've gone VERY wrong, please let my owner ${OWNER_NAME} know about this if you can`,
content: `Invalid button for some reason. Something must've gone VERY wrong, please let my owner \`${OWNER_NAME}\` know about this if you can`,
ephemeral: true,
});
}
Expand Down Expand Up @@ -305,7 +303,7 @@ export function errorLog({ message, errorObject }: ErrorLogOptions) {

const errorMessageWithoutStack = dedent`
An Error occurred on ${currentTime}
**Server:** ${message.guild?.name ?? "Unknown"} - ${message.guild?.id ?? "Unknown"}
**Server:** ${message.guild?.name ?? "Unknown"} - ${message.guild?.id ?? "DM"}
**Room:** ${(message.channel as TextChannel).name} - ${message.channel.id}
**User:** ${message.author.username} - ${message.author.id}
**Command used:** ${commandUsed}
Expand Down

0 comments on commit ecf1b92

Please sign in to comment.