Skip to content

Commit

Permalink
Merge pull request #256 from Kathund/statsChannels
Browse files Browse the repository at this point in the history
feat(Stats Channels)
  • Loading branch information
Killermaschine88 authored Dec 28, 2024
2 parents 2ae16fb + 6b304cf commit 48cdee8
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 1 deletion.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,45 @@ The `ranks` option is an array that takes in an object like the following exampl
}
```

### Stats Channels

The Stats Channels are channels that show stats for your guild. These stats can be your guild level, amount of members in the guild and other.

The `enabled` option determines whether the Stats Channels system is enabled. By default, this is set to false.

The `autoUpdaterInterval` allows you to change how often the autoUpdater for the channels runs. By default this option is set to 5 making the autoUpdater run every 5 minutes.

The `channels` option is an array that takes in an object like the following example, this allows the updater to know what channels there are and what to name them.
```json
{
"id": "CHANNEL_ID",
"name": "Guild Level: {guildLevel}"
}
```

<details>
<summary>Channel Name Variables</summary>

`{guildName}` The name of the guild.

`{guildLeve}` The current level of the guild.

`{guildXP}` The **Raw** amount of xp your guild has.

`{guildWeeklyXP}` The amount of xp your guild has gotten in the past week.

`{guildMembers}` The amount of members currently in the guild.

`{discordMembers}` The amount of users in your discord.

`{discordChannels}` The amount of channels in your discord.

`{discordRoles}` The amount of roles in your discord.

</details>
<br>



### Chat Triggers Module

Expand Down
5 changes: 5 additions & 0 deletions config.example.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,10 @@
"autoUpdater": true,
"autoUpdaterInterval": 12,
"logToFiles": true
},
"statsChannels": {
"enabled": false,
"autoUpdaterInterval": 5,
"channels": [{ "id": "CHANNEL_ID", "name": "Guild Level: {guildLevel}" }]
}
}
5 changes: 5 additions & 0 deletions src/contracts/embedHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class SuccessEmbed extends Embed {
/**
* Constructs a new SuccessEmbed instance.
* @param {string} description - The description of the success.
* @param {object} footer - The footer of the success.
*/
constructor(description, footer) {
super();
Expand All @@ -70,6 +71,10 @@ class SuccessEmbed extends Embed {
if (footer) this.setFooter(footer);

this.setDescription(description);

if (footer) {
this.setFooter(footer);
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/discord/CommandHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class CommandHandler {
constructor(discord) {
this.discord = discord;

const commands = [];
let commands = [];
const commandFiles = fs.readdirSync("src/discord/commands").filter((file) => file.endsWith(".js"));

for (const file of commandFiles) {
Expand All @@ -20,6 +20,8 @@ class CommandHandler {
commands.push(command);
}

commands = commands.filter((command) => !command.channelsCommand);

const rest = new REST({ version: "10" }).setToken(config.discord.bot.token);

const clientID = Buffer.from(config.discord.bot.token.split(".")[0], "base64").toString("ascii");
Expand Down
41 changes: 41 additions & 0 deletions src/discord/commands/forceUpdateChannelsCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const hypixelRebornAPI = require("../../contracts/API/HypixelRebornAPI.js");
const { replaceVariables } = require("../../contracts/helperFunctions.js");
const { SuccessEmbed } = require("../../contracts/embedHandler.js");
const config = require("../../../config.json");

module.exports = {
name: "force-update-channels",
description: "Update the stats Channels",
moderatorOnly: true,
channelsCommand: true,

execute: async (interaction, hidden = false) => {
const hypixelGuild = await hypixelRebornAPI.getGuild("player", bot.username);
const [channels, roles] = await Promise.all([guild.channels.fetch(), guild.roles.fetch()]);

config.statsChannels.channels.forEach(async (channelInfo) => {
const channel = await guild.channels.fetch(channelInfo.id);
channel.setName(
replaceVariables(channelInfo.name, {
guildName: hypixelGuild.name,
guildLevel: hypixelGuild.level,
guildXP: hypixelGuild.experience,
guildWeeklyXP: hypixelGuild.totalWeeklyGexp,
guildMembers: hypixelGuild.members.length,

discordMembers: guild.memberCount,
discordChannels: channels.size,
discordRoles: roles.size,
}),
"Updated Channels",
);
});

if (hidden) return
const embed = new SuccessEmbed("The channels have been updated successfully.", {
text: `by @kathund. | /help [command] for more information`,
iconURL: "https://i.imgur.com/uUuZx2E.png",
});
await interaction.followUp({ embeds: [embed] });
},
};
4 changes: 4 additions & 0 deletions src/discord/events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ module.exports = {
throw new HypixelDiscordChatBridgeError("Verification is disabled.");
}

if (command.channelsCommand === true && config.statsChannels.enabled === false) {
throw new HypixelDiscordChatBridgeError("Channel Stats is disabled.");
}

if (command.moderatorOnly === true && isModerator(interaction) === false) {
throw new HypixelDiscordChatBridgeError("You don't have permission to use this command.");
}
Expand Down
16 changes: 16 additions & 0 deletions src/discord/handlers/StateHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,26 @@ class StateHandler {
activities: [{ name: `/help | by @duckysolucky` }],
});

global.guild = await client.guilds.fetch(config.discord.bot.serverID);
Logger.discordMessage("Guild ready, successfully fetched " + guild.name);

const channel = await this.getChannel("Guild");
if (channel === undefined) {
return Logger.errorMessage(`Channel "Guild" not found!`);
}

if (config.statsChannels.enabled) require("../other/statsChannels.js");

global.guild = await client.guilds.fetch(config.discord.bot.serverID);
if (guild === undefined) {
return Logger.errorMessage(`Guild not found!`);
}

Logger.discordMessage("Guild ready, successfully fetched " + guild.name);

if (config.verification.autoUpdater) {
require("../other/updateUsers.js");
}

global.guild = await client.guilds.fetch(config.discord.bot.serverID);
if (guild === undefined) {
Expand Down
8 changes: 8 additions & 0 deletions src/discord/other/statsChannels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const updateChannels = require("../commands/forceUpdateChannelsCommand.js");
const config = require("../../../config.json");
const cron = require("node-cron");

if (config.statsChannels.enabled) {
console.log("Stats channels enabled");
cron.schedule(`*/${config.verification.autoUpdaterInterval} * * * *`, () => updateChannels.execute(null, true));
}

0 comments on commit 48cdee8

Please sign in to comment.