Skip to content

Commit

Permalink
moved bot_stats and cmd_stats to mariadb
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsfl committed Dec 27, 2024
1 parent e3754b2 commit 52f4237
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 39 deletions.
19 changes: 14 additions & 5 deletions commands/botstats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ import * as self from "../index"
import { cmdStructure } from "../modules/Command";
import CommandCategories from "../modules/CommandCategories";
import Embed from "../modules/Embed";
import Database from "../modules/Database";

class Command {
name = ["botstats", "bs", "bots"]
category = CommandCategories.Bot
description = "Shows you some bot statistics."

callback = async (cmd: cmdStructure) => {
const bot_stats: BotStats = await self.file_cache.getSync("bot_stats")
//const bot_stats: BotStats = await self.file_cache.getSync("bot_stats")
const [bot_stats, errorCode, errorMessage] = await Database.GetTable("bot_statistics")

if (errorCode || errorMessage) {
console.error(errorMessage)
return self.utils.SendErrorMessage("error", cmd, errorCode)
}

const _bot_stats: BotStats = bot_stats[0]
const embed = Embed({
title: "Lua Obfuscator - Bot Statistics",
color: Colors.Green,
Expand All @@ -23,9 +32,9 @@ class Command {
{
name: "Statistics:",
value: `
${inlineCode("Obfuscations:")} ${inlineCode(bot_stats.obfuscations.toString())}
${inlineCode("Executed Commands:")} ${inlineCode(bot_stats.total_commands_executed.toString())}
${inlineCode("Retards that tried deobf:")} ${inlineCode(bot_stats.total_monkey_deobfuscations.toString())}
${inlineCode("Obfuscations:")} ${inlineCode(_bot_stats.obfuscations.toString())}
${inlineCode("Executed Commands:")} ${inlineCode(_bot_stats.total_commands_executed.toString())}
${inlineCode("Retards that tried deobf:")} ${inlineCode(_bot_stats.deobf_tries.toString())}
`,
inline: false
}
Expand All @@ -40,7 +49,7 @@ class Command {
export interface BotStats {
obfuscations: number,
total_commands_executed: number,
total_monkey_deobfuscations: number,
deobf_tries: number,
}

module.exports = Command
13 changes: 11 additions & 2 deletions commands/cmdstats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@ import * as self from "../index"
import { cmdStructure } from "../modules/Command";
import CommandCategories from "../modules/CommandCategories";
import Embed from "../modules/Embed";
import Database from "../modules/Database";

class Command {
name = ["cmdstats", "cs", "cstats"]
category = CommandCategories.Misc
description = "Shows the statistics of all commands aka wich command is being used the most."

callback = async (cmd: cmdStructure) => {
const cmd_stats: BotStats = await self.file_cache.getSync("cmd_stats")
//const cmd_stats: BotStats = await self.file_cache.getSync("cmd_stats")
const [cmd_stats, errorCode, errorMessage] = await Database.GetTable("cmd_stats")

if (errorCode || errorMessage) {
console.error(errorMessage)
return self.utils.SendErrorMessage("error", cmd, errorCode)
}

let commands_value = ``
self.command.getAllCommands().forEach(cmd => {
commands_value += `${inlineCode(cmd.name[0] + ":")} ${bold(underscore(inlineCode(cmd_stats[cmd.name[0]] || "0")))}\n`
const stat = cmd_stats.find(s => s.command_name === cmd.name[0])
commands_value += `${inlineCode(cmd.name[0] + ":")} ${bold(underscore(inlineCode(stat.call_count || "0")))}\n`
})
const embed = Embed({
title: "Lua Obfuscator - Command Statistics",
Expand Down
42 changes: 25 additions & 17 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import express from "express"
import dotenv from "dotenv"
import fs from "fs"
import cors from "cors"
import mariadb from "mariadb"
import Config from "./config"
import Utils from "./modules/Utils"
import Command, { cmdStructure, command } from "./modules/Command"
Expand All @@ -21,6 +22,7 @@ import { createClient, RedisClientType } from "redis"
import { gzipSync } from "zlib";
import RedisClient from "./modules/RedisClient"
import ForumSyncTest, { ForumThread } from "./modules/ForumSyncTest"
//import GPTKeywordDetectorThing from "./modules/GPTKeywordDetectorThing"

const app = express()
dotenv.config()
Expand All @@ -35,6 +37,14 @@ const env = process.argv[2] || "prod"
let cache: MemoryCache
let file_cache: FileSystemCache

const pool = mariadb.createPool({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
connectionLimit: 5
});

const process_path = process.cwd()
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;

Expand Down Expand Up @@ -97,7 +107,6 @@ client.on("ready", async () => {
})
})


// statusDisplay recursion
const actionRecursion = async () => {
await action_updateStats().then((res) => {
Expand All @@ -109,12 +118,11 @@ client.on("ready", async () => {
client.on(Events.MessageCreate, async (message) => {
try {
if (message.channelId == statusDisplay.status_message.channelId) { await message.delete(); return }
if (NoHello(message) || DeobfLaugh(message)) return
//if (NoHello(message) || DeobfLaugh(message) || GPTKeywordDetectorThing(message)) return
if (message.author.bot || !message.content || !message.content.startsWith(config.prefix)) return
const _command = command.getCommand(message)?.replace(/```[^`]*```/gm, "").trim(),
_args: Array<number | string> = command.getArgs(message).splice(1)


command.commands.forEach(async c => {
if (typeof (c.name) == "object" && !c.name.includes(_command) || typeof (c.name) == "string" && c.name != _command) return
if (message.channel.isDMBased() && c.direct_message == false) return console.log(`> command '${c.name}', requested by '${message.author.username}', blocked. (direct_message not allowed)`);
Expand Down Expand Up @@ -172,8 +180,6 @@ app.listen(process.env.PORT, async () => {
file_cache.setSync("outage_log", utils.ToBase64(gzipSync(JSON.stringify(_cacheValue))))
}
})
//redisClient = new RedisClient()
//await redisClient.Init().catch(console.error)

console.log(`> programm initalized in ${new Date().getTime() - start_tick}ms`)
})
Expand All @@ -198,6 +204,7 @@ app.get("/api/v1/commands/:cmdname", (req, res) => {
res.json(_command)
})

// TODO: get botstats and cmdstats from database since its not saved in file cache anymore (why not do it now? too lazy)
app.get("/api/v1/cache/:name", async (req, res) => {
const session_ids: Array<any> = await cache.get("stats_session_ids")
if (session_ids && session_ids.includes(req.query.session) || env === "dev") {
Expand All @@ -214,6 +221,7 @@ app.get("/api/v1/cache/:name", async (req, res) => {
return res.status(401).json({ code: 401, message: "Unauthorized", error: "Invalid session id" })
})

/*
client.on(Events.ThreadCreate, async (thread, newlyCreated) => await ForumSyncTest.HandleNewThread(thread, newlyCreated))
client.on(Events.ThreadDelete, async (thread) => await ForumSyncTest.HandleDeletedThread(thread))
client.on(Events.MessageCreate, async (message) => await ForumSyncTest.HandleNewThreadMessage(message))
Expand Down Expand Up @@ -368,28 +376,28 @@ app.get("/api/dev/forum/threads/:channelId/:threadId", async (req, res) => {
<body>
<div class="container">
<a class="threaditem">
<img src="${thread.author.avatarURL}"></img>
<div class="threaditem-author grid">
<h3>${thread.name}</h3>
<p><span>${thread.author.username}</span> <span>${new Date(thread.createdTimestamp).toLocaleTimeString()}</span></p>
</div>
<div class="break"></div>
<span>${thread.firstMessage.content}</span>
</a>
${htmlStuff}
<a class="threaditem">
<img src="${thread.author.avatarURL}"></img>
<div class="threaditem-author grid">
<h3>${thread.name}</h3>
<p><span>${thread.author.username}</span> <span>${new Date(thread.createdTimestamp).toLocaleTimeString()}</span></p>
</div>
<div class="break"></div>
<span>${thread.firstMessage.content}</span>
</a>
${htmlStuff}
</div>
</body>
</html>`)
})
})*/

export interface Bot_Settings {
alert_pings: boolean
}

export {
Debug, statusDisplay, command, utils, obfuscatorStats, userPluginSaves,
client, config, env, cache, file_cache, cacheValues, redisClient,
client, config, env, cache, file_cache, cacheValues, redisClient, pool,
start_tick
}
29 changes: 14 additions & 15 deletions modules/Command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as self from "../index"
import { randomUUID } from "crypto"
import NoHello from "./NoHello"
import { BotStats } from "../commands/botstats"
import Database from "./Database"

export default class Command {
constructor(
Expand Down Expand Up @@ -40,27 +41,25 @@ export default class Command {
if (!cmd.allowed) return self.utils.SendErrorMessage("permission", cmd, "Missing required permissions.")
if (cmd.public_command === false && !self.config.allowed_guild_ids.includes(cmd.message.guildId)) return self.utils.SendErrorMessage("permission", cmd, "This command is disabled for this guild.")
if (this.ratelimits.get(cmd.message.author.id) === true) return await self.utils.SendErrorMessage("ratelimit", cmd, null, null, null, 5000);

try {
const bot_stats: BotStats = await self.file_cache.get("bot_stats"),
cmd_stats: BotStats = await self.file_cache.getSync("cmd_stats")
if (bot_stats) {
if (!bot_stats.total_commands_executed) bot_stats.total_commands_executed = 0
bot_stats.total_commands_executed++;
self.file_cache.set("bot_stats", bot_stats)
}
this.ratelimits.set(cmd.message.author.id, true);

const success = await cmd.callback(cmd)
cmd.success = success
if (cmd_stats) {
if (!cmd_stats[cmd.name[0]]) cmd_stats[cmd.name[0]] = 0
cmd_stats[cmd.name[0]]++;
self.file_cache.set("cmd_stats", cmd_stats)

const [existsInCmdStats] = await Database.RowExists("cmd_stats", { command_name: cmd.name[0] })
if (!existsInCmdStats) {
console.log(`${cmd.name[0]} cmd not registered in database yet. inserting...`);
await Database.Insert("cmd_stats", { command_name: cmd.name[0], call_count: 1 })
} else {
const [db_success, db_errorCode, db_errorMessage] = await Database.Increment("cmd_stats", "call_count", { command_name: cmd.name[0] })
if (!db_success) console.error(db_errorMessage)
}
//let command_log: Array<cmdStructure> = await self.cache.get("command_log")
//if (!command_log) { await self.cache.set("command_log", []); command_log = [] }
//command_log.push(cmd)
//await self.cache.set(JSON.stringify(command_log), cmd)

const [db_success, db_errorCode, db_errorMessage] = await Database.Increment("bot_statistics", "total_commands_executed")
if (!db_success) console.error(db_errorMessage)

this.ratelimits.set(cmd.message.author.id, false);
console.log(`> command '${cmd.used_command_name}', requested by '${cmd.message.author.username}', finished in ${new Date().getTime() - cmd.timestamp}ms (id: ${cmd.id})`);
} catch (error) {
Expand Down
98 changes: 98 additions & 0 deletions modules/Database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// my goofy ahh database module thing. works so dont blame me -.-

import { PoolConnection } from "mariadb";
import { pool } from "..";
import self from "./Database"

export type DatabaseTable = "bot_statistics" | "cmd_stats"

export default {
async GetTable(table: DatabaseTable, reqQuery?: any): Promise<[any, string?, number?]> {
let connection: PoolConnection

try {
connection = await pool.getConnection();

const [queryName, queryValue] = reqQuery && self.ParseSearchQuery(reqQuery) || [];
const query = `SELECT * FROM ${table}${queryName ? ` WHERE ${queryName} = ?` : ""}`;
const rows = await connection.query(query, queryName ? [queryValue] : []);

return rows.length ? (queryName ? [rows[0]] : [rows]) : [null, "databaseNotFound", 404];
} catch (err) {
console.error(err)
return [null, err.code, err.message]
} finally {
if (connection) connection.release();
}
},

async RowExists(table: DatabaseTable, reqQuery?: any): Promise<[any, string?, number?]> {
let connection: PoolConnection

try {
connection = await pool.getConnection();

const [queryName, queryValue] = self.ParseSearchQuery(reqQuery) || [];
const query = `SELECT * FROM ${table}${queryName ? ` WHERE ${queryName} = ?` : ""}`;
const rows = await connection.query(query, queryName ? [queryValue] : []);

return [rows.length > 0]
} catch (err) {
console.error(err)
return [null, err.code, err.message]
} finally {
if (connection) connection.release();
}
},

async Insert(table: DatabaseTable, values: Object) {
let connection: PoolConnection

try {
connection = await pool.getConnection();

let _values = "",
_valuesC = ""
Object.keys(values).forEach(v => {
_values = _values + `${v}, `
_valuesC = _valuesC + `?, `
});
_values = _values.replace(/,\s+$/gm, "")
_valuesC = _valuesC.replace(/,\s+$/gm, "")

const query = `INSERT INTO ${table} (${_values}) VALUES (${_valuesC});`
const result = await connection.query(query, Object.values(values))

} catch (err) {
console.error(err)
return [null, err.code, err.message]
} finally {
if (connection) connection.release();
}
},

async Increment(table: DatabaseTable, value: string, reqQuery?: any): Promise<[boolean, string?, number?]> {
let connection: PoolConnection

try {
connection = await pool.getConnection();
const [queryName, queryValue] = reqQuery ? self.ParseSearchQuery(reqQuery) : [];
const query = `UPDATE ${table} SET ${value} = ${value} + 1${queryName ? ` WHERE ${queryName} = ?` : ""}`;
const result = await connection.query(query, queryName ? [queryValue] : []);

return result.affectedRows > 0 ? [true, null, 200] : [false, "databaseNotFound", 404];
} catch (err) {
console.error(err)
return [false, err.code, err.message]
} finally {
if (connection) connection.release();
}
},

ParseSearchQuery(query: any): [string, string] {
const queryName = Object.keys(query)[0]
if (!queryName) return [null, null]

return [queryName, query[queryName]]
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"fs": "^0.0.1-security",
"http-status": "^1.6.2",
"luamin": "^1.0.4",
"mariadb": "^3.4.0",
"node-fetch": "^3.3.2",
"node-gzip": "^1.1.2",
"redis": "^4.6.15",
Expand Down

0 comments on commit 52f4237

Please sign in to comment.