disable sudo alert on antispam rules, add trycatch
This commit is contained in:
parent
3b64fbb587
commit
b1612824dd
4 changed files with 114 additions and 106 deletions
|
@ -1,11 +1,10 @@
|
||||||
import { Message } from "@janderedev/revolt.js/dist/maps/Messages";
|
import { Message } from "@janderedev/revolt.js/dist/maps/Messages";
|
||||||
import { ulid } from "ulid";
|
import { ulid } from "ulid";
|
||||||
import { client, dbs } from "../..";
|
import { dbs } from "../..";
|
||||||
import AntispamRule from "../../struct/antispam/AntispamRule";
|
import AntispamRule from "../../struct/antispam/AntispamRule";
|
||||||
import Infraction from "../../struct/antispam/Infraction";
|
import Infraction from "../../struct/antispam/Infraction";
|
||||||
import InfractionType from "../../struct/antispam/InfractionType";
|
import InfractionType from "../../struct/antispam/InfractionType";
|
||||||
import ModerationAction from "../../struct/antispam/ModerationAction";
|
import ModerationAction from "../../struct/antispam/ModerationAction";
|
||||||
import ServerConfig from "../../struct/ServerConfig";
|
|
||||||
import logger from "../logger";
|
import logger from "../logger";
|
||||||
import { isModerator, storeInfraction } from "../util";
|
import { isModerator, storeInfraction } from "../util";
|
||||||
|
|
||||||
|
@ -17,6 +16,7 @@ let msgCountStore: Map<string, { users: any }> = new Map();
|
||||||
* @returns true if ok, false if spam rule triggered
|
* @returns true if ok, false if spam rule triggered
|
||||||
*/
|
*/
|
||||||
async function antispam(message: Message): Promise<boolean> {
|
async function antispam(message: Message): Promise<boolean> {
|
||||||
|
try {
|
||||||
let serverRules = await dbs.SERVERS.findOne({ id: message.channel!.server_id! });
|
let serverRules = await dbs.SERVERS.findOne({ id: message.channel!.server_id! });
|
||||||
if (!serverRules?.automodSettings) return true;
|
if (!serverRules?.automodSettings) return true;
|
||||||
|
|
||||||
|
@ -30,7 +30,7 @@ async function antispam(message: Message): Promise<boolean> {
|
||||||
if (message.author?.bot != null) break;
|
if (message.author?.bot != null) break;
|
||||||
if (serverRules.whitelist?.users?.includes(message.author_id)) break;
|
if (serverRules.whitelist?.users?.includes(message.author_id)) break;
|
||||||
if (message.member?.roles?.filter(r => serverRules!.whitelist?.roles?.includes(r)).length) break;
|
if (message.member?.roles?.filter(r => serverRules!.whitelist?.roles?.includes(r)).length) break;
|
||||||
if (serverRules.whitelist?.managers !== false && await isModerator(message)) break;
|
if (serverRules.whitelist?.managers !== false && await isModerator(message, false)) break;
|
||||||
if (rule.channels?.length && rule.channels.indexOf(message.channel_id) == -1) break;
|
if (rule.channels?.length && rule.channels.indexOf(message.channel_id) == -1) break;
|
||||||
|
|
||||||
let store = msgCountStore.get(rule.id)!;
|
let store = msgCountStore.get(rule.id)!;
|
||||||
|
@ -92,6 +92,10 @@ async function antispam(message: Message): Promise<boolean> {
|
||||||
}
|
}
|
||||||
|
|
||||||
return !ruleTriggered;
|
return !ruleTriggered;
|
||||||
|
} catch(e) {
|
||||||
|
console.error(''+e);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getWarnMsg(rule: AntispamRule, message: Message) {
|
function getWarnMsg(rule: AntispamRule, message: Message) {
|
||||||
|
|
|
@ -109,7 +109,7 @@ let commands: SimpleCommand[];
|
||||||
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}`);
|
||||||
|
|
||||||
// Create document for server in DB, if not already present
|
// Create document for server in DB, if not already present
|
||||||
if (JSON.stringify(config) == '{}') await dbs.SERVERS.insert({ id: message.channel!.server_id! });
|
if (JSON.stringify(config) == '{}' || !config) await dbs.SERVERS.insert({ id: message.channel!.server_id! });
|
||||||
|
|
||||||
if (cmd.removeEmptyArgs !== false) {
|
if (cmd.removeEmptyArgs !== false) {
|
||||||
args = args.filter(a => a.length > 0);
|
args = args.filter(a => a.length > 0);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { Message } from "@janderedev/revolt.js/dist/maps/Messages";
|
import { Message } from "@janderedev/revolt.js/dist/maps/Messages";
|
||||||
import { client } from "../../..";
|
import { dbs } from "../../..";
|
||||||
import ServerConfig from "../../../struct/ServerConfig";
|
|
||||||
import logger from "../../logger";
|
import logger from "../../logger";
|
||||||
import messageContentTrigger from "./message_content_trigger";
|
import messageContentTrigger from "./message_content_trigger";
|
||||||
|
|
||||||
|
@ -8,10 +7,10 @@ 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, 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 }) ?? {};
|
try {
|
||||||
|
let serverConfig = await dbs.SERVERS.findOne({ id: message.channel!.server_id! });
|
||||||
let rules = serverConfig?.automodSettings?.custom;
|
let rules = serverConfig?.automodSettings?.custom;
|
||||||
if (!rules) return;
|
if (!rules) return;
|
||||||
|
|
||||||
|
@ -50,6 +49,9 @@ async function checkCustomRules(message: Message, isEdit: boolean = false) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch(e) {
|
||||||
|
console.error(''+e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default checkCustomRules;
|
export default checkCustomRules;
|
||||||
|
|
|
@ -75,7 +75,7 @@ async function parseUserOrId(text: string): Promise<User|{_id: string}|null> {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function isModerator(message: Message) {
|
async function isModerator(message: Message, announceSudo?: boolean) {
|
||||||
let member = message.member!, server = message.channel!.server!;
|
let member = message.member!, server = message.channel!.server!;
|
||||||
|
|
||||||
if (hasPerm(member, 'KickMembers')) return true;
|
if (hasPerm(member, 'KickMembers')) return true;
|
||||||
|
@ -83,32 +83,34 @@ async function isModerator(message: Message) {
|
||||||
const [ isManager, mods, isSudo ] = await Promise.all([
|
const [ isManager, mods, isSudo ] = await Promise.all([
|
||||||
isBotManager(message),
|
isBotManager(message),
|
||||||
dbs.SERVERS.findOne({ id: server._id }),
|
dbs.SERVERS.findOne({ id: server._id }),
|
||||||
checkSudoPermission(message),
|
checkSudoPermission(message, announceSudo),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return isManager
|
return isManager
|
||||||
|| (mods?.moderators?.indexOf(member.user?._id!) ?? -1) > -1
|
|| (mods?.moderators?.indexOf(member.user?._id!) ?? -1) > -1
|
||||||
|| isSudo;
|
|| isSudo;
|
||||||
}
|
}
|
||||||
async function isBotManager(message: Message) {
|
async function isBotManager(message: Message, announceSudo?: boolean) {
|
||||||
let member = message.member!, server = message.channel!.server!;
|
let member = message.member!, server = message.channel!.server!;
|
||||||
|
|
||||||
if (hasPerm(member, 'ManageServer')) return true;
|
if (hasPerm(member, 'ManageServer')) return true;
|
||||||
|
|
||||||
const [ managers, isSudo ] = await Promise.all([
|
const [ managers, isSudo ] = await Promise.all([
|
||||||
dbs.SERVERS.findOne({ id: server._id }),
|
dbs.SERVERS.findOne({ id: server._id }),
|
||||||
checkSudoPermission(message),
|
checkSudoPermission(message, announceSudo),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
return (managers?.botManagers?.indexOf(member.user?._id!) ?? -1) > -1
|
return (managers?.botManagers?.indexOf(member.user?._id!) ?? -1) > -1
|
||||||
|| isSudo;
|
|| isSudo;
|
||||||
}
|
}
|
||||||
async function checkSudoPermission(message: Message): Promise<boolean> {
|
async function checkSudoPermission(message: Message, announce?: boolean): Promise<boolean> {
|
||||||
const hasPerm = isSudo(message.author!);
|
const hasPerm = isSudo(message.author!);
|
||||||
if (!hasPerm) return false;
|
if (!hasPerm) return false;
|
||||||
else {
|
else {
|
||||||
|
if (announce !== false) {
|
||||||
await message.reply(`# :unlock: Bypassed permission check\n`
|
await message.reply(`# :unlock: Bypassed permission check\n`
|
||||||
+ `Sudo mode is enabled for @${message.author!.username}.\n`);
|
+ `Sudo mode is enabled for @${message.author!.username}.\n`);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue