Improve service handling
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import type { Message } from "discord.js";
|
import type { Message } from "discord.js";
|
||||||
import type { Command } from "../service";
|
import ChanService from "../../services/chan";
|
||||||
import Chan from "../../util/chan";
|
import type { Command } from "../../models";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "chan",
|
name: "chan",
|
||||||
@@ -14,11 +14,11 @@ export default {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (Chan.isNsfwBoard(board)) {
|
if (ChanService.isNsfwBoard(board)) {
|
||||||
await message.reply("Fuck off nigger")
|
await message.reply("Fuck off nigger")
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
await message.reply(await Chan.getMediaLink(board, search));
|
await message.reply(await ChanService.getMediaLink(board, search));
|
||||||
}
|
}
|
||||||
} satisfies Command;
|
} satisfies Command;
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import type { Message } from "discord.js";
|
import type { Message } from "discord.js";
|
||||||
import type { Command } from "../service";
|
import ChanService from "../../services/chan";
|
||||||
import Chan from "../../util/chan";
|
import type { Command } from "../../models";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "comfy",
|
name: "comfy",
|
||||||
@@ -9,6 +9,6 @@ export default {
|
|||||||
execute: async (message: Message) => {
|
execute: async (message: Message) => {
|
||||||
const boards = ["wsg", "wg"];
|
const boards = ["wsg", "wg"];
|
||||||
const board = boards[Math.floor(Math.random() * boards.length)] ?? "wsg";
|
const board = boards[Math.floor(Math.random() * boards.length)] ?? "wsg";
|
||||||
await message.reply(await Chan.getMediaLink(board, "comfy"));
|
await message.reply(await ChanService.getMediaLink(board, "comfy"));
|
||||||
}
|
}
|
||||||
} satisfies Command;
|
} satisfies Command;
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
import type { Message } from "discord.js";
|
import type { Message } from "discord.js";
|
||||||
import type { Command } from "../service";
|
import type { Command } from "../../models";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "limbo",
|
name: "limbomode",
|
||||||
description: "Toggles on/off limbo mode. When limbo mode is on, new members will be placed in #limbo. When off, new members will be placed in #chat-and-shitpost.",
|
description: "Toggles on/off limbo mode. When limbo mode is on, new members will be placed in #limbo. When off, new members will be placed in #chat-and-shitpost.",
|
||||||
usage: ".limbo [on|off]",
|
usage: ".limbomode [on|off]",
|
||||||
execute: async (message: Message) => {
|
execute: async (message: Message) => {
|
||||||
}
|
}
|
||||||
} satisfies Command;
|
} satisfies Command;
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
import type { Message } from "discord.js";
|
import type { Message } from "discord.js";
|
||||||
import type { Command } from "../service";
|
import type { Command } from "../../models";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "redpillme",
|
name: "redpillme",
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import type { Message } from "discord.js";
|
||||||
|
import type { Command } from "../../models";
|
||||||
|
|
||||||
|
export default {
|
||||||
|
name: "roleinit",
|
||||||
|
description: "Creates all of the required roles, if they do not exist yet.",
|
||||||
|
usage: ".roleinit",
|
||||||
|
execute: async (message: Message) => {
|
||||||
|
}
|
||||||
|
} satisfies Command;
|
||||||
@@ -1,34 +0,0 @@
|
|||||||
import type { Client, Message } from "discord.js";
|
|
||||||
import { readdir } from "fs/promises";
|
|
||||||
|
|
||||||
const CMD_PREFIX = ".";
|
|
||||||
|
|
||||||
export interface Command {
|
|
||||||
name: string,
|
|
||||||
description: string,
|
|
||||||
usage: string,
|
|
||||||
execute: (message: Message) => void | Promise<void>
|
|
||||||
}
|
|
||||||
|
|
||||||
export class CommandService {
|
|
||||||
private client: Client
|
|
||||||
private registry: Record<string, Command> = {}
|
|
||||||
|
|
||||||
constructor(client: Client) {
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
async init() {
|
|
||||||
const commands = await Promise.all(
|
|
||||||
(await readdir(`${import.meta.dir}/registry`)).map(async file => (await import(`./registry/${file}`)).default as Command)
|
|
||||||
);
|
|
||||||
for (const command of commands)
|
|
||||||
this.registry[command.name] = command;
|
|
||||||
};
|
|
||||||
|
|
||||||
parse(message: Message): Command | undefined {
|
|
||||||
if (!message.content.startsWith(CMD_PREFIX)) return;
|
|
||||||
const name = message.content.slice(CMD_PREFIX.length).split(/\s+/)[0];
|
|
||||||
return this.registry[name ?? ""];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+9
-11
@@ -1,36 +1,34 @@
|
|||||||
import { Client, Events, GatewayIntentBits } from 'discord.js';
|
import { Client, Events, GatewayIntentBits } from 'discord.js';
|
||||||
import { CommandService } from './cmd/service';
|
|
||||||
import RoleService from './services/role';
|
import RoleService from './services/role';
|
||||||
|
import CommandService from './services/command';
|
||||||
|
|
||||||
const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent] });
|
const client = new Client({
|
||||||
|
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
|
||||||
// Services
|
});
|
||||||
const commandService = new CommandService(client);
|
|
||||||
const roleService = new RoleService(client);
|
|
||||||
|
|
||||||
client.once(Events.ClientReady, async (readyClient) => {
|
client.once(Events.ClientReady, async (readyClient) => {
|
||||||
// Initialize & validate
|
// Initialize & validate
|
||||||
await commandService.init();
|
await CommandService.init();
|
||||||
roleService.validate(client.guilds.cache);
|
RoleService.validate(client.guilds.cache);
|
||||||
|
|
||||||
// Ready!
|
// Ready!
|
||||||
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
console.log(`Ready! Logged in as ${readyClient.user.tag}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("guildMemberAdd", async (guildMember) => {
|
client.on("guildMemberAdd", async (guildMember) => {
|
||||||
await roleService.giveStarterRoles(guildMember);
|
await RoleService.giveStarterRoles(guildMember);
|
||||||
});
|
});
|
||||||
|
|
||||||
client.on("messageCreate", async (message) => {
|
client.on("messageCreate", async (message) => {
|
||||||
if (message.author.bot) return;
|
if (message.author.bot) return;
|
||||||
|
|
||||||
const command = commandService.parse(message);
|
const command = CommandService.parse(message);
|
||||||
if (command) command.execute(message);
|
if (command) command.execute(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
Bun.cron("0 */6 * * *", async () => {
|
Bun.cron("0 */6 * * *", async () => {
|
||||||
for (const guild of client.guilds.cache.values()) {
|
for (const guild of client.guilds.cache.values()) {
|
||||||
const lurkerRole = roleService.getRoleByName(guild, RoleService.ROLES["LURKER"]?.name);
|
const lurkerRole = RoleService.getRoleByName(guild, RoleService.ROLES["LURKER"]?.name);
|
||||||
if (!lurkerRole) continue;
|
if (!lurkerRole) continue;
|
||||||
|
|
||||||
await guild.members.prune({ days: 5, roles: [lurkerRole] });
|
await guild.members.prune({ days: 5, roles: [lurkerRole] });
|
||||||
|
|||||||
@@ -0,0 +1,10 @@
|
|||||||
|
import type { Message } from "discord.js";
|
||||||
|
|
||||||
|
// COMMANDS
|
||||||
|
|
||||||
|
export interface Command {
|
||||||
|
name: string,
|
||||||
|
description: string,
|
||||||
|
usage: string,
|
||||||
|
execute: (message: Message) => void | Promise<void>
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
export default class Chan {
|
export default abstract class ChanService {
|
||||||
private static readonly nsfwBoards = new Set([
|
private static readonly nsfwBoards = new Set([
|
||||||
"b", "d", "e", "h", "hc", "hr", "gif", "s", "t", "u", "r9k", "soc", "vr", "w",
|
"b", "d", "e", "h", "hc", "hr", "gif", "s", "t", "u", "r9k", "soc", "vr", "w",
|
||||||
"i", "ic", "r", "p", "cgl", "cm", "n", "vip", "qa", "y", "trash", "hm", "aco", "lgbt"
|
"i", "ic", "r", "p", "cgl", "cm", "n", "vip", "qa", "y", "trash", "hm", "aco", "lgbt"
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import type { Message } from "discord.js";
|
||||||
|
import { readdir } from "fs/promises";
|
||||||
|
import type { Command } from "../models";
|
||||||
|
|
||||||
|
const CMD_PREFIX = ".";
|
||||||
|
|
||||||
|
export default abstract class CommandService {
|
||||||
|
private static registry: Record<string, Command> = {}
|
||||||
|
|
||||||
|
static async init() {
|
||||||
|
const commands = await Promise.all(
|
||||||
|
(await readdir(`${import.meta.dir}/registry`)).map(async file => (await import(`./registry/${file}`)).default satisfies Command)
|
||||||
|
);
|
||||||
|
for (const command of commands)
|
||||||
|
CommandService.registry[command.name] = command;
|
||||||
|
};
|
||||||
|
|
||||||
|
static parse(message: Message): Command | undefined {
|
||||||
|
if (!message.content.startsWith(CMD_PREFIX)) return;
|
||||||
|
const name = message.content.slice(CMD_PREFIX.length).split(/\s+/)[0];
|
||||||
|
return this.registry[name ?? ""];
|
||||||
|
}
|
||||||
|
}
|
||||||
+6
-12
@@ -1,6 +1,6 @@
|
|||||||
import type { Client, Guild, GuildMember, Collection, RoleCreateOptions } from "discord.js";
|
import type { Guild, GuildMember, Collection, RoleCreateOptions } from "discord.js";
|
||||||
|
|
||||||
export default class RoleService {
|
export default abstract class RoleService {
|
||||||
static readonly ROLES: Record<string, RoleCreateOptions> = {
|
static readonly ROLES: Record<string, RoleCreateOptions> = {
|
||||||
// Main
|
// Main
|
||||||
"ROBOT": {
|
"ROBOT": {
|
||||||
@@ -59,20 +59,14 @@ export default class RoleService {
|
|||||||
|
|
||||||
static readonly STARTER_ROLES = [RoleService.ROLES["LURKER"], RoleService.ROLES["NEWFAG"]];
|
static readonly STARTER_ROLES = [RoleService.ROLES["LURKER"], RoleService.ROLES["NEWFAG"]];
|
||||||
|
|
||||||
private client: Client
|
static validate(guilds: Collection<string, Guild>): void {
|
||||||
|
|
||||||
constructor(client: Client) {
|
|
||||||
this.client = client;
|
|
||||||
}
|
|
||||||
|
|
||||||
validate(guilds: Collection<string, Guild>): void {
|
|
||||||
guilds.forEach(guild => {
|
guilds.forEach(guild => {
|
||||||
const missing = Object.values(RoleService.ROLES).filter(role => !this.getRoleByName(guild, role.name));
|
const missing = Object.values(RoleService.ROLES).filter(role => !this.getRoleByName(guild, role.name));
|
||||||
if (missing.length) console.error(`${guild.name} is missing roles: ${missing.map(r => r.name).join(", ")}`);
|
if (missing.length) console.error(`${guild.name} is missing roles: ${missing.map(r => r.name).join(", ")}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async createRoles(guild: Guild, roles: RoleCreateOptions[]) {
|
static async createRoles(guild: Guild, roles: RoleCreateOptions[]) {
|
||||||
for (const role of roles) {
|
for (const role of roles) {
|
||||||
try {
|
try {
|
||||||
await guild.roles.create(role);
|
await guild.roles.create(role);
|
||||||
@@ -83,7 +77,7 @@ export default class RoleService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async giveStarterRoles(guildMember: GuildMember) {
|
static async giveStarterRoles(guildMember: GuildMember) {
|
||||||
for (const starterRole of RoleService.STARTER_ROLES) {
|
for (const starterRole of RoleService.STARTER_ROLES) {
|
||||||
const role = this.getRoleByName(guildMember.guild, starterRole?.name);
|
const role = this.getRoleByName(guildMember.guild, starterRole?.name);
|
||||||
if (!role) continue;
|
if (!role) continue;
|
||||||
@@ -97,7 +91,7 @@ export default class RoleService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getRoleByName(guild: Guild, roleName?: string) {
|
static getRoleByName(guild: Guild, roleName?: string) {
|
||||||
return guild.roles.cache.find(role => role.name === roleName);
|
return guild.roles.cache.find(role => role.name === roleName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user