Skip to content

Commit

Permalink
Added message reader with reddit auto thread creator
Browse files Browse the repository at this point in the history
  • Loading branch information
5jiji committed Oct 28, 2023
1 parent 128a008 commit e43dbf2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/events/Message/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { ExtendedClient } from "../../Client.js";
import { MessageManager } from "../../managers/messages/init.js";
import { Events } from "../base.js";
import { Message } from "discord.js";
import { Message, PartialMessage } from "discord.js";

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

public execute(message: Message) {
message
public async execute(message: Message | PartialMessage) {
const mes = message.partial ? await message.fetch() : message;
const client: ExtendedClient = message.client;

if (client.managers?.message instanceof MessageManager) client.managers.message.newMessage(mes);
}
}
29 changes: 29 additions & 0 deletions src/managers/messages/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Message } from "discord.js";
import { BaseManager } from "../base.js";
import { readdir } from "node:fs/promises";
import { URL, fileURLToPath } from "node:url";
import { BaseMessage } from "./messages/base.js";

export class MessageManager extends BaseManager {
public name = "message";
#messages: BaseMessage[] = [];

public async init() {
const __dirname = fileURLToPath(new URL(".", import.meta.url));
const files = await readdir(__dirname + "messages");

for (const filename of files) {
const message: BaseMessage = (await import(__dirname + "messages/" + filename)).default;
if (!message || !message.enable) continue;
this.#messages.push(message);
}
}

public async newMessage(message: Message) {
for (const messageConfig of this.#messages) {
messageConfig.newMessage(message)
}
}
}

export default new MessageManager();
7 changes: 7 additions & 0 deletions src/managers/messages/messages/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Message } from "discord.js";

export abstract class BaseMessage {
public abstract enable: boolean;

public abstract newMessage(message: Message): any;
}
15 changes: 15 additions & 0 deletions src/managers/messages/messages/reddit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Message } from "discord.js";
import { BaseMessage } from "./base.js";

export class Reddit extends BaseMessage {
public enable = true;

public newMessage(message: Message) {
if (message.author.id !== "282286160494067712") return;

const title = message.embeds[0]?.title ?? "Couldn't obtain embed title.";
message.startThread({name: title.length > 100 ? title.slice(0, 97) + "..." : title})
}
}

export default new Reddit();

0 comments on commit e43dbf2

Please sign in to comment.