Skip to content

Commit 2bbf36f

Browse files
committed
feat: added db usage
1 parent 7051990 commit 2bbf36f

File tree

10 files changed

+75
-2
lines changed

10 files changed

+75
-2
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ Please report bugs in `bug-reports` on our server or open an issue on this repo!
1919
> [!WARNING]
2020
> **Chatr** has entered Beta! (don't worry, we will deal with the headaches for you)
2121
22+
# Questions
23+
## Why doesn't Chatr update my user information
24+
We've noticed during testing that not all updates are properly dealt with, and we aren't able to understand why. We update information whenever we get an "event" from Discord, however not all are broadcasted properly.
25+
Sending a message will **100%** update your information as we do update your information whenever a message is sent
26+
2227
# Developer Instructions
2328

2429
This a project created using (Bun)[https://bun.sh]

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
},
99
"dependencies": {
1010
"cors": "^2.8.5",
11+
"cron": "^3.1.7",
1112
"express": "^4.19.2",
1213
"mysql2": "^3.10.3"
1314
},

api/src/db/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export const pool = mysql.createPool({
1212
export * from './init';
1313
export * from './queries/guilds';
1414
export * from './queries/users';
15-
export * from './queries/updates';
15+
export * from './queries/updates';
16+
export * from './queries/tracking';

api/src/db/init.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,14 @@ export async function initTables() {
3636
level INT NOT NULL
3737
)
3838
`;
39+
const createTrackingTable = `
40+
CREATE TABLE IF NOT EXISTS tracking (
41+
time TIMESTAMP,
42+
user_id VARCHAR(255) NOT NULL,
43+
guild_id VARCHAR(255) NOT NULL,
44+
xp INT NOT NULL
45+
)
46+
`;
3947

4048
pool.query(createGuildsTable, (err) => {
4149
if (err) {
@@ -60,4 +68,12 @@ export async function initTables() {
6068
console.log("Roles table created");
6169
}
6270
});
71+
72+
pool.query(createTrackingTable, (err) => {
73+
if (err) {
74+
console.error("Error creating tracking table:", err);
75+
} else {
76+
console.log("Tracking table created");
77+
}
78+
});
6379
}

api/src/db/queries/tracking.ts

Whitespace-only changes.

api/src/index.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,22 @@ app.get('/get/botinfo', async (_req, res) => {
181181
return res.status(200).json(data);
182182
});
183183

184+
app.get('/get/dbusage', (_req, res) => {
185+
pool.query(`SELECT table_schema AS "name", SUM(data_length + index_length) / 1024 / 1024 AS "size" FROM information_schema.TABLES GROUP BY table_schema;`, (err, results) => {
186+
if (err) {
187+
console.error("Error fetching database size:", err);
188+
return res.status(500).json({ message: "Internal server error" });
189+
} else {
190+
const discordXpBot = results.find((result) => result.name === process.env.MYSQL_DATABASE);
191+
if (discordXpBot) {
192+
return res.status(200).json({ sizeInMB: parseFloat(discordXpBot.size) });
193+
} else {
194+
return res.status(404).json({ message: "Database not found" });
195+
}
196+
}
197+
})
198+
});
199+
184200
app.get("/get/:guild/:user", async (req, res) => {
185201
const { guild, user } = req.params;
186202

bot/src/commands.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import client from '.';
44
import { ActionRowBuilder, ButtonBuilder, ButtonStyle, type CommandInteraction, ChannelType, type APIApplicationCommandOption, GuildMember, AttachmentBuilder, ComponentType } from 'discord.js';
55
import { heapStats } from 'bun:jsc';
6-
import { getGuildLeaderboard, makeGETRequest, getRoles, removeRole, addRole, enableUpdates, disableUpdates, getCooldown, setCooldown, getUpdatesChannel, setUpdatesChannel, setXP, setLevel, syncFromBot } from './utils/requestAPI';
6+
import { getGuildLeaderboard, makeGETRequest, getRoles, removeRole, addRole, enableUpdates, disableUpdates, getCooldown, setCooldown, getUpdatesChannel, setUpdatesChannel, setXP, setLevel, syncFromBot, getDBSize } from './utils/requestAPI';
77
import convertToLevels from './utils/convertToLevels';
88
import quickEmbed from './utils/quickEmbed';
99
import { Font, RankCardBuilder } from 'canvacord';
@@ -107,6 +107,7 @@ const commands: Record<string, Command> = {
107107
execute: async (interaction) => {
108108
const heap = heapStats();
109109
Bun.gc(false);
110+
const dbSize = await getDBSize();
110111
await interaction
111112
.reply({
112113
ephemeral: false,
@@ -116,6 +117,7 @@ const commands: Record<string, Command> = {
116117
1024 /
117118
1024
118119
).toFixed(2)} MB (${(heap.extraMemorySize / 1024 / 1024).toFixed(2,)} MB) (${heap.objectCount.toLocaleString()} objects, ${heap.protectedObjectCount.toLocaleString()} protected-objects)`,
120+
`Disk usage: ${dbSize.sizeInMB.toFixed(2)} MB`,
119121
]
120122
.join('\n')
121123
.slice(0, 2000),

bot/src/utils/requestAPI.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ export async function setLevel(guild: string, user: string, level: number) {
101101
return response.status === 200;
102102
}
103103

104+
export async function getDBSize() {
105+
const response = await fetch('http://localhost:18103/get/dbusage')
106+
if (!response.ok) {
107+
console.error(`HTTP error! Status: ${response.status}`);
108+
return null;
109+
}
110+
return response.json();
111+
}
112+
104113
//#region Roles
105114
export async function getRoles(guild: string) {
106115
const response = await fetch(`http://localhost:18103/admin/roles/${guild}/get`, {

bun.lockb

1.01 KB
Binary file not shown.

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)