Skip to content

Commit

Permalink
fixing several issues
Browse files Browse the repository at this point in the history
 issues concerning DraftbotInteraction's error management #2590 (maybe #2661 ?)
  • Loading branch information
romain22222 committed Dec 9, 2024
1 parent 73cdd72 commit 5747537
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions Discord/src/messages/DraftbotInteraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ type ReplyFunctionLike<OptionValue> = (options: OptionValue) => Promise<Message>
export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands {
public userLanguage: Language = LANGUAGE.DEFAULT_LANGUAGE;

// @ts-expect-error - Property 'options' is initialized in the caster, which is the only normal way to create a DraftbotInteraction
public options: CommandInteractionOptionResolver;
public options!: CommandInteractionOptionResolver;

// @ts-expect-error - Property '_channel' is initialized in the caster, which is the only normal way to create a DraftbotInteraction
private _channel: DraftbotChannel;
private _channel!: DraftbotChannel;

/**
* Get the channel of the interaction
Expand Down Expand Up @@ -179,8 +177,8 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
* @param fallback function to execute if the bot can't send the message
*/
public async reply(options: OptionLike, fallback?: () => void | Promise<void>): Promise<Message> {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
return await DraftbotInteraction.prototype.commonSendCommand(CommandInteraction.prototype.reply.bind(this), options as ReplyOptionsSpecial, fallback ?? (() => {
// @ts-expect-error - We consider that the following function passed as argument has the correct typing
return await DraftbotInteraction.prototype.commonSendCommand.call(this, CommandInteraction.prototype.reply.bind(this), options, fallback ?? (() => {

Check failure on line 181 in Discord/src/messages/DraftbotInteraction.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Missing return type on function

Check failure on line 181 in Discord/src/messages/DraftbotInteraction.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Discord/src/messages/DraftbotInteraction.ts#L181

Missing return type on function.
// Do nothing by default if no fallback is provided
})) as Message;
}
Expand All @@ -191,8 +189,7 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
* @param fallback function to execute if the bot can't send the message
*/
public async followUp(options: OptionLike, fallback?: () => void | Promise<void>): Promise<Message> {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
return await DraftbotInteraction.prototype.commonSendCommand(CommandInteraction.prototype.followUp.bind(this), options, fallback ?? (() => {
return await DraftbotInteraction.prototype.commonSendCommand.call(this, CommandInteraction.prototype.followUp.bind(this), options, fallback ?? (() => {

Check failure on line 192 in Discord/src/messages/DraftbotInteraction.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Missing return type on function

Check failure on line 192 in Discord/src/messages/DraftbotInteraction.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Discord/src/messages/DraftbotInteraction.ts#L192

Missing return type on function.
// Do nothing by default if no fallback is provided
})) as Message;
}
Expand All @@ -215,7 +212,7 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
return await functionPrototype(options);
}
catch (e) {
console.error(`Weird Permission Error ${(e as Error).stack}`);
console.error(`An error occured during a send, either a permission issue or a send/reply/followUp/editReply conflict : ${(e as Error).stack}`);
await DraftbotInteraction.prototype.manageFallback.bind(this)(functionPrototype);
await fallback();
return null;
Expand All @@ -227,7 +224,7 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
* @private
*/
private async manageFallback<OptionType extends OptionLike>(functionPrototype: ReplyFunctionLike<OptionType>): Promise<void> {
const errorText = i18n.t("bot:noSpeakPermission", {lng: this.channel.language});
const errorText = i18n.t("bot:noSpeakPermission", {lng: this.userLanguage});
try {
// @ts-expect-error - We consider that the functionPrototype is a function that can be called with these parameters (i.e, accepts a InteractionReplyOptions)
await functionPrototype.call(this, {
Expand All @@ -236,12 +233,18 @@ export class DraftbotInteraction extends DraftbotInteractionWithoutSendCommands
});
}
catch (e) {
if (functionPrototype !== DraftbotChannel.prototype.send) {
// try again to manage fallback with the send function

Check failure on line 237 in Discord/src/messages/DraftbotInteraction.ts

View workflow job for this annotation

GitHub Actions / eslint-discord-module

Comments should not begin with a lowercase character

Check warning on line 237 in Discord/src/messages/DraftbotInteraction.ts

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

Discord/src/messages/DraftbotInteraction.ts#L237

Comments should not begin with a lowercase character.
// @ts-expect-error - We consider that the functionPrototype is a function that can be called with these parameters (i.e, accepts a InteractionReplyOptions)
await DraftbotInteraction.prototype.manageFallback.bind(this)(BaseGuildTextChannel.prototype.send.bind(this.channel));
return;
}
// We can't send ephemeral message, so we send the message in DM
try {
await CommandInteraction.prototype.user.send.bind(this.user)({content: errorText});
}
catch (e) {
console.log(`Unable to alert user of no speak permission : c:${this.channel.id} / u:${this.user.id}`);
console.log(`Unable to alert user of no speak permission : c:${this.channel?.id} / u:${this.user?.id}`);
}
}
}
Expand Down

0 comments on commit 5747537

Please sign in to comment.