Skip to content

Commit

Permalink
fix: some typing stuff and canvas cache clear (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Sattler authored Dec 10, 2023
1 parent 951f4a3 commit db0d4b9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
42 changes: 29 additions & 13 deletions src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
CacheType,
ButtonInteraction,
Interaction,
InteractionDeferUpdateOptions,
} from "discord.js";
import { Event } from "../models";
import { UserModel } from "../util/Models/userModel";
Expand All @@ -10,7 +9,7 @@ import { captureException } from "@sentry/node";

const event: Event = {
event: "interactionCreate",
execute: async (client: WouldYou, interaction: Interaction<CacheType>) => {
execute: async (client: WouldYou, interaction: Interaction) => {
const user = await UserModel.findOne({ userID: interaction.user.id });

if (!user) {
Expand Down Expand Up @@ -45,7 +44,7 @@ const event: Event = {
truth: "truth.used.command",
dare: "dare.used.command",
random: "random.used.command",
} as any;
} as Record<string, string>;
// Get the field path based on the command name
const fieldPath = statsMap[interaction.commandName];
if (fieldPath) {
Expand All @@ -65,7 +64,8 @@ const event: Event = {
});
return;
});
} else if (interaction.isButton()) {
}
else if (interaction.isButton()) {
const guildDb = await client.database.getGuild(
interaction.guild?.id as string,
true,
Expand Down Expand Up @@ -100,10 +100,10 @@ const event: Event = {
truth: "truth.used.replay",
dare: "dare.used.replay",
random: "random.used.replay",
} as any;
} as Record<string, string>;

// Get the field path based on the command name
const fieldPath = replyMap[(interaction as any).customId];
const fieldPath = replyMap[interaction.customId];

if (fieldPath) {
// Increment the specified field using $inc
Expand Down Expand Up @@ -173,7 +173,7 @@ const event: Event = {
interaction.customId.startsWith("higher_") ||
interaction.customId.startsWith("lower_")
) {
return button.execute(interaction, client, guildDb);
return button.execute(interaction as ButtonInteraction, client, guildDb);
}

if (
Expand Down Expand Up @@ -288,7 +288,7 @@ const event: Event = {
}
}

button.execute(interaction, client, guildDb);
return button.execute(interaction as ButtonInteraction, client, guildDb);
} catch (err) {
if (err) console.error(err);
interaction.reply({
Expand All @@ -297,16 +297,32 @@ const event: Event = {
});
return;
}
} else {
}
else if (
interaction.isStringSelectMenu()
|| interaction.isUserSelectMenu()
|| interaction.isRoleSelectMenu()
|| interaction.isMentionableSelectMenu()
|| interaction.isChannelSelectMenu()
){
const guildDb = await client.database.getGuild(
interaction.guild?.id as string,
true,
);
if (!guildDb) return;

const button = client.buttons.get((interaction as any).customId);
if (button)
return await button.execute(interaction as any, client, guildDb);
const selectMenu = client.buttons!.get(interaction.customId);

if(!selectMenu) {
interaction.reply({
content: "An error occurred while trying to execute that command.",
ephemeral: true,
});

return;
}

return selectMenu.execute(interaction as any, client, guildDb);
}
},
};
Expand Down
5 changes: 4 additions & 1 deletion src/util/Classes/generateHOR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ export default class HOR {
ctx.fillText(`Score: ${score}`, 712, 50);
ctx.closePath();

return canvasObject.encode("png");
const img = await canvasObject.encode("png");
Canvas.clearAllCache();

return img;
}
}
5 changes: 4 additions & 1 deletion src/util/Classes/generateLOSE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ export default class LOSE {
ctx.fillText(`Score: ${score}`, 712, 280);
ctx.closePath();

return canvasObject.encode("png");
const img = await canvasObject.encode("png");
Canvas.clearAllCache();

return img;
}
}
4 changes: 2 additions & 2 deletions src/util/dailyMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export default class DailyMessage {
return;
} // Always directly return before do to many actions

var General = await getWouldYouRather(db.language);
var WhatYouDo = await getWwyd(db.language);
const General = await getWouldYouRather(db.language);
const WhatYouDo = await getWwyd(db.language);

let randomDaily: any;
let dailyId;
Expand Down
16 changes: 8 additions & 8 deletions src/util/translationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export default class TranslationHandler {
* @return {void}
* @private
*/
initLanguage(key: any, language: any) {
initLanguage(key: string, language: object): void {
this.translations[key] = language;
}

Expand All @@ -63,7 +63,7 @@ export default class TranslationHandler {
* @return {boolean} if the value is a valid translation key
* @private
*/
checkRegex(value: any) {
checkRegex(value: string): boolean {
return /^[a-z]{2}_[A-Z]{2}(?:_rather|_wwyd|_nhie)?$/.test(value);
}

Expand All @@ -73,7 +73,7 @@ export default class TranslationHandler {
* @return {object}
* @private
*/
getLanguage(language: any) {
getLanguage(language: string): object {
if (!this.checkRegex(language)) return this.translations["en_EN"];
return this.translations[language];
}
Expand All @@ -94,7 +94,7 @@ export default class TranslationHandler {
* Reload the translation handler
* @return {void}
*/
reload() {
reload(): void {
this.translations = {};
for (const l of this.availableLanguages) {
try {
Expand All @@ -114,14 +114,14 @@ export default class TranslationHandler {
* @param {string} language the language key
* @param {string} path the path to the translation
* @param {object} data the data to replace in the translation
* @return {string|null} the translation
* @return {string} the translation
* @example
* const translation = getTranslation('en_EN', 'commands.ping.pong', {ping: 100});
*/
get(language: string, path: string, data: any = {}) {
get(language: string, path: string, data: Record<string, any> = {}): string {
if (!language) language = "en_EN";

const l = this.getLanguage(language);
const l: Record<string, any> = this.getLanguage(language);
const p = path.split(".");
let c = null;

Expand Down Expand Up @@ -149,7 +149,7 @@ export default class TranslationHandler {
if (data) {
return c.replace(
/{(\w+)}/g,
(match: any, key: any) => data[key] ?? match,
(match: string, key: string) => data[key] ?? match,
);
}

Expand Down

0 comments on commit db0d4b9

Please sign in to comment.