From 300a37c725732e39b1c645ab0585e8e6a5cc9cc4 Mon Sep 17 00:00:00 2001 From: Struck713 Date: Sun, 17 Sep 2023 01:13:28 -0400 Subject: [PATCH] Fixed bruh count --- .gitignore | 3 ++- package.json | 2 +- src/app.ts | 13 ++++++++----- src/lib/listeners/bruh.ts | 20 ++++++++++++++++++++ src/lib/state.ts | 29 +++++++++++++++++++++++++++++ 5 files changed, 60 insertions(+), 7 deletions(-) create mode 100644 src/lib/listeners/bruh.ts create mode 100644 src/lib/state.ts diff --git a/.gitignore b/.gitignore index 51459b9..2c259a0 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ node_modules/ config.json .env dist/ -binaries/ \ No newline at end of file +binaries/ +state.lock \ No newline at end of file diff --git a/package.json b/package.json index 7ea17df..97a23f7 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "build": "npx tsc", - "start": "node dist/src/app.js", + "start": "npm run build && node dist/src/app.js", "dev": "concurrently \"npx tsc --watch\" \"nodemon -q dist/src/app.js\"" }, "author": "Struck713", diff --git a/src/app.ts b/src/app.ts index 9599491..a3fe353 100644 --- a/src/app.ts +++ b/src/app.ts @@ -1,10 +1,14 @@ import { Client, Events, GatewayIntentBits } from "discord.js"; -import { TOKEN } from '../config.json'; +import { TOKEN, BRUH } from '../config.json'; import { Commands } from "./lib/command"; import { VoiceManager } from "./lib/voice"; +import { StateManager } from "./lib/state"; +import { BruhListener } from "./lib/listeners/bruh"; -export const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.MessageContent ] }); +export const client = new Client({ intents: [ GatewayIntentBits.Guilds, GatewayIntentBits.GuildVoiceStates, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent ] }); export const voiceManager = new VoiceManager(); +export const stateManager = new StateManager(); +export const bruhManager = new BruhListener(); client.once(Events.ClientReady, async client => { console.log(`Logged in as ${client.user.tag}`); @@ -26,9 +30,8 @@ client.on(Events.InteractionCreate, async interaction => { client.on(Events.MessageCreate, async message => { if (message.author.bot) return; - if (message.content.toLowerCase().includes("bruh")) { - await message.channel.send("bruh"); - } + let content = message.content.toLowerCase(); + if (message.channelId === BRUH.MESSAGE && content === "bruh") stateManager.bruhCount++; }); //deploy(); diff --git a/src/lib/listeners/bruh.ts b/src/lib/listeners/bruh.ts new file mode 100644 index 0000000..7551052 --- /dev/null +++ b/src/lib/listeners/bruh.ts @@ -0,0 +1,20 @@ +import { client, stateManager } from "../../app"; +import { BRUH } from "../../../config.json"; + +export class BruhListener { + + private thread?: NodeJS.Timer; + + constructor() { + this.thread = setInterval(() => { + let guild = client.guilds.resolve(BRUH.GUILD); + if (!guild) return; + + let channel = guild.channels.resolve(BRUH.VOICE); + if (!channel) return; + channel.edit({ name: `Bruh Count: ${stateManager.bruhCount}`, reason: "New bruh added" }); + }, 5 * 60 * 1000); // 5 minutes + } + + +} \ No newline at end of file diff --git a/src/lib/state.ts b/src/lib/state.ts new file mode 100644 index 0000000..3bd385e --- /dev/null +++ b/src/lib/state.ts @@ -0,0 +1,29 @@ +import fs from "fs"; + +interface State { + bruhCount: number; +} + +export class StateManager { + + bruhCount: number = 0; + + constructor() { + this.load(); + } + + load = () => { + if (!fs.existsSync("state.lock")) this.save(); + + let { bruhCount } = JSON.parse(fs.readFileSync("state.lock", "utf-8")) as State; + this.bruhCount = bruhCount; + } + + save = () => { + fs.writeFileSync("state.lock", JSON.stringify({ + bruhCount: this.bruhCount + })); + } + + +} \ No newline at end of file