add banlist
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
.env
|
.env
|
||||||
node_modules
|
node_modules
|
||||||
mapping.json
|
mapping.json
|
||||||
|
banlist.json
|
||||||
*.js
|
*.js
|
||||||
|
|||||||
30
discord_commands.ts
Normal file
30
discord_commands.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { writeFileSync, readFileSync } from "fs";
|
||||||
|
|
||||||
|
import { PermissionsBitField, Message } from "discord.js";
|
||||||
|
|
||||||
|
let BANLIST = JSON.parse(readFileSync("./banlist.json", "utf-8"));
|
||||||
|
|
||||||
|
export function get_banlist(): Record<string, boolean> {
|
||||||
|
return BANLIST;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function handle_command(message: Message, parts: string[]) {
|
||||||
|
const command = parts.shift();
|
||||||
|
if (!message.member?.permissionsIn(message.channel.id).has(PermissionsBitField.Flags.ManageMessages)) return;
|
||||||
|
switch (command) {
|
||||||
|
case "ban":
|
||||||
|
//probably do regex
|
||||||
|
if (!parts[0].startsWith("@") || !parts[0].includes(".") || !parts[0].includes(":")) return;
|
||||||
|
if (BANLIST[parts[0].toLowerCase()]) return await message.reply(`${parts[0]} already in banlist`);
|
||||||
|
BANLIST[parts[0].toLowerCase()] = true;
|
||||||
|
writeFileSync("banlist.json", JSON.stringify(BANLIST, undefined, 2));
|
||||||
|
return await message.reply(`${parts[0]} added to banlist`);
|
||||||
|
case "unban":
|
||||||
|
//probably do regex
|
||||||
|
if (!parts[0].startsWith("@") || !parts[0].includes(".") || !parts[0].includes(":")) return;
|
||||||
|
delete BANLIST[parts[0].toLowerCase()];
|
||||||
|
writeFileSync("banlist.json", JSON.stringify(BANLIST, undefined, 2));
|
||||||
|
return await message.reply(`${parts[0]} removed from banlist`);
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
15
index.ts
15
index.ts
@@ -2,7 +2,9 @@ import { appendFileSync, readFileSync } from "fs";
|
|||||||
|
|
||||||
import * as discord from "discord.js";
|
import * as discord from "discord.js";
|
||||||
import * as matrix from "matrix-js-sdk";
|
import * as matrix from "matrix-js-sdk";
|
||||||
import { logger } from 'matrix-js-sdk/lib/logger.js';
|
import { logger } from "matrix-js-sdk/lib/logger.js";
|
||||||
|
|
||||||
|
import { handle_command, get_banlist } from "./discord_commands.js";
|
||||||
|
|
||||||
logger.disableAll();
|
logger.disableAll();
|
||||||
|
|
||||||
@@ -32,6 +34,10 @@ matrix_client.startClient({ initialSyncLimit: 0 });
|
|||||||
|
|
||||||
discord_client.on("messageCreate", async (message) => {
|
discord_client.on("messageCreate", async (message) => {
|
||||||
if (!MAPPING[message.channelId] || message.author.id === discord_client.user.id || (message.webhookId && (await message.fetchWebhook()).name === "Matrix Bridge")) return;
|
if (!MAPPING[message.channelId] || message.author.id === discord_client.user.id || (message.webhookId && (await message.fetchWebhook()).name === "Matrix Bridge")) return;
|
||||||
|
if (message.content.startsWith("bridge!")) {
|
||||||
|
const parts = message.content.replace("bridge!", "").split(" ");
|
||||||
|
await handle_command(message, parts);
|
||||||
|
}
|
||||||
const channel_id = message.channelId;
|
const channel_id = message.channelId;
|
||||||
let attachments = "";
|
let attachments = "";
|
||||||
for (const [_, a] of message.attachments) {
|
for (const [_, a] of message.attachments) {
|
||||||
@@ -51,7 +57,7 @@ discord_client.on("messageCreate", async (message) => {
|
|||||||
matrix_client.on(matrix.RoomEvent.Timeline, async (event, room) => {
|
matrix_client.on(matrix.RoomEvent.Timeline, async (event, room) => {
|
||||||
if (event.getType() !== "m.room.message") return;
|
if (event.getType() !== "m.room.message") return;
|
||||||
const { sender, content, room_id } = event.event;
|
const { sender, content, room_id } = event.event;
|
||||||
if (!MAPPING[room_id] || sender === process.env.MATRIX_USER) return;
|
if (!MAPPING[room_id] || sender == process.env.MATRIX_USER || get_banlist()[sender.toLowerCase()]) return;
|
||||||
const channel_id = MAPPING[room_id];
|
const channel_id = MAPPING[room_id];
|
||||||
let webhook_id = process.env[`webhookid_${channel_id}`];
|
let webhook_id = process.env[`webhookid_${channel_id}`];
|
||||||
let webhook_token = process.env[`webhooktoken_${channel_id}`];
|
let webhook_token = process.env[`webhooktoken_${channel_id}`];
|
||||||
@@ -66,11 +72,12 @@ matrix_client.on(matrix.RoomEvent.Timeline, async (event, room) => {
|
|||||||
process.env[`webhooktoken_${channel_id}`] = webhook_token;
|
process.env[`webhooktoken_${channel_id}`] = webhook_token;
|
||||||
appendFileSync(".env", `\nwebhookid_${channel_id}=${webhook_id}\nwebhooktoken_${channel_id}=${webhook_token}`);
|
appendFileSync(".env", `\nwebhookid_${channel_id}=${webhook_id}\nwebhooktoken_${channel_id}=${webhook_token}`);
|
||||||
}
|
}
|
||||||
|
const display_name = event.sender.rawDisplayName;
|
||||||
const webhook_client = new discord.WebhookClient({ id: webhook_id, token: webhook_token });
|
const webhook_client = new discord.WebhookClient({ id: webhook_id, token: webhook_token });
|
||||||
await webhook_client.send({
|
await webhook_client.send({
|
||||||
content: content.body,
|
content: content.body,
|
||||||
username: sender,
|
username: display_name == sender ? sender : `${display_name} (${sender})`,
|
||||||
avatarURL: event.sender.getAvatarUrl(matrix_client.getHomeserverUrl(), 128, 128, "crop", true, false).replace("media/v3", "client/v1/media"),
|
avatarURL: event.sender.getAvatarUrl(matrix_client.getHomeserverUrl(), 128, 128, "crop", true, false)?.replace("media/v3", "client/v1/media"),
|
||||||
});
|
});
|
||||||
//
|
//
|
||||||
//await (await discord_client.channels.fetch(channel_id) as discord.TextChannel).send(`**${sender}:** ${content.body}`);
|
//await (await discord_client.channels.fetch(channel_id) as discord.TextChannel).send(`**${sender}:** ${content.body}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user