mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
fix(core): resolve unhandled rejections on 403s and undefined channel guildId
- Safely resolve guildId in CommandHandler replyHandler and prefix formatters - Catch MISSING_PERMISSIONS and COMMUNICATION_DISABLED (403) across MessageHandler send/reply/edit - Prevent join failure in PlayerLifecycleMixin when text channel messages are denied - Suppress sending error-replies into channels where the bot lacks send permissions
This commit is contained in:
1 parent
7bd80e62f7
commit
ec441ea20a
5 files changed
+91
-24
No files matched your search
@@ -70,7 +70,10 @@ export class CommandHandler extends EventEmitter {
|
|||||||
this.helpHandler = new HelpHandler(this);
|
this.helpHandler = new HelpHandler(this);
|
||||||
this.helpCommand = "help";
|
this.helpCommand = "help";
|
||||||
this.replyHandler = (message, msg) => {
|
this.replyHandler = (message, msg) => {
|
||||||
msg.reply(this.format(message, msg.channel.channel.guildId));
|
const guildId = msg?.channel?.guildId ?? msg?.channel?.channel?.guildId ?? msg?.guildId ?? msg?.message?.guildId ?? null;
|
||||||
|
Promise.resolve(msg.reply(this.format(message, guildId))).catch(err => {
|
||||||
|
logger.warn("[CommandHandler] Failed to reply:", err?.message);
|
||||||
|
});
|
||||||
};
|
};
|
||||||
this.messages.onMessage(this.messageHandler.bind(this));
|
this.messages.onMessage(this.messageHandler.bind(this));
|
||||||
}
|
}
|
||||||
@@ -269,7 +272,10 @@ export class CommandHandler extends EventEmitter {
|
|||||||
if (cmd.requirements.length > 0 && !external) {
|
if (cmd.requirements.length > 0 && !external) {
|
||||||
if (!this.assertRequirements(cmd, msg)) return;
|
if (!this.assertRequirements(cmd, msg)) return;
|
||||||
}
|
}
|
||||||
if (previous === false) previous = this.format("$prefix" + cmd.name, msg.channel?.channel?.guildId);
|
if (previous === false) {
|
||||||
|
const prevGuildId = msg.channel?.guildId ?? msg.channel?.channel?.guildId ?? msg.message?.guildId ?? null;
|
||||||
|
previous = this.format("$prefix" + cmd.name, prevGuildId);
|
||||||
|
}
|
||||||
if (!external) this.emit("command", { command: cmd, message: msg });
|
if (!external) this.emit("command", { command: cmd, message: msg });
|
||||||
|
|
||||||
if (cmd.subcommands.length !== 0) {
|
if (cmd.subcommands.length !== 0) {
|
||||||
|
|||||||
@@ -44,23 +44,31 @@ export class CommandLoader {
|
|||||||
this.commands.on("run", (data) => {
|
this.commands.on("run", (data) => {
|
||||||
if (!this.runnables.has(data.command.uid)) return;
|
if (!this.runnables.has(data.command.uid)) return;
|
||||||
const runFc = this.runnables.get(data.command.uid);
|
const runFc = this.runnables.get(data.command.uid);
|
||||||
|
const isPermError = (err) => {
|
||||||
|
return err?.code === "MISSING_PERMISSIONS" ||
|
||||||
|
err?.code === "COMMUNICATION_DISABLED" ||
|
||||||
|
err?.statusCode === 403;
|
||||||
|
};
|
||||||
const sendErrorReply = (id) => {
|
const sendErrorReply = (id) => {
|
||||||
Promise.resolve(data.message.reply("An error occurred. Error id: `#" + id + "`"))
|
Promise.resolve(data.message.reply("An error occurred. Error id: `#" + id + "`"))
|
||||||
.catch(err => logger.warn("[CommandHandler] Failed to send error-reply; error id #" + id + ":", err?.message));
|
.catch(err => logger.warn("[CommandHandler] Failed to send error-reply; error id #" + id + ":", err?.message));
|
||||||
};
|
};
|
||||||
try {
|
const handleCmdError = (e) => {
|
||||||
const result = runFc.call(this.context, data.message, data);
|
if (isPermError(e)) {
|
||||||
if (result && typeof result.catch === "function") {
|
logger.warn(`[CommandHandler] Missing permissions in channel ${data.message?.channelId ?? data.message?.channel?.id}: ${e.code ?? e.message}`);
|
||||||
result.catch(e => {
|
return;
|
||||||
const id = Utils.uid();
|
|
||||||
logger.error("Error running command; error id #" + id, e);
|
|
||||||
sendErrorReply(id);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} catch (e) {
|
|
||||||
const id = Utils.uid();
|
const id = Utils.uid();
|
||||||
logger.error("Error running command; error id #" + id, e);
|
logger.error("Error running command; error id #" + id, e);
|
||||||
sendErrorReply(id);
|
sendErrorReply(id);
|
||||||
|
};
|
||||||
|
try {
|
||||||
|
const result = runFc.call(this.context, data.message, data);
|
||||||
|
if (result && typeof result.catch === "function") {
|
||||||
|
result.catch(handleCmdError);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
handleCmdError(e);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
+3
-3
@@ -233,10 +233,10 @@ class Remix {
|
|||||||
),
|
),
|
||||||
false,
|
false,
|
||||||
{
|
{
|
||||||
icon_url: msg.channel.channel.guild?.icon
|
icon_url: (msg.channel?.channel?.guild?.icon || msg.server?.icon)
|
||||||
? `https://cdn.fluxer.app/icons/${msg.channel.channel.guild.id}/${msg.channel.channel.guild.icon}.webp`
|
? `https://cdn.fluxer.app/icons/${msg.channel?.channel?.guild?.id ?? msg.server?.id}/${msg.channel?.channel?.guild?.icon ?? msg.server?.icon}.webp`
|
||||||
: null,
|
: null,
|
||||||
title: msg.channel.channel.guild?.name ?? null,
|
title: msg.channel?.channel?.guild?.name ?? msg.server?.name ?? null,
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -557,7 +557,7 @@ const PlayerLifecycleMixin = {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
const statusMsg = await message.reply(this._t(message, "responses.join.joining"));
|
const statusMsg = await message.reply(this._t(message, "responses.join.joining")).catch(() => null);
|
||||||
try {
|
try {
|
||||||
await player.join(cid);
|
await player.join(cid);
|
||||||
|
|
||||||
@@ -565,7 +565,7 @@ const PlayerLifecycleMixin = {
|
|||||||
this._indexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
|
this._indexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
|
||||||
this._pendingJoins.delete(cleanChannelId);
|
this._pendingJoins.delete(cleanChannelId);
|
||||||
|
|
||||||
await statusMsg.edit(this._t(message, "responses.join.joined", { channel: cid }));
|
await statusMsg?.edit?.(this._t(message, "responses.join.joined", { channel: cid })).catch(() => {});
|
||||||
|
|
||||||
const guildId = cleanId(channel.guildId ?? getMessageGuildId(message));
|
const guildId = cleanId(channel.guildId ?? getMessageGuildId(message));
|
||||||
this._restorePlayerVolume(player, guildId);
|
this._restorePlayerVolume(player, guildId);
|
||||||
@@ -584,7 +584,7 @@ const PlayerLifecycleMixin = {
|
|||||||
|
|
||||||
this.playerMap.set(cleanChannelId, player);
|
this.playerMap.set(cleanChannelId, player);
|
||||||
this._indexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
|
this._indexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
|
||||||
await statusMsg.edit(this._t(message, "responses.join.joined", { channel: cid }));
|
await statusMsg?.edit?.(this._t(message, "responses.join.joined", { channel: cid })).catch(() => {});
|
||||||
|
|
||||||
const retryGuildId = cleanId(channel.guildId ?? getMessageGuildId(message));
|
const retryGuildId = cleanId(channel.guildId ?? getMessageGuildId(message));
|
||||||
this._restorePlayerVolume(player, retryGuildId);
|
this._restorePlayerVolume(player, retryGuildId);
|
||||||
@@ -602,7 +602,7 @@ const PlayerLifecycleMixin = {
|
|||||||
} else {
|
} else {
|
||||||
errorMsg = this._t(message, "responses.join.joinFailed", { error: err.message });
|
errorMsg = this._t(message, "responses.join.joinFailed", { error: err.message });
|
||||||
}
|
}
|
||||||
await statusMsg.edit(errorMsg).catch(() => {});
|
await statusMsg?.edit?.(errorMsg).catch(() => {});
|
||||||
this.playerMap.delete(cleanChannelId);
|
this.playerMap.delete(cleanChannelId);
|
||||||
this._unindexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
|
this._unindexPlayer(channel.guildId ?? getMessageGuildId(message), cleanChannelId);
|
||||||
player.destroy();
|
player.destroy();
|
||||||
|
|||||||
@@ -395,7 +395,15 @@ export class MessageHandler {
|
|||||||
} else {
|
} else {
|
||||||
opts = { ...message };
|
opts = { ...message };
|
||||||
}
|
}
|
||||||
return new Message(await replyingTo.reply(opts, { ping: false }), this);
|
try {
|
||||||
|
return new Message(await replyingTo.reply(opts, { ping: false }), this);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] Cannot reply in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -409,7 +417,15 @@ export class MessageHandler {
|
|||||||
if (!(await this.assertPermissions(["SendMessages", "EmbedLinks"], replyingTo))) return null;
|
if (!(await this.assertPermissions(["SendMessages", "EmbedLinks"], replyingTo))) return null;
|
||||||
|
|
||||||
if (typeof message === "object" && Array.isArray(message.embeds)) {
|
if (typeof message === "object" && Array.isArray(message.embeds)) {
|
||||||
return new Message(await replyingTo.reply(message, { ping: false }), this);
|
try {
|
||||||
|
return new Message(await replyingTo.reply(message, { ping: false }), this);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
@@ -423,7 +439,15 @@ export class MessageHandler {
|
|||||||
const { embedText, ...rest } = message;
|
const { embedText, ...rest } = message;
|
||||||
payload = { ...payload, ...rest };
|
payload = { ...payload, ...rest };
|
||||||
}
|
}
|
||||||
return new Message(await replyingTo.reply(payload, { ping: false }), this);
|
try {
|
||||||
|
return new Message(await replyingTo.reply(payload, { ping: false }), this);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] Cannot replyEmbed in channel ${replyingTo.channelId ?? replyingTo.channel?.id}: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -443,7 +467,15 @@ export class MessageHandler {
|
|||||||
} else {
|
} else {
|
||||||
opts = message;
|
opts = message;
|
||||||
}
|
}
|
||||||
return new Message(await channel.send(opts), this);
|
try {
|
||||||
|
return new Message(await channel.send(opts), this);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] Cannot sendMessage in channel ${channel.id}: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -458,7 +490,15 @@ export class MessageHandler {
|
|||||||
return this.sendMessage(channel, typeof content === "string" ? content : content?.embedText ?? "");
|
return this.sendMessage(channel, typeof content === "string" ? content : content?.embedText ?? "");
|
||||||
}
|
}
|
||||||
if (typeof content === "object" && Array.isArray(content.embeds)) {
|
if (typeof content === "object" && Array.isArray(content.embeds)) {
|
||||||
return new Message(await channel.send(content), this);
|
try {
|
||||||
|
return new Message(await channel.send(content), this);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] Cannot sendEmbed in channel ${channel.id}: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const text = (typeof content === "object") ? content.embedText : content;
|
const text = (typeof content === "object") ? content.embedText : content;
|
||||||
const payload = this.#createEmbed(text, embedOptions);
|
const payload = this.#createEmbed(text, embedOptions);
|
||||||
@@ -466,7 +506,15 @@ export class MessageHandler {
|
|||||||
const { embedText, ...rest } = content;
|
const { embedText, ...rest } = content;
|
||||||
Object.assign(payload, rest);
|
Object.assign(payload, rest);
|
||||||
}
|
}
|
||||||
return new Message(await channel.send(payload), this);
|
try {
|
||||||
|
return new Message(await channel.send(payload), this);
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] Cannot sendEmbed in channel ${channel.id}: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -503,6 +551,11 @@ export class MessageHandler {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (err.code === "MISSING_PERMISSIONS" || err.code === "COMMUNICATION_DISABLED" || err.statusCode === 403) {
|
||||||
|
logger.warn(`[MessageHandler] editEmbed: missing permissions or communication disabled: ${err.code ?? err.message}`);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
if (RETRYABLE.has(err.statusCode) && attempt < MAX_ATTEMPTS) {
|
if (RETRYABLE.has(err.statusCode) && attempt < MAX_ATTEMPTS) {
|
||||||
logger.warn(`[MessageHandler] editEmbed: ${err.statusCode} on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS}ms…`);
|
logger.warn(`[MessageHandler] editEmbed: ${err.statusCode} on attempt ${attempt}/${MAX_ATTEMPTS}, retrying in ${RETRY_DELAY_MS}ms…`);
|
||||||
await new Promise(r => setTimeout(r, RETRY_DELAY_MS));
|
await new Promise(r => setTimeout(r, RETRY_DELAY_MS));
|
||||||
|
|||||||
Reference in new issue
Block a user