Skip to content

Commit

Permalink
blocklist is now in the repo
Browse files Browse the repository at this point in the history
  • Loading branch information
MrSerge01 committed Dec 20, 2024
1 parent af1adb8 commit 2b5d5ef
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 4 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"devDependencies": {
"@types/ms": "^0.7.34",
"bun-types": "^1.1.38",
"bun-types": "^1.1.41",
"typescript": "^5.7.2"
}
}
5 changes: 5 additions & 0 deletions src/bot.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ActivityType, Client } from "discord.js";
import { registerCommands } from "./handlers/commands";
import { loadEvents } from "./handlers/events";
import { leavePlease } from "./utils/leavePlease";
import { rescheduleUnbans } from "./utils/unbanScheduler";

const client = new Client({
Expand All @@ -18,6 +19,10 @@ const client = new Client({
});

client.on("ready", async () => {
const guilds = client.guilds.cache;
for (const id of guilds.keys())
await leavePlease(guilds.get(id)!, await guilds.get(id)?.fetchOwner()!, "Not like that.");

await loadEvents(client);
await registerCommands(client);
console.log("ちーっす!");
Expand Down
6 changes: 4 additions & 2 deletions src/events/guildCreate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { EmbedBuilder, type DMChannel } from "discord.js";
import { EmbedBuilder } from "discord.js";
import { commands } from "../handlers/commands";
import { genColor } from "../utils/colorGen";
import { check } from "../utils/database/blocklist";
import { leavePlease } from "../utils/leavePlease";
import { replace } from "../utils/replace";
import { Event } from "../utils/types";

export default (async function run(guild) {
const owner = await guild.fetchOwner();
if (owner.user.bot) return;
if (!check(owner.id)) return await leavePlease(guild, owner, "No.");

const client = guild.client;
const embed = new EmbedBuilder()
Expand Down
13 changes: 12 additions & 1 deletion src/events/interactionCreate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { commands, subCommands } from "../handlers/commands";
import { check } from "../utils/database/blocklist";
import { errorEmbed } from "../utils/embeds/errorEmbed";
import { Event } from "../utils/types";

export default (async function run(interaction) {
Expand All @@ -13,6 +15,15 @@ export default (async function run(interaction) {
else command = subCommand;

if (!command) return;
if (interaction.isChatInputCommand()) command.run(interaction);
if (interaction.isChatInputCommand()) {
if (!check(interaction.member?.user.id!))
return await errorEmbed(
interaction,
"The bot has experienced an internal error.",
"Please try again later."
);

command.run(interaction);
}
if (command.autocomplete) command.autocomplete(interaction);
} as Event<"interactionCreate">);
33 changes: 33 additions & 0 deletions src/events/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,48 @@ import { readdirSync } from "fs";
import { join } from "path";
import { pathToFileURL } from "url";
import { genColor } from "../utils/colorGen";
import { add, check, remove } from "../utils/database/blocklist";
import { getLevel, setLevel } from "../utils/database/leveling";
import { getSetting } from "../utils/database/settings";
import { kominator } from "../utils/kominator";
import { leavePlease } from "../utils/leavePlease";
import { Event } from "../utils/types";

const cooldowns = new Map<string, number>();
export default (async function run(message) {
if (message.content.startsWith("!SYSTEM")) {
if (message.author.id != process.env.OWNER) return;
let args = message.content.split(" ");

if (!args[2]) return message.reply("ERROR: Expected three arguments");
const username = (await message.client.users.fetch(args[2])).username;
switch (args[1]) {
case "add": {
add(args[2]);
await message.reply(`${username} has been blocklisted from Sokora.`);

const guilds = message.client.guilds.cache;
for (const id of guilds.keys())
await leavePlease(guilds.get(id)!, await guilds.get(id)?.fetchOwner()!, "No.");
break;
}
case "remove":
remove(args[2]);
await message.reply(`${username} has been removed from the Sokora blocklist.`);
break;
case "check":
await message.reply(`${!check(args[2])}`);
break;
default:
await message.reply(
"Hello, this is the system interface to control top level Sokora moderation utilities."
);
}
}

const author = message.author;
if (author.bot) return;
if (!check(author.id)) return;
const guild = message.guild!;

// Easter egg handler
Expand Down
25 changes: 25 additions & 0 deletions src/utils/database/blocklist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { getDatabase } from ".";
import { TableDefinition } from "./types";

const tableDefinition: TableDefinition = {
name: "blocklist",
definition: {
id: "INTEGER"
}
};

const database = getDatabase(tableDefinition);
const checkQuery = database.query("SELECT * FROM blocklist WHERE id = $1;");
export function check(userID: string) {
return checkQuery.all(userID).length == 0;
}

const addQuery = database.query("INSERT INTO blocklist (id) VALUES (?1);");
export function add(userID: string) {
addQuery.run(userID);
}

const removeQuery = database.query("DELETE FROM blocklist WHERE id = $1;");
export function remove(userID: string) {
removeQuery.run(userID);
}
16 changes: 16 additions & 0 deletions src/utils/leavePlease.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { EmbedBuilder, type DMChannel, type Guild, type GuildMember } from "discord.js";
import { genColor } from "./colorGen";
import { check } from "./database/blocklist";

export async function leavePlease(guild: Guild, owner: GuildMember, embedText?: string) {
if (check(owner.id)) return;
if (embedText) {
const dmChannel = (await owner.createDM().catch(() => null)) as DMChannel | undefined;
if (dmChannel)
await dmChannel.send({
embeds: [new EmbedBuilder().setTitle(embedText).setColor(genColor(0))]
});
}

return await guild.leave();
}

0 comments on commit 2b5d5ef

Please sign in to comment.