mirror of
https://github.com/Burn-E99/TheArtificer.git
synced 2026-06-04 09:03:50 -04:00
Added Audit system, Guild auditing put on hold
This commit is contained in:
@@ -27,6 +27,7 @@ export const generateStats = (guildCount: number, channelCount: number, memberCo
|
||||
embeds: [{
|
||||
color: infoColor2,
|
||||
title: 'The Artificer\'s Statistics:',
|
||||
timestamp: new Date().toISOString(),
|
||||
fields: [
|
||||
{
|
||||
name: 'Guilds:',
|
||||
|
||||
@@ -11,6 +11,7 @@ import { api } from './apiCmd.ts';
|
||||
import { emoji } from './emoji.ts';
|
||||
import { roll } from './roll.ts';
|
||||
import { handleMentions } from './handleMentions.ts';
|
||||
import { audit } from './audit.ts';
|
||||
|
||||
export default {
|
||||
ping,
|
||||
@@ -26,4 +27,5 @@ export default {
|
||||
emoji,
|
||||
roll,
|
||||
handleMentions,
|
||||
audit,
|
||||
};
|
||||
|
||||
54
src/commands/audit.ts
Normal file
54
src/commands/audit.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import config from '../../config.ts';
|
||||
import { dbClient } from '../db.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
DiscordenoMessage,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
} from '../../deps.ts';
|
||||
import auditCommands from './auditCmd/_index.ts';
|
||||
import { failColor } from '../commandUtils.ts';
|
||||
|
||||
export const audit = async (message: DiscordenoMessage, args: string[]) => {
|
||||
// Light telemetry to see how many times a command is being run
|
||||
dbClient.execute(`CALL INC_CNT("audit");`).catch((e) => {
|
||||
log(LT.ERROR, `Failed to call stored procedure INC_CNT: ${JSON.stringify(e)}`);
|
||||
});
|
||||
|
||||
// Local apiArg in lowercase
|
||||
const auditArg = (args[0] || 'help').toLowerCase();
|
||||
|
||||
// Makes sure the user is authenticated to run the API command
|
||||
if (message.authorId === config.api.admin) {
|
||||
switch (auditArg) {
|
||||
case 'help':
|
||||
case 'h':
|
||||
// [[audit help or [[audit h
|
||||
// Shows API help details
|
||||
auditCommands.auditHelp(message);
|
||||
break;
|
||||
case 'db':
|
||||
// [[audit db
|
||||
// Shows current DB table sizes
|
||||
auditCommands.auditDB(message);
|
||||
break;
|
||||
case 'guilds':
|
||||
// [[audit guilds
|
||||
// Shows breakdown of guilds and detials on them
|
||||
auditCommands.auditGuilds(message);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
message.send({
|
||||
embeds: [{
|
||||
color: failColor,
|
||||
title: `Audit commands are powerful and can only be used by ${config.name}'s owner.`,
|
||||
}],
|
||||
}).catch((e) => {
|
||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
|
||||
});
|
||||
}
|
||||
};
|
||||
9
src/commands/auditCmd/_index.ts
Normal file
9
src/commands/auditCmd/_index.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { auditHelp } from './auditHelp.ts';
|
||||
import { auditDB } from './auditDB.ts';
|
||||
import { auditGuilds } from './auditGuilds.ts';
|
||||
|
||||
export default {
|
||||
auditHelp,
|
||||
auditDB,
|
||||
auditGuilds,
|
||||
};
|
||||
48
src/commands/auditCmd/auditDB.ts
Normal file
48
src/commands/auditCmd/auditDB.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { dbClient } from '../../db.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
DiscordenoMessage,
|
||||
EmbedField,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
} from '../../../deps.ts';
|
||||
import { infoColor2 } from '../../commandUtils.ts';
|
||||
import { compilingStats } from '../../commonEmbeds.ts';
|
||||
|
||||
export const auditDB = async (message: DiscordenoMessage) => {
|
||||
try {
|
||||
const m = await message.send(compilingStats);
|
||||
|
||||
// Get DB statistics
|
||||
const auditQuery = await dbClient.query(`SELECT * FROM db_size;`).catch((e) => {
|
||||
log(LT.ERROR, `Failed to query DB: ${JSON.stringify(e)}`);
|
||||
});
|
||||
|
||||
// Turn all tables into embed fields, currently only properly will handle 25 tables, but we'll fix that when artificer gets 26 tables
|
||||
const embedFields: Array<EmbedField> = [];
|
||||
auditQuery.forEach((row: any) => {
|
||||
embedFields.push({
|
||||
name: `${row.table}`,
|
||||
value: `**Size:** ${row.size} MB
|
||||
**Rows:** ${row.rows}`,
|
||||
inline: true,
|
||||
});
|
||||
});
|
||||
|
||||
// Send the results
|
||||
m.edit({
|
||||
embeds: [{
|
||||
color: infoColor2,
|
||||
title: 'Database Audit',
|
||||
description: 'Lists all tables with their current size and row count.',
|
||||
timestamp: new Date().toISOString(),
|
||||
fields: embedFields,
|
||||
}],
|
||||
}).catch((e) => {
|
||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
|
||||
});
|
||||
} catch (e) {
|
||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
|
||||
}
|
||||
};
|
||||
21
src/commands/auditCmd/auditGuilds.ts
Normal file
21
src/commands/auditCmd/auditGuilds.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import {
|
||||
// Discordeno deps
|
||||
DiscordenoMessage,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
} from '../../../deps.ts';
|
||||
import { infoColor2 } from '../../commandUtils.ts';
|
||||
|
||||
export const auditGuilds = (message: DiscordenoMessage) => {
|
||||
message.send({
|
||||
embeds: [{
|
||||
color: infoColor2,
|
||||
title: 'Guilds Audit',
|
||||
description: 'WIP',
|
||||
timestamp: new Date().toISOString(),
|
||||
}],
|
||||
}).catch((e) => {
|
||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
|
||||
});
|
||||
};
|
||||
35
src/commands/auditCmd/auditHelp.ts
Normal file
35
src/commands/auditCmd/auditHelp.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import config from '../../../config.ts';
|
||||
import {
|
||||
// Discordeno deps
|
||||
DiscordenoMessage,
|
||||
// Log4Deno deps
|
||||
log,
|
||||
LT,
|
||||
} from '../../../deps.ts';
|
||||
import { infoColor1 } from '../../commandUtils.ts';
|
||||
|
||||
export const auditHelp = (message: DiscordenoMessage) => {
|
||||
message.send({
|
||||
embeds: [{
|
||||
color: infoColor1,
|
||||
title: 'Audit Help',
|
||||
fields: [
|
||||
{
|
||||
name: `\`${config.prefix}audit help\``,
|
||||
value: 'This command',
|
||||
inline: true
|
||||
}, {
|
||||
name: `\`${config.prefix}audit db\``,
|
||||
value: 'Shows current DB table sizes',
|
||||
inline: true
|
||||
}, {
|
||||
name: `\`${config.prefix}audit guilds\``,
|
||||
value: 'Shows breakdown of guilds and detials on them',
|
||||
inline: true
|
||||
},
|
||||
]
|
||||
}],
|
||||
}).catch((e) => {
|
||||
log(LT.ERROR, `Failed to send message: ${JSON.stringify(message)} | ${JSON.stringify(e)}`);
|
||||
});
|
||||
};
|
||||
@@ -8,7 +8,8 @@ import {
|
||||
log,
|
||||
LT,
|
||||
} from '../../deps.ts';
|
||||
import { generateStats, warnColor } from '../commandUtils.ts';
|
||||
import { generateStats } from '../commandUtils.ts';
|
||||
import { compilingStats } from '../commonEmbeds.ts';
|
||||
|
||||
export const stats = async (message: DiscordenoMessage) => {
|
||||
// Light telemetry to see how many times a command is being run
|
||||
@@ -17,12 +18,7 @@ export const stats = async (message: DiscordenoMessage) => {
|
||||
});
|
||||
|
||||
try {
|
||||
const m = await message.send({
|
||||
embeds: [{
|
||||
color: warnColor,
|
||||
title: 'Compiling latest statistics . . .',
|
||||
}],
|
||||
});
|
||||
const m = await message.send(compilingStats);
|
||||
|
||||
// Calculate how many times commands have been run
|
||||
const rollQuery = await dbClient.query(`SELECT count FROM command_cnt WHERE command = "roll";`).catch((e) => {
|
||||
|
||||
8
src/commonEmbeds.ts
Normal file
8
src/commonEmbeds.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { warnColor } from './commandUtils.ts';
|
||||
|
||||
export const compilingStats = {
|
||||
embeds: [{
|
||||
color: warnColor,
|
||||
title: 'Compiling latest statistics . . .',
|
||||
}],
|
||||
};
|
||||
Reference in New Issue
Block a user