fix crash when message is deleted before replying

This commit is contained in:
janderedev 2022-01-13 20:20:48 +01:00
parent c2bae09528
commit c2f5d889f5
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A
11 changed files with 45 additions and 8 deletions

View file

@ -2,6 +2,7 @@ import Command from "../../struct/Command";
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import { inspect } from 'util'; import { inspect } from 'util';
import { client } from "../.."; import { client } from "../..";
import MessageCommandContext from "../../struct/MessageCommandContext";
export default { export default {
name: 'eval', name: 'eval',
@ -10,7 +11,7 @@ export default {
restrict: 'BOTOWNER', restrict: 'BOTOWNER',
removeEmptyArgs: false, removeEmptyArgs: false,
category: 'owner', category: 'owner',
run: async (message: Message, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
let cmd = args.join(' '); let cmd = args.join(' ');
let m = await message.channel?.sendMessage(`Executing...`); let m = await message.channel?.sendMessage(`Executing...`);

View file

@ -2,6 +2,7 @@ import Command from "../../struct/Command";
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import { commands, DEFAULT_PREFIX, ownerIDs } from "../modules/command_handler"; import { commands, DEFAULT_PREFIX, ownerIDs } from "../modules/command_handler";
import CommandCategory from "../../struct/CommandCategory"; import CommandCategory from "../../struct/CommandCategory";
import MessageCommandContext from "../../struct/MessageCommandContext";
const categories: { [key: string]: CommandCategory } = { const categories: { [key: string]: CommandCategory } = {
'moderation': { 'moderation': {
@ -37,7 +38,7 @@ export default {
description: 'Help command.', description: 'Help command.',
removeEmptyArgs: true, removeEmptyArgs: true,
category: 'misc', category: 'misc',
run: async (message: Message, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
const isBotOwner = ownerIDs.includes(message.author_id); const isBotOwner = ownerIDs.includes(message.author_id);
const prefix = DEFAULT_PREFIX; // TODO: fetch prefix from server config const prefix = DEFAULT_PREFIX; // TODO: fetch prefix from server config

View file

@ -1,13 +1,14 @@
import Command from "../../struct/Command"; import Command from "../../struct/Command";
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import { client } from "../.."; import { client } from "../..";
import MessageCommandContext from "../../struct/MessageCommandContext";
export default { export default {
name: 'ping', name: 'ping',
aliases: null, aliases: null,
description: 'ping pong', description: 'ping pong',
category: 'misc', category: 'misc',
run: async (message: Message, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
let now = Date.now(); let now = Date.now();
message.reply(`Measuring...`) message.reply(`Measuring...`)
?.catch(console.error) ?.catch(console.error)

View file

@ -4,6 +4,7 @@ import { client } from "../..";
import ServerConfig from "../../struct/ServerConfig"; import ServerConfig from "../../struct/ServerConfig";
import { DEFAULT_PREFIX } from "../modules/command_handler"; import { DEFAULT_PREFIX } from "../modules/command_handler";
import { hasPerm, isBotManager, NO_MANAGER_MSG } from "../util"; import { hasPerm, isBotManager, NO_MANAGER_MSG } from "../util";
import MessageCommandContext from "../../struct/MessageCommandContext";
const SYNTAX = '/prefix set [new prefix]; /prefix get; prefix clear'; const SYNTAX = '/prefix set [new prefix]; /prefix get; prefix clear';
const MENTION_TEXT = 'You can also @mention me instead of using the prefix.'; const MENTION_TEXT = 'You can also @mention me instead of using the prefix.';
@ -14,7 +15,7 @@ export default {
description: 'Configure AutoMod\'s prefix', description: 'Configure AutoMod\'s prefix',
syntax: SYNTAX, syntax: SYNTAX,
category: 'configuration', category: 'configuration',
run: async (message: Message, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
let config: ServerConfig = (await client.db.get('servers').findOne({ id: message.channel?.server_id })) ?? {}; let config: ServerConfig = (await client.db.get('servers').findOne({ id: message.channel?.server_id })) ?? {};
switch(args[0]?.toLowerCase()) { switch(args[0]?.toLowerCase()) {

View file

@ -2,6 +2,7 @@ import Command from "../../struct/Command";
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import { decodeTime } from 'ulid'; import { decodeTime } from 'ulid';
import { isModerator, parseUser } from "../util"; import { isModerator, parseUser } from "../util";
import MessageCommandContext from "../../struct/MessageCommandContext";
const SYNTAX = '/purge [SELECTOR] [@user?[m @user?[, ...]]]; where SELECTOR: [number] || [messageID]-[messageID]'; const SYNTAX = '/purge [SELECTOR] [@user?[m @user?[, ...]]]; where SELECTOR: [number] || [messageID]-[messageID]';
const MAX_PURGE_AMOUNT = 100; const MAX_PURGE_AMOUNT = 100;
@ -12,7 +13,7 @@ export default {
description: 'Mass delete messages', description: 'Mass delete messages',
syntax: SYNTAX, syntax: SYNTAX,
category: 'moderation', category: 'moderation',
run: async (message: Message, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
try { try {
if (!message.member || !await isModerator(message.member!, message.channel?.server!)) return message.reply('🔒 Access denied'); if (!message.member || !await isModerator(message.member!, message.channel?.server!)) return message.reply('🔒 Access denied');

View file

@ -1,6 +1,7 @@
import Command from "../../struct/Command"; import Command from "../../struct/Command";
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import { exec } from 'child_process'; import { exec } from 'child_process';
import MessageCommandContext from "../../struct/MessageCommandContext";
export default { export default {
name: 'shell', name: 'shell',
@ -9,7 +10,7 @@ export default {
restrict: 'BOTOWNER', restrict: 'BOTOWNER',
removeEmptyArgs: false, removeEmptyArgs: false,
category: 'owner', category: 'owner',
run: async (message: Message, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
let cmd = args.join(' '); let cmd = args.join(' ');
let m = await message.channel?.sendMessage(`Executing...`); let m = await message.channel?.sendMessage(`Executing...`);

View file

@ -1,12 +1,13 @@
import Command from "../../struct/Command"; import Command from "../../struct/Command";
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import MessageCommandContext from "../../struct/MessageCommandContext";
export default { export default {
name: 'test', name: 'test',
aliases: [ 'testalias' ], aliases: [ 'testalias' ],
description: 'Test command', description: 'Test command',
category: 'misc', category: 'misc',
run: (message: Message, args: string[]) => { run: (message: MessageCommandContext, args: string[]) => {
message.reply('Beep boop.'); message.reply('Beep boop.');
} }
} as Command; } as Command;

View file

@ -9,6 +9,7 @@ import checkCustomRules from "./custom_rules/custom_rules";
import MessageCommandContext from "../../struct/MessageCommandContext"; import MessageCommandContext from "../../struct/MessageCommandContext";
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { getOwnMemberInServer, hasPermForChannel } from "../util"; import { getOwnMemberInServer, hasPermForChannel } from "../util";
import { prepareMessage } from "./prepare_message";
// thanks a lot esm // thanks a lot esm
const filename = fileURLToPath(import.meta.url); const filename = fileURLToPath(import.meta.url);
@ -88,6 +89,7 @@ let commands: Command[];
let message: MessageCommandContext = msg as MessageCommandContext; let message: MessageCommandContext = msg as MessageCommandContext;
message.serverContext = serverCtx; message.serverContext = serverCtx;
prepareMessage(message);
logger.info(`Command: ${message.author?.username} (${message.author?._id}) in ${message.channel?.server?.name} (${message.channel?.server?._id}): ${message.content}`); logger.info(`Command: ${message.author?.username} (${message.author?._id}) in ${message.channel?.server?.name} (${message.channel?.server?._id}): ${message.content}`);

View file

@ -7,7 +7,8 @@ import messageContentTrigger from "./message_content_trigger";
import custom_sendMessage from "./actions/sendMessage"; import custom_sendMessage from "./actions/sendMessage";
import custom_delete from "./actions/delete"; import custom_delete from "./actions/delete";
import custom_warn from "./actions/warn"; import custom_warn from "./actions/warn";
import { getOwnMemberInServer, hasPerm, hasPermForChannel } from "../../util"; import { getOwnMemberInServer, hasPermForChannel } from "../../util";
import MessageCommandContext from "../../../struct/MessageCommandContext";
async function checkCustomRules(message: Message, isEdit: boolean = false) { async function checkCustomRules(message: Message, isEdit: boolean = false) {
let serverConfig: ServerConfig = await client.db.get('servers').findOne({ id: message.channel?.server_id }) ?? {}; let serverConfig: ServerConfig = await client.db.get('servers').findOne({ id: message.channel?.server_id }) ?? {};

View file

@ -0,0 +1,26 @@
import { Message } from "revolt.js/dist/maps/Messages";
import logger from "../logger";
// We modify the way `reply()` works to make sure we
// don't crash if the original message was deleted.
export function prepareMessage(message: Message) {
message.reply = (...args: Parameters<typeof Message.prototype.reply>) => {
return new Promise<Message>((resolve, reject) => {
message.channel?.sendMessage({
content: typeof args[0] == 'string' ? args[0] : args[0].content,
replies: [ { id: message._id, mention: args[1] ?? true } ],
})
?.then(m => resolve(m))
.catch(e => {
if (e?.response?.status == 404) {
logger.warn("Replying to message gave 404, trying again without reply");
if (!message.channel) return reject("Channel does not exist");
message.channel?.sendMessage(typeof args[0] == 'string' ? { content: args[0] } : args[0])
.then(resolve)
.catch(reject);
} else reject(e);
});
});
}
}

View file

@ -1,5 +1,6 @@
import { Message } from "revolt.js/dist/maps/Messages"; import { Message } from "revolt.js/dist/maps/Messages";
import { Server } from "revolt.js/dist/maps/Servers"; import { Server } from "revolt.js/dist/maps/Servers";
import logger from "../bot/logger";
class MessageCommandContext extends Message { class MessageCommandContext extends Message {
// The server to which the command should be applied. // The server to which the command should be applied.