adapt to new system message type

This commit is contained in:
Lea 2023-05-01 13:52:10 +02:00
parent 4776e54bde
commit 5bf7852f22
3 changed files with 25 additions and 20 deletions

View file

@ -8,27 +8,29 @@ import logger from "../logger";
import { storeInfraction } from "../util"; import { storeInfraction } from "../util";
import { DEFAULT_PREFIX } from "./command_handler"; import { DEFAULT_PREFIX } from "./command_handler";
import { SendableEmbed } from "revolt-api"; import { SendableEmbed } from "revolt-api";
import { UserSystemMessage } from "revolt.js";
const DM_SESSION_LIFETIME = 1000 * 60 * 60 * 24 * 30; const DM_SESSION_LIFETIME = 1000 * 60 * 60 * 24 * 30;
// Listen to system messages // Listen to system messages
client.on('messageCreate', async message => { client.on('messageCreate', async message => {
let sysMsg = message.systemMessage; let systemMessage = message.systemMessage;
if (sysMsg) switch(sysMsg.type) { if (systemMessage) switch(systemMessage.type) {
case 'user_kicked': case 'user_kicked':
case 'user_banned': case 'user_banned':
try { try {
let sysMsg = systemMessage as UserSystemMessage;
let recentEvents = await dbs.INFRACTIONS.findOne({ let recentEvents = await dbs.INFRACTIONS.findOne({
date: { $gt: Date.now() - 30000 }, date: { $gt: Date.now() - 30000 },
user: sysMsg.id, user: sysMsg.userId,
server: message.channel!.serverId!, server: message.channel!.serverId!,
actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban', actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban',
}); });
if (!message.channel || if (!message.channel ||
!sysMsg.id || !sysMsg.userId ||
recentEvents) return; recentEvents) return;
storeInfraction({ storeInfraction({
@ -38,28 +40,30 @@ client.on('messageCreate', async message => {
date: message.createdAt.getTime(), date: message.createdAt.getTime(),
server: message.channel!.serverId, server: message.channel!.serverId,
type: InfractionType.Manual, type: InfractionType.Manual,
user: sysMsg.id, user: sysMsg.userId,
actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban', actionType: sysMsg.type == 'user_kicked' ? 'kick' : 'ban',
} as Infraction).catch(console.warn); } as Infraction).catch(console.warn);
} catch(e) { console.error(e) } } catch(e) { console.error(e) }
break; break;
case 'user_joined': { case 'user_joined': {
try { try {
let sysMsg = systemMessage as UserSystemMessage;
const [ serverConfig, userConfig ] = await Promise.all([ const [ serverConfig, userConfig ] = await Promise.all([
dbs.SERVERS.findOne({ id: message.channel!.serverId }), dbs.SERVERS.findOne({ id: message.channel!.serverId }),
dbs.USERS.findOne({ id: sysMsg.id }), dbs.USERS.findOne({ id: sysMsg.userId }),
]); ]);
if (userConfig?.globalBlacklist && !serverConfig?.allowBlacklistedUsers) { if (userConfig?.globalBlacklist && !serverConfig?.allowBlacklistedUsers) {
const server = message.channel?.server; const server = message.channel?.server;
if (server && server.havePermission('BanMembers')) { if (server && server.havePermission('BanMembers')) {
await server.banUser(sysMsg.id, { reason: BLACKLIST_BAN_REASON }); await server.banUser(sysMsg.userId, { reason: BLACKLIST_BAN_REASON });
if (server.systemMessages?.user_banned) { if (server.systemMessages?.user_banned) {
const channel = server.channels.find(c => c?.id == server.systemMessages?.user_banned); const channel = server.channels.find(c => c?.id == server.systemMessages?.user_banned);
if (channel && channel.havePermission('SendMessage')) { if (channel && channel.havePermission('SendMessage')) {
const user = client.users.get(sysMsg.id); const user = sysMsg.user;
await channel.sendMessage(BLACKLIST_MESSAGE(user?.username ?? sysMsg.id)); await channel.sendMessage(BLACKLIST_MESSAGE(user?.username ?? sysMsg.userId));
} }
} }
} }

View file

@ -1,9 +1,9 @@
import { BRIDGED_EMOJIS, BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from ".."; import { BRIDGED_EMOJIS, BRIDGED_MESSAGES, BRIDGE_CONFIG, logger } from "..";
import { AUTUMN_URL, client } from "./client"; import { AUTUMN_URL, client } from "./client";
import { client as discordClient } from "../discord/client"; import { client as discordClient } from "../discord/client";
import { Channel as DiscordChannel, Message as DiscordMessage, MessageEmbed, MessagePayload, TextChannel, WebhookClient, WebhookMessageOptions } from "discord.js"; import { Message as DiscordMessage, MessageEmbed, MessagePayload, TextChannel, WebhookClient, WebhookMessageOptions } from "discord.js";
import GenericEmbed from "../types/GenericEmbed"; import GenericEmbed from "../types/GenericEmbed";
import { SendableEmbed, SystemMessage } from "revolt-api"; import { SendableEmbed } from "revolt-api";
import { import {
clipText, clipText,
discordFetchMessage, discordFetchMessage,
@ -13,6 +13,7 @@ import {
import { smartReplace } from "smart-replace"; import { smartReplace } from "smart-replace";
import { metrics } from "../metrics"; import { metrics } from "../metrics";
import { fetchEmojiList } from "../discord/bridgeEmojis"; import { fetchEmojiList } from "../discord/bridgeEmojis";
import { ChannelRenamedSystemMessage, SystemMessage, TextSystemMessage, UserSystemMessage } from "revolt.js";
const RE_MENTION_USER = /<@[0-9A-HJ-KM-NP-TV-Z]{26}>/g; const RE_MENTION_USER = /<@[0-9A-HJ-KM-NP-TV-Z]{26}>/g;
const RE_MENTION_CHANNEL = /<#[0-9A-HJ-KM-NP-TV-Z]{26}>/g; const RE_MENTION_CHANNEL = /<#[0-9A-HJ-KM-NP-TV-Z]{26}>/g;
@ -507,35 +508,35 @@ async function renderSystemMessage(message: SystemMessage): Promise<string> {
case "user_joined": case "user_joined":
case "user_added": case "user_added":
return `<:joined:1042831832888127509> ${await getUsername( return `<:joined:1042831832888127509> ${await getUsername(
message.id (message as UserSystemMessage).userId
)} joined`; )} joined`;
case "user_left": case "user_left":
case "user_remove": case "user_remove":
return `<:left:1042831834259652628> ${await getUsername( return `<:left:1042831834259652628> ${await getUsername(
message.id (message as UserSystemMessage).userId
)} left`; )} left`;
case "user_kicked": case "user_kicked":
return `<:kicked:1042831835421483050> ${await getUsername( return `<:kicked:1042831835421483050> ${await getUsername(
message.id (message as UserSystemMessage).userId
)} was kicked`; )} was kicked`;
case "user_banned": case "user_banned":
return `<:banned:1042831836675588146> ${await getUsername( return `<:banned:1042831836675588146> ${await getUsername(
message.id (message as UserSystemMessage).userId
)} was banned`; )} was banned`;
case "channel_renamed": case "channel_renamed":
return `<:channel_renamed:1042831837912891392> ${await getUsername( return `<:channel_renamed:1042831837912891392> ${await getUsername(
message.by (message as ChannelRenamedSystemMessage).byId
)} renamed the channel to **${message.name}**`; )} renamed the channel to **${(message as ChannelRenamedSystemMessage).name}**`;
case "channel_icon_changed": case "channel_icon_changed":
return `<:channel_icon:1042831840538542222> ${await getUsername( return `<:channel_icon:1042831840538542222> ${await getUsername(
message.by (message as ChannelRenamedSystemMessage).byId
)} changed the channel icon`; )} changed the channel icon`;
case "channel_description_changed": case "channel_description_changed":
return `<:channel_description:1042831839217328228> ${await getUsername( return `<:channel_description:1042831839217328228> ${await getUsername(
message.by (message as ChannelRenamedSystemMessage).byId
)} changed the channel description`; )} changed the channel description`;
case "text": case "text":
return message.content; return (message as TextSystemMessage).content;
default: default:
return Object.entries(message) return Object.entries(message)
.map((e) => `${e[0]}: ${e[1]}`) .map((e) => `${e[0]}: ${e[1]}`)

Binary file not shown.