Synchronously save automod message
This commit is contained in:
parent
0fb3306a25
commit
57dc207485
@ -413,43 +413,29 @@ router.post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
//@ts-ignore
|
// @ts-ignore
|
||||||
message.member.roles = message.member.roles.filter((x) => x.id != x.guild_id).map((x) => x.id);
|
message.member.roles = message.member.roles.filter((x) => x.id != x.guild_id).map((x) => x.id);
|
||||||
|
|
||||||
try {
|
if (message.content)
|
||||||
if (message.content)
|
try {
|
||||||
for (const rule of await AutomodRule.find({ where: { guild_id: message.guild_id, enabled: true, event_type: AutomodRuleEventType.MESSAGE_SEND } })) {
|
const matchingRules = await AutomodRule.find({
|
||||||
|
where: { guild_id: message.guild_id, enabled: true, event_type: AutomodRuleEventType.MESSAGE_SEND },
|
||||||
|
order: { position: "ASC" },
|
||||||
|
});
|
||||||
|
for (const rule of matchingRules) {
|
||||||
if (rule.exempt_channels.includes(channel_id)) continue;
|
if (rule.exempt_channels.includes(channel_id)) continue;
|
||||||
if (message.member.roles.some((x) => rule.exempt_roles.includes(x.id))) continue;
|
if (message.member.roles.some((x) => rule.exempt_roles.includes(x.id))) continue;
|
||||||
|
|
||||||
if (rule.trigger_type == AutomodTriggerTypes.CUSTOM_WORDS) {
|
if (rule.trigger_type == AutomodTriggerTypes.CUSTOM_WORDS) {
|
||||||
const triggerMeta = rule.trigger_metadata as AutomodCustomWordsRule;
|
const triggerMeta = rule.trigger_metadata as AutomodCustomWordsRule;
|
||||||
const regexes = triggerMeta.regex_patterns
|
const regexes = triggerMeta.regex_patterns.map((x) => new RegExp(x, "i")).concat(triggerMeta.keyword_filter.map((k) => k.globToRegexp("i")));
|
||||||
.map((x) => new RegExp(x, "i"))
|
const allowedRegexes = triggerMeta.allow_list.map((k) => k.globToRegexp("i"));
|
||||||
.concat(
|
|
||||||
triggerMeta.keyword_filter
|
|
||||||
.map((k) =>
|
|
||||||
k
|
|
||||||
// Convert simple wildcard patterns to regex
|
|
||||||
.replace(".", "\\.")
|
|
||||||
.replace("?", ".")
|
|
||||||
.replace("*", ".*"),
|
|
||||||
)
|
|
||||||
.map((k) => new RegExp(k, "i")),
|
|
||||||
);
|
|
||||||
const allowedRegexes = triggerMeta.allow_list
|
|
||||||
.map((k) =>
|
|
||||||
k
|
|
||||||
// Convert simple wildcard patterns to regex
|
|
||||||
.replace(".", "\\.")
|
|
||||||
.replace("?", ".")
|
|
||||||
.replace("*", ".*"),
|
|
||||||
)
|
|
||||||
.map((k) => new RegExp(k, "i"));
|
|
||||||
|
|
||||||
const matches = regexes
|
const matches = regexes
|
||||||
.map((r) => message.content!.match(r))
|
.map((r) => message.content!.match(r))
|
||||||
.filter((x) => x !== null && x.length > 0)
|
.filter((x) => x !== null && x.length > 0)
|
||||||
.filter((x) => !allowedRegexes.some((ar) => ar.test(x![0])));
|
.filter((x) => !allowedRegexes.some((ar) => ar.test(x![0])));
|
||||||
|
|
||||||
if (matches.length > 0) {
|
if (matches.length > 0) {
|
||||||
console.log("Automod triggered by message:", message.id, "matches:", matches);
|
console.log("Automod triggered by message:", message.id, "matches:", matches);
|
||||||
if (rule.actions.some((x) => x.type == AutomodRuleActionType.SEND_ALERT_MESSAGE && x.metadata.channel_id)) {
|
if (rule.actions.some((x) => x.type == AutomodRuleActionType.SEND_ALERT_MESSAGE && x.metadata.channel_id)) {
|
||||||
@ -458,32 +444,32 @@ router.post(
|
|||||||
const alertChannel = await Channel.findOne({ where: { id: action.metadata.channel_id } });
|
const alertChannel = await Channel.findOne({ where: { id: action.metadata.channel_id } });
|
||||||
if (!alertChannel) continue;
|
if (!alertChannel) continue;
|
||||||
const msg = await Message.createWithDefaults({
|
const msg = await Message.createWithDefaults({
|
||||||
channel_id: alertChannel.id,
|
|
||||||
content: `Automod Alert: Message ${message.id} by <@${message.author_id}> in <#${channel.id}> triggered automod rule "${rule.name}".\nMatched terms: ${matches
|
content: `Automod Alert: Message ${message.id} by <@${message.author_id}> in <#${channel.id}> triggered automod rule "${rule.name}".\nMatched terms: ${matches
|
||||||
.map((x) => `\`${x![0]}\``)
|
.map((x) => `\`${x![0]}\``)
|
||||||
.join(", ")}`,
|
.join(", ")}`,
|
||||||
author: message.author,
|
author: message.author,
|
||||||
|
channel_id: alertChannel.id,
|
||||||
guild_id: message.guild_id,
|
guild_id: message.guild_id,
|
||||||
member_id: message.member_id,
|
member_id: message.member_id,
|
||||||
author_id: message.author_id
|
author_id: message.author_id,
|
||||||
});
|
});
|
||||||
|
|
||||||
await Promise.all([
|
await message.save();
|
||||||
message.save(),
|
// await Promise.all([
|
||||||
emitEvent({
|
await emitEvent({
|
||||||
event: "MESSAGE_CREATE",
|
event: "MESSAGE_CREATE",
|
||||||
channel_id: msg.channel_id,
|
channel_id: msg.channel_id,
|
||||||
data: msg.toJSON(),
|
data: msg.toJSON(),
|
||||||
} as MessageCreateEvent),
|
} as MessageCreateEvent);
|
||||||
]);
|
// ]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("[Automod] failed to process message:", e);
|
console.log("[Automod] failed to process message:", e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let read_state = await ReadState.findOne({
|
let read_state = await ReadState.findOne({
|
||||||
|
|||||||
15
src/util/util/extensions/String.test.ts
Normal file
15
src/util/util/extensions/String.test.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import moduleAlias from "module-alias";
|
||||||
|
moduleAlias();
|
||||||
|
import './String';
|
||||||
|
import { describe, it } from 'node:test';
|
||||||
|
import assert from 'node:assert/strict';
|
||||||
|
|
||||||
|
describe("String extensions", () => {
|
||||||
|
|
||||||
|
it("globToRegexp", () => {
|
||||||
|
const pattern = "file-*.txt";
|
||||||
|
const regex = pattern.globToRegexp();
|
||||||
|
assert.ok(regex.test("file-123.txt"));
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
37
src/util/util/extensions/String.ts
Normal file
37
src/util/util/extensions/String.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/*
|
||||||
|
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
|
||||||
|
Copyright (C) 2025 Spacebar and Spacebar Contributors
|
||||||
|
|
||||||
|
This program is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU Affero General Public License as published
|
||||||
|
by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
This program is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU Affero General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Affero General Public License
|
||||||
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface String {
|
||||||
|
globToRegexp(flags?: string): RegExp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stringGlobToRegexp(str: string, flags?: string): RegExp {
|
||||||
|
// Convert simple wildcard patterns to regex
|
||||||
|
const escaped = str.replace(".", "\\.")
|
||||||
|
.replace("?", ".")
|
||||||
|
.replace("*", ".*")
|
||||||
|
return new RegExp(escaped, flags);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register extensions
|
||||||
|
if (!String.prototype.globToRegexp)
|
||||||
|
String.prototype.globToRegexp = function (str: string, flags?: string) {
|
||||||
|
return stringGlobToRegexp.call(null, str, flags);
|
||||||
|
};
|
||||||
@ -2,3 +2,4 @@ export * from "./Array";
|
|||||||
export * from "./Math";
|
export * from "./Math";
|
||||||
export * from "./Url";
|
export * from "./Url";
|
||||||
export * from "./Object";
|
export * from "./Object";
|
||||||
|
export * from "./String";
|
||||||
Reference in New Issue
Block a user