bot shouldnt crash randomly anymore

or at least less
i hope
please
This commit is contained in:
janderedev 2022-01-05 22:15:58 +01:00
parent fc4aa66c8f
commit 426d5f6259
No known key found for this signature in database
GPG key ID: 5D5E18ACB990F57A
7 changed files with 55 additions and 33 deletions

View file

@ -11,10 +11,7 @@ export default {
removeEmptyArgs: false, removeEmptyArgs: false,
category: 'owner', category: 'owner',
run: async (message: Message, args: string[]) => { run: async (message: Message, args: string[]) => {
let cmd = `let { client } = require("../..");` let cmd = args.join(' ');
+ `let axios = require("axios").default;`
+ `let crypto = require("crypto");`
+ args.join(' ');
let m = await message.channel?.sendMessage(`Executing...`); let m = await message.channel?.sendMessage(`Executing...`);

View file

@ -1,6 +1,6 @@
import Log75, { LogLevel } from 'log75'; import Log75, { LogLevel } from 'log75';
// Thanks to being forced to switch to ESM this broke somehow? // Thanks to being forced to switch to ESM this broke somehow?
let logger = new (Log75 as any).default(process.env.NODE_ENV == 'production' ? LogLevel.Standard : LogLevel.Debug); let logger: Log75 = new (Log75 as any).default(process.env.NODE_ENV == 'production' ? LogLevel.Standard : LogLevel.Debug);
export default logger; export default logger;

View file

@ -55,7 +55,8 @@ async function antispam(message: Message): Promise<boolean> {
if (!userStore.warnTriggered) { if (!userStore.warnTriggered) {
userStore.warnTriggered = true; userStore.warnTriggered = true;
setTimeout(() => userStore.warnTriggered = false, 5000); setTimeout(() => userStore.warnTriggered = false, 5000);
message.channel?.sendMessage(getWarnMsg(rule, message)); message.channel?.sendMessage(getWarnMsg(rule, message))
.catch(() => logger.warn('Antispam: Failed to send message'));
} }
break; break;
case ModerationAction.Warn: case ModerationAction.Warn:
@ -73,7 +74,8 @@ async function antispam(message: Message): Promise<boolean> {
user: message.author_id, user: message.author_id,
} as Infraction; } as Infraction;
let m = message.channel?.sendMessage('## User has been warned.\n\u200b\n' + getWarnMsg(rule, message)); message.channel?.sendMessage('## User has been warned.\n\u200b\n' + getWarnMsg(rule, message))
.catch(() => logger.warn('Antispam: Failed to send warn message'));
await storeInfraction(inf); await storeInfraction(inf);
} }

View file

@ -8,6 +8,7 @@ import { antispam } from "./antispam";
import checkCustomRules from "./custom_rules/custom_rules"; 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";
// thanks a lot esm // thanks a lot esm
const filename = fileURLToPath(import.meta.url); const filename = fileURLToPath(import.meta.url);
@ -35,6 +36,14 @@ let commands: Command[];
msg.author_id == client.user?._id || msg.author_id == client.user?._id ||
!msg.channel?.server) return; !msg.channel?.server) return;
if (!msg.member) await msg.channel.server.fetchMember(msg.author_id);
// If we can't reply to the message, return
if (!hasPermForChannel(await getOwnMemberInServer(msg.channel.server), msg.channel, 'SendMessage')) {
logger.debug('Cannot reply to message; returning');
return;
}
// Send message through anti spam check and custom rules // Send message through anti spam check and custom rules
if (!await antispam(msg)) return; if (!await antispam(msg)) return;
checkCustomRules(msg); checkCustomRules(msg);

View file

@ -7,6 +7,7 @@ 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";
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 }) ?? {};
@ -24,13 +25,22 @@ async function checkCustomRules(message: Message, isEdit: boolean = false) {
for (const action of rule.action) { for (const action of rule.action) {
switch(action.action) { switch(action.action) {
case 'sendMessage': case 'sendMessage':
if (hasPermForChannel(await getOwnMemberInServer(message.channel!.server!), message.channel!, 'SendMessage'))
await custom_sendMessage(message, action); await custom_sendMessage(message, action);
else
logger.warn(`Custom rule ${rule._id}: 'sendMessage' action lacks permission`);
break; break;
case 'delete': case 'delete':
if (hasPermForChannel(await getOwnMemberInServer(message.channel!.server!), message.channel!, 'ManageMessages'))
await custom_delete(message, action); await custom_delete(message, action);
else
logger.warn(`Custom rule ${rule._id}: 'delete' action lacks permission`);
break; break;
case 'warn': case 'warn':
if (hasPermForChannel(await getOwnMemberInServer(message.channel!.server!), message.channel!, 'SendMessage'))
await custom_warn(message, action); await custom_warn(message, action);
else
logger.warn(`Custom rule ${rule._id}: 'warn' action lacks permission`);
break; break;
default: default:
logger.warn(`Unknown action ${action.action} in custom rule ${rule._id} in server ${message.channel?.server_id}`); logger.warn(`Unknown action ${action.action} in custom rule ${rule._id} in server ${message.channel?.server_id}`);

View file

@ -8,22 +8,12 @@ import axios from 'axios';
import { Server } from "revolt.js/dist/maps/Servers"; import { Server } from "revolt.js/dist/maps/Servers";
import LogConfig from "../struct/LogConfig"; import LogConfig from "../struct/LogConfig";
import LogMessage from "../struct/LogMessage"; import LogMessage from "../struct/LogMessage";
import { ColorResolvable, MessageAttachment, MessageEmbed, WebhookClient } from "discord.js"; import { ColorResolvable, MessageEmbed } from "discord.js";
import logger from "./logger"; import logger from "./logger";
import { ulid } from "ulid"; import { ulid } from "ulid";
import { Channel } from "revolt.js/dist/maps/Channels";
import { ChannelPermission, ServerPermission } from "revolt.js";
let ServerPermissions = {
['View' as string]: 1 << 0,
['ManageRoles' as string]: 1 << 1,
['ManageChannels' as string]: 1 << 2,
['ManageServer' as string]: 1 << 3,
['KickMembers' as string]: 1 << 4,
['BanMembers' as string]: 1 << 5,
['ChangeNickname' as string]: 1 << 12,
['ManageNicknames' as string]: 1 << 13,
['ChangeAvatar' as string]: 1 << 14,
['RemoveAvatars' as string]: 1 << 15,
}
const NO_MANAGER_MSG = '🔒 Missing permission'; const NO_MANAGER_MSG = '🔒 Missing permission';
const ULID_REGEX = /^[0-9A-HJ-KM-NP-TV-Z]{26}$/i; const ULID_REGEX = /^[0-9A-HJ-KM-NP-TV-Z]{26}$/i;
@ -95,10 +85,8 @@ async function isBotManager(member: Member, server: Server) {
.botManagers?.indexOf(member.user?._id!) ?? -1) > -1; .botManagers?.indexOf(member.user?._id!) ?? -1) > -1;
} }
function hasPerm(member: Member, perm: 'View'|'ManageRoles'|'ManageChannels'|'ManageServer'| // its late and im tired function hasPerm(member: Member, perm: keyof typeof ServerPermission): boolean {
'KickMembers'|'BanMembers'|'ChangeNickname'| // dont judge my code let p = ServerPermission[perm];
'ManageNicknames'|'ChangeAvatar'|'RemoveAvatars'): boolean {
let p = ServerPermissions[perm];
if (member.server?.owner == member.user?._id) return true; if (member.server?.owner == member.user?._id) return true;
// this should work but im not 100% certain // this should work but im not 100% certain
@ -108,6 +96,16 @@ function hasPerm(member: Member, perm: 'View'|'ManageRoles'|'ManageChannels'|'M
return !!(userPerm & p); return !!(userPerm & p);
} }
function hasPermForChannel(member: Member, channel: Channel, perm: keyof typeof ChannelPermission): boolean {
if (!member.server) throw 'hasPermForChannel(): Server is undefined';
return !!(channel.permission & ChannelPermission[perm]);
}
async function getOwnMemberInServer(server: Server): Promise<Member> {
return client.members.getKey({ server: server._id, user: client.user!._id })
|| await server.fetchMember(client.user!._id);
}
async function storeInfraction(infraction: Infraction): Promise<{ userWarnCount: number }> { async function storeInfraction(infraction: Infraction): Promise<{ userWarnCount: number }> {
let collection = client.db.get('infractions'); let collection = client.db.get('infractions');
let p = [ let p = [
@ -162,9 +160,7 @@ async function sendLogMessage(config: LogConfig, content: LogMessage) {
data.append("payload_json", JSON.stringify({ embeds: [ embed.toJSON() ] }), { contentType: 'application/json' }); data.append("payload_json", JSON.stringify({ embeds: [ embed.toJSON() ] }), { contentType: 'application/json' });
axios.post(config.discord.webhookUrl, data, {headers: data.getHeaders() }) axios.post(config.discord.webhookUrl, data, {headers: data.getHeaders() })
.catch(e => { .catch(e => logger.error(`Failed to send log message (discord): ${e}`));
logger.error('Failed to fire Discord webhook: ' + e);
});
} }
if (config.revolt?.channel) { if (config.revolt?.channel) {
@ -224,12 +220,12 @@ async function sendLogMessage(config: LogConfig, content: LogMessage) {
break; break;
} }
await channel.sendMessage({ channel.sendMessage({
content: message, content: message,
attachments: content.attachments ? attachments: content.attachments ?
await Promise.all(content.attachments?.map(a => uploadFile(a.content, a.name))) : await Promise.all(content.attachments?.map(a => uploadFile(a.content, a.name))) :
undefined undefined
}); }).catch(e => logger.error(`Failed to send log message (revolt): ${e}`));
} catch(e) { } catch(e) {
logger.error(`Failed to send log message in ${config.revolt.channel}: ${e}`); logger.error(`Failed to send log message in ${config.revolt.channel}: ${e}`);
} }
@ -277,6 +273,8 @@ function sanitizeMessageContent(msg: string): string {
export { export {
getAutumnURL, getAutumnURL,
hasPerm, hasPerm,
hasPermForChannel,
getOwnMemberInServer,
isModerator, isModerator,
isBotManager, isBotManager,
parseUser, parseUser,

View file

@ -1,3 +1,5 @@
import { ChannelPermission, ServerPermission } from "revolt.js";
class Command { class Command {
name: string; name: string;
aliases: string[] | null; aliases: string[] | null;
@ -7,6 +9,10 @@ class Command {
removeEmptyArgs?: boolean | null; removeEmptyArgs?: boolean | null;
run: Function; run: Function;
category?: string; category?: string;
requiredPermissions?: {
server?: keyof typeof ServerPermission,
channel?: keyof typeof ChannelPermission,
}
} }
export default Command; export default Command;