From f18f5fff1fd0b8336df464d6e6f62efbc29aa618 Mon Sep 17 00:00:00 2001 From: Andrew Lee Date: Sat, 29 Mar 2025 11:42:40 -0400 Subject: Added analytics; try/catch for ready event --- bot/src/events/ClientReady.js | 48 ++++++++++++++++++++----------------- bot/src/events/GuildMemberUpdate.js | 2 +- bot/src/events/InteractionCreate.js | 3 +++ bot/src/models/command-usages.js | 26 ++++++++++++++++++++ bot/src/plugins/analytics.js | 17 +++++++++++++ bot/src/storage/consts.js | 1 + bot/src/utils/sync.js | 5 ++++ 7 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 bot/src/models/command-usages.js create mode 100644 bot/src/plugins/analytics.js (limited to 'bot/src') diff --git a/bot/src/events/ClientReady.js b/bot/src/events/ClientReady.js index eeb8ab0..9669287 100644 --- a/bot/src/events/ClientReady.js +++ b/bot/src/events/ClientReady.js @@ -28,28 +28,32 @@ export default { console.log(`[i] Bot ID: ${client.user.id}`); console.log(`[i] Running version ${abVersion} | Serving in ${client.guilds.cache.size} guilds`); - await botActivity(client); - await QuoteOfTheDay(client); - - if (process.env.NODE_ENV !== 'development') { - const readyEmbed = new EmbedBuilder() - .setAuthor({ name: 'AleeBot Status', iconURL: client.user.avatarURL() }) - .setDescription('AleeBot has started') - .addFields( - { name: 'Version', value: `${abVersion}`, inline: true }, - { name: 'Node.JS Version', value: `${process.versions.node}`, inline: true }, - { name: 'Discord.JS Version', value: `${version}`, inline: true } - ) - .setColor(abEmbedColour); - - let statusChannel = client.channels.cache.get(process.env.STATUS_CHANNEL_ID); - if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); - await statusChannel.send({ embeds: [readyEmbed] }); + try { + await botActivity(client); + await QuoteOfTheDay(client); + + if (process.env.NODE_ENV !== 'development') { + const readyEmbed = new EmbedBuilder() + .setAuthor({name: 'AleeBot Status', iconURL: client.user.avatarURL()}) + .setDescription('AleeBot has started') + .addFields( + {name: 'Version', value: `${abVersion}`, inline: true}, + {name: 'Node.JS Version', value: `${process.versions.node}`, inline: true}, + {name: 'Discord.JS Version', value: `${version}`, inline: true} + ) + .setColor(abEmbedColour); + + let statusChannel = client.channels.cache.get(process.env.STATUS_CHANNEL_ID); + if (!statusChannel) return console.error('The status channel does not exist! Skipping.'); + await statusChannel.send({embeds: [readyEmbed]}); + } + + setInterval(function () { + botActivity(client); + }, 200000); + + } catch (e) { + console.error(e); } - - setInterval(function() { - botActivity(client); - }, 200000); - } }; diff --git a/bot/src/events/GuildMemberUpdate.js b/bot/src/events/GuildMemberUpdate.js index 3c90268..1fec4fe 100644 --- a/bot/src/events/GuildMemberUpdate.js +++ b/bot/src/events/GuildMemberUpdate.js @@ -24,7 +24,7 @@ export default { await guildMember.send({ embeds: [logEmbed] }); } catch (e) { - console.error(e); + console.error(e); } } }; diff --git a/bot/src/events/InteractionCreate.js b/bot/src/events/InteractionCreate.js index ac979d2..777ce39 100644 --- a/bot/src/events/InteractionCreate.js +++ b/bot/src/events/InteractionCreate.js @@ -1,4 +1,5 @@ import { Events, MessageFlags } from 'discord.js'; +import { Analytics } from '../plugins/analytics.js'; function error(e) { return `Something went wrong. [Submit an issue at the AleeBot repository.]()\nMessage:\n\`\`\`js\n${e.stack}\`\`\``; @@ -14,6 +15,8 @@ export default { if (!command) return; try { + console.log(`[i] ${interaction.user.username} has executed ${command.data.name}`); + await Analytics(command, interaction); await command.execute(interaction); } catch (e) { console.error(e); diff --git a/bot/src/models/command-usages.js b/bot/src/models/command-usages.js new file mode 100644 index 0000000..a9dafa4 --- /dev/null +++ b/bot/src/models/command-usages.js @@ -0,0 +1,26 @@ +import { INTEGER, STRING } from 'sequelize'; +import { sequelize } from '../utils/sequelize.js'; + +export const commandUsages = sequelize.define('command-usages', { + id: { + type: INTEGER, + autoIncrement: true, + primaryKey: true + }, + command: { + type: STRING, + allowNull: false + }, + userID: { + type: STRING, + allowNull: false + }, + guildID: { + type: STRING, + allowNull: true + } + +}, { + updatedAt: false, +}); + diff --git a/bot/src/plugins/analytics.js b/bot/src/plugins/analytics.js new file mode 100644 index 0000000..137207f --- /dev/null +++ b/bot/src/plugins/analytics.js @@ -0,0 +1,17 @@ +import { commandUsages } from '../models/command-usages.js'; +import { enableAnalytics } from '../storage/consts.js'; + +export async function Analytics(command, interaction) { + if (enableAnalytics) { + if (!interaction.guild) return await commandUsages.create({ + command: command.data.name, + userID: interaction.user.id + }); + + return await commandUsages.create({ + command: command.data.name, + userID: interaction.user.id, + guildID: interaction.guild.id + }); + } +} diff --git a/bot/src/storage/consts.js b/bot/src/storage/consts.js index b62d218..71a77ab 100644 --- a/bot/src/storage/consts.js +++ b/bot/src/storage/consts.js @@ -1,5 +1,6 @@ export const abEmbedColour = '#0066a6'; export const ollamaGlobal = true; +export const enableAnalytics = true; export const featureSuggestChannel = '427495678390960148'; export const userWhitelist = ['242775871059001344']; diff --git a/bot/src/utils/sync.js b/bot/src/utils/sync.js index fbf6c2e..fa3d8c9 100644 --- a/bot/src/utils/sync.js +++ b/bot/src/utils/sync.js @@ -1,5 +1,6 @@ import { quote, pendingQuote } from '../models/quote.js'; import { guildSettings } from '../models/guild-settings.js'; +import { commandUsages } from '../models/command-usages.js'; export function syncDB() { quote.sync().then(() => { @@ -13,4 +14,8 @@ export function syncDB() { guildSettings.sync().then(() => { console.log('[>] Guild database synced!'); }); + + commandUsages.sync().then(() => { + console.log('[>] Command usage database synced!'); + }); } -- cgit v1.2.3