Skip to content

Commit

Permalink
Add auto mod blocked reason message
Browse files Browse the repository at this point in the history
  • Loading branch information
encode42 committed May 11, 2024
1 parent 8e30e41 commit df8461f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ MESSAGE_COOLDOWN=3
SUPPORTER_ROLE=
DONATOR_ROLE=
BOOSTER_ROLE=

AUTOMOD_RULE_ID=
2 changes: 1 addition & 1 deletion src/discord/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { string } from "../util/env";
log.info("Creating Discord client...");

export const client = new Client({
"intents": [GatewayIntentBits.MessageContent, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers]
"intents": [GatewayIntentBits.MessageContent, GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers, GatewayIntentBits.AutoModerationExecution]
});

const success = !!(await client.login(string("DISCORD_TOKEN")));
Expand Down
60 changes: 60 additions & 0 deletions src/discord/events/automodMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import type { Channel, User } from "discord.js";
import { Events } from "discord.js";
import { sleep } from "bun";
import { client } from "../client";
import { string } from "../../util/env";
import { log } from "../../log";

const ruleId = string("AUTOMOD_RULE_ID");
const blockedReason = "To prevent spam, links are blocked until you're verified! You'll be verified in a few messages :)";

function getReason(user?: User) {
let reason = blockedReason;

if (user) {
reason = `<@${user.id}>, ${reason}`;
}

return reason;
}

async function sendMessage(channel: Channel | null, reason: string) {
if (!channel || !("send" in channel)) {
return;
}

log.debug("Sending blocked reason channel message...");
const message = await channel.send(reason);

await sleep(7000);
message.delete();
}

client.on(Events.AutoModerationActionExecution, async (action) => {
if (action.ruleId !== ruleId || action.alertSystemMessageId) {
return;
}

log.debug(`Detected auto moderation action for user ${action.userId}...`);

if (!action.user) {
log.debug("User is not defined.");

await sendMessage(action.channel, blockedReason);
return;
}

let success: boolean;
try {
await action.user.send(blockedReason);
success = true;

log.debug("Sent blocked reason in DM!");
} catch {
success = false;
}

if (!success) {
await sendMessage(action.channel, getReason(action.user));
}
});

0 comments on commit df8461f

Please sign in to comment.