Skip to content

Commit

Permalink
It is finaly time.
Browse files Browse the repository at this point in the history
  • Loading branch information
5jiji committed Oct 27, 2023
1 parent dcdf452 commit 95cf9c6
Show file tree
Hide file tree
Showing 51 changed files with 1,530 additions and 702 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"rules": {
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-explicit-any": 0,
"@typescript-eslint/no-empty-function": 0
"@typescript-eslint/no-empty-function": 0,
"no-debugger": 0
}
}
5 changes: 3 additions & 2 deletions .github/workflows/typeCheck.yml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ name: "Check if compilable"

on:
push:
branches: main
branches: [main]
pull_request:
branches: main
branches: [main]

jobs:
build:
Expand All @@ -16,4 +16,5 @@ jobs:
with:
node-version: 18
- run: npm ci
- run: npm run fast-setup
- run: tsc -b
Empty file modified LICENSE
100644 → 100755
Empty file.
439 changes: 288 additions & 151 deletions package-lock.json

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"type": "module",
"scripts": {
"build": "npm i && tsc -b",
"run": "node dist/main.js"
"run": "node dist/main.js",
"setup": "node setup.js",
"fast-setup": "node setup.js -fast",
"test": "jest"
},
"keywords": [
"discord bot",
Expand All @@ -16,11 +19,11 @@
"author": "5jiji",
"license": "MIT",
"dependencies": {
"@atproto/api": "^0.6.19",
"axios": "^1.4.0",
"discord.js": "^14.8.0",
"mysql2": "^3.3.3",
"sequelize": "^6.31.0",
"typescript": "^5.1.6"
"sequelize": "^6.31.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.57.0",
Expand Down
11 changes: 6 additions & 5 deletions src/GetFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ExtendedClient } from "./Client.js";
import { Collection } from "discord.js";
import { readdir } from "fs/promises";
import { URL, fileURLToPath } from "url";
import { URL, fileURLToPath } from "node:url";
// Events
import type { Events } from "./events/base.js";
// Interactions
Expand Down Expand Up @@ -49,8 +49,8 @@ export async function getAllInteractions(log = false) {
const CollectionContext = new Collection<string, Interaction>();

for (const { default: command } of commandsInteractions) {
if (command.data.name) CollectionCommands.set(command.data.name, command);
if (log) console.log(`Initiated Command "${command.data.name}"`)
if (command?.data?.name) CollectionCommands.set(command.data.name, command);
if (log && command?.data?.name) console.log(`Initiated Command "${command.data.name}"`)
}

for (const { default: context } of contextInteractions) {
Expand Down Expand Up @@ -93,7 +93,8 @@ export async function getAllManagers(client: ExtendedClient, log = false) {
client.managers = {};

for (const {default: manager} of managers) {
manager.init(client);
if (log) console.log(`Loaded manager ${manager.name}`)
if (!manager || !manager.init) continue;
if (log) console.log(`Loading manager ${manager.name}`)
await manager.init(client);
}
}
27 changes: 22 additions & 5 deletions src/baseConfig.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { RoleResolvable } from "discord.js";
import { ExtendedClient } from "./Client";

export interface BaseConfig {
bot: {
token: string,
Expand All @@ -22,19 +25,33 @@ export interface BaseConfig {

thingboard: {
enable: boolean,
emoji: ThingBoardEmojiObject[]
emoji: ThingBoardEmoji[]
},

socials: {
mastodon: {
enable: boolean
enable: boolean;
emojis: EmojiObject[];
socials: {
[key: string]: {
enable: boolean;
config?: ImplementationConfig;
} | undefined;
}
},

start: (client: ExtendedClient) => any;

jailRole: RoleResolvable,
}

interface ThingBoardEmojiObject {
export interface EmojiObject {
emoji: string,
number: number,
}

export interface ThingBoardEmoji extends EmojiObject {
channelId: string,
serverId: string,
number: number,
}

export type ImplementationConfig = Record<string, unknown> | undefined;
4 changes: 3 additions & 1 deletion src/dos/commands/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import type { ExtendedClient } from "../../Client.js"

export abstract class DOSCommands {
public abstract name: string | string[];
public abstract execute: (this: DOSCommands, config: Config, client: ExtendedClient<true>, args: string[], ...rest: any[]) => Promise<Config | void>;
public abstract execute(this: DOSCommands, config: Config, client: ExtendedClient<true>, args: string[], ...rest: any[]): Promise<Config | void>;
public arguments = "";
public help = "";
}
6 changes: 3 additions & 3 deletions src/dos/commands/exit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class Exit extends DOSCommands {
public name = "exit";
public execute = async (config: Config, client: ExtendedClient) => {
if (!env.PM2_USAGE) {
config.cmd.close()
client.destroy()
exit(0)
config.cmd.close();
await client.destroy();
exit(0);
}
console.log("You are using pm2, to exit the command line, use Ctrl+C.\nIf you want to reload command file, use reload in the C: drive");
return config;
Expand Down
2 changes: 1 addition & 1 deletion src/dos/commands/reload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Reload extends DOSCommands {
const map = new Map()
map.set("commands", commands)
map.set("context", context)

client.interactions = map;

console.log("Reload finished.");
break
}
Expand Down
20 changes: 10 additions & 10 deletions src/dos/commands/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class Status extends DOSCommands {

if (args.join("").trim() == "") client.user.setActivity()

const status = args[1]
const status = args[0]
const activity = {
name: args.slice(3).join(" "),
type: args[2].toUpperCase(),
name: args.slice(2).join(" "),
type: args[1].toUpperCase(),
}

if (this.checkActivity(activity)) client.user.setActivity(activity);
Expand All @@ -35,20 +35,20 @@ class Status extends DOSCommands {
checkActivity(activity: Record<any, unknown>): activity is ActivitiesOptions {
if (!(typeof activity.name === "string" || activity.name === undefined)) return false;
if (!(typeof activity.url === "string" || activity.url === undefined)) return false;

switch (typeof activity.type) {
case "string": {
switch (activity.type.toLowerCase()) {
case "playing": case "streaming":
case "listening": case "watching":
case "competing": break;
case "playing": case "streaming":
case "listening": case "watching":
case "competing": case "custom":
break;
default: return false;
}
break;
}
}
case "number": {
// 4 is custom and invalid in the case of a status.
if (![1, 2, 3, 5].includes(activity.type)) return false;
if (![1, 2, 3, 4, 5].includes(activity.type)) return false;
break;
}
default: return false
Expand Down
4 changes: 2 additions & 2 deletions src/dos/commands/tail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CategoryChannel } from "discord.js";

class Tail extends DOSCommands {
public name = "tail";
public execute = async (config: Config, client: ExtendedClient, args: string[]) => {
async execute(config: Config, client: ExtendedClient, args: string[]) {
if (config.drives.current === "C" || config.drives.S.current == null) {
console.log("Please, enter a server in the 'S' drive");
return;
Expand All @@ -25,7 +25,7 @@ class Tail extends DOSCommands {

const MessageAmount = parseInt(args[2]) || 10
for (const [, e] of await server.messages.fetch({ limit: MessageAmount })) {
console.log(`${e.id.padEnd(20)}${e.author.tag.padEnd(13)}${e.content}`)
console.log(`${e.id.padEnd(20)}${(e.author.discriminator !== "#0" ? e.author.username : `${e.author.username}#${e.author.discriminator}`).padEnd(13)}${e.content}`)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/dos/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getAllDOSCommands } from "../GetFiles.js";
export async function ProcessDOS(client: ExtendedClient<true>) {
const commands = await getAllDOSCommands();
const cmd = createInterface(input, output);

let config: Config = {
drives: {
current: "S",
Expand Down Expand Up @@ -36,13 +37,12 @@ export async function ProcessDOS(client: ExtendedClient<true>) {
cmd.on('line', async line => {
const commandName = line.trim().split(" ")[0];
if (commandName === "") return;
if (line.trim() === "eval commands") return console.log(commands)

const command = commands.get(commandName);
if (!command) return console.log("Bad command or file name")

const maybeConfig = await command.execute(config, client, line.trim().split(" ").slice(1)).catch(console.log)
if (maybeConfig) config = maybeConfig;
const newConfig = await command.execute(config, client, line.trim().split(" ").slice(1)).catch(console.error)
if (newConfig) config = newConfig;

cmd.setPrompt(`${config.drives.current}:${config.drives.dir.join("")}>`)
cmd.prompt();
Expand Down
27 changes: 9 additions & 18 deletions src/events/Client/ready.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { OAuth2Guild, Collection } from "discord.js";
import type { ExtendedClient } from "../../Client";
import { ProcessDOS } from "../../dos/main.js"
import { Events } from "../base.js";
import { errorToEmbed } from "../../util/errorConverter.js";

export default new class ClientReady extends Events {
public name = "ready";
public name = "ready" as const;
public once = true;

public execute = async (client: ExtendedClient<true>): Promise<void> => {
console.log(`Connected as ${client.user?.tag}`);

const guilds = await client.guilds.fetch();
async execute(client: ExtendedClient<true>): Promise<void> {
console.log(`Connected as ${client.user.discriminator !== "#0" ? client.user.username : `${client.user.username}#${client.user.discriminator}`}`);
ProcessDOS(client);
await this.#fetchAllChannels(guilds);
console.log("Every guilds has been loaded");

try {
if (client.config?.start) client.config.start(client);
} catch {
console.error("Error while running the start function in the config.");
}

process.on("uncaughtException", (error) => {
console.error(error);
if (client.logging?.error) client.logging.error.send({ embeds: [errorToEmbed(error, "uncaughtException")] }).catch();
Expand All @@ -26,14 +27,4 @@ export default new class ClientReady extends Events {
if (client.logging?.error) client.logging.error.send({ embeds: [errorToEmbed(error, "unhandledRejection")] }).catch();
});
}

async #fetchAllChannels(guilds: Collection<string, OAuth2Guild>): Promise<void> {
for (const [, guild] of guilds) {
console.log(`Fetching all channels & threads from ${guild.name} (${guild.id})...`)
const guildData = await guild.fetch()
await guildData.channels.fetch();

console.log(`Fetched all channels from ${guild.name}.`)
}
}
}
12 changes: 6 additions & 6 deletions src/events/Interaction/create.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { ChatInputCommandInteraction, BaseInteraction, ContextMenuCommandInteraction, ModalSubmitInteraction, CategoryChannel, PartialGroupDMChannel, Events as Event, codeBlock } from "discord.js";
import { ChatInputCommandInteraction, ContextMenuCommandInteraction, ModalSubmitInteraction, CategoryChannel, PartialGroupDMChannel, codeBlock, Interaction } from "discord.js";
import { ExtendedClient } from "../../Client";
import { Events } from "../base.js";

export default new class InteractionCreate extends Events {
public name = Event.InteractionCreate;
public name = "interactionCreate" as const;
public once = false;

async execute(interaction: BaseInteraction): Promise<any> {
async execute(interaction: Interaction): Promise<any> {
if (interaction instanceof ChatInputCommandInteraction) return await this.getCommand(interaction);
else if (interaction instanceof ContextMenuCommandInteraction) return await this.getContext(interaction);
else if (interaction instanceof ModalSubmitInteraction) return await this.getModal(interaction);
if (interaction instanceof ContextMenuCommandInteraction) return await this.getContext(interaction);
if (interaction instanceof ModalSubmitInteraction) return await this.getModal(interaction);
}

async getCommand(interaction: ChatInputCommandInteraction): Promise<void> {
Expand All @@ -21,7 +21,7 @@ export default new class InteractionCreate extends Events {

await command.execute(interaction).catch(e => {
console.error(e);
if (interaction.deferred) interaction.followUp(e);
if (interaction.deferred) interaction.followUp(`An error ocurred while running this command: ${codeBlock(e)}`);
else interaction.reply(`An error ocurred while running this command: ${codeBlock(e)}`);
});
}
Expand Down
13 changes: 8 additions & 5 deletions src/events/Message/Reaction/add.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import { MessageReaction, User, Events as Event } from "discord.js";
import { Events } from "../../base";
import { MessageReaction, PartialMessageReaction, User } from "discord.js";
import { Events } from "../../base.js";
import { ExtendedClient } from "../../../Client";
import { ThingBoardManager } from "../../../managers/thingboard/init.js";
import { SocialsManager } from "../../../managers/socials/init.js";

export default new class MessageReactionAdd extends Events {
public name = Event.MessageReactionAdd;
public name = "messageReactionAdd" as const;
public once = false;

public execute(reaction: MessageReaction, user: User) {
public execute(reaction: MessageReaction | PartialMessageReaction, user: User) {
const client: ExtendedClient<true> = reaction.client;

if (client.managers?.starboard && client.config?.starboard.enable) client.managers.starboard.checkReactions(reaction, user);
if (client.managers?.thingboard instanceof ThingBoardManager && client.config?.thingboard?.enable) client.managers.thingboard.check(reaction, user);
if (client.managers?.socials instanceof SocialsManager && client.config?.socials?.enable) client.managers.socials.check(reaction, user);
}
}
4 changes: 2 additions & 2 deletions src/events/Message/create.ts
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Events } from "../base.js";
import { Events as Event, Message } from "discord.js";
import { Message } from "discord.js";

export default new class MessageCreate extends Events {
public name = Event.MessageCreate;
public name = "messageCreate" as const;
public once = false;

public execute(message: Message) {
Expand Down
6 changes: 4 additions & 2 deletions src/events/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ClientEvents } from "discord.js";

export abstract class Events {
public abstract name: string;
public abstract name: keyof ClientEvents;
public abstract once: boolean;
public abstract execute(this: Events, ...args: any[]): any;
public abstract execute(this: Events, ...args: ClientEvents[typeof this.name]): any;
}
Loading

0 comments on commit 95cf9c6

Please sign in to comment.