Skip to content

Commit 48cdee8

Browse files
authored
Merge pull request #256 from Kathund/statsChannels
feat(Stats Channels)
2 parents 2ae16fb + 6b304cf commit 48cdee8

File tree

8 files changed

+121
-1
lines changed

8 files changed

+121
-1
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,45 @@ The `ranks` option is an array that takes in an object like the following exampl
512512
}
513513
```
514514

515+
### Stats Channels
516+
517+
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.
518+
519+
The `enabled` option determines whether the Stats Channels system is enabled. By default, this is set to false.
520+
521+
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.
522+
523+
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.
524+
```json
525+
{
526+
"id": "CHANNEL_ID",
527+
"name": "Guild Level: {guildLevel}"
528+
}
529+
```
530+
531+
<details>
532+
<summary>Channel Name Variables</summary>
533+
534+
`{guildName}` The name of the guild.
535+
536+
`{guildLeve}` The current level of the guild.
537+
538+
`{guildXP}` The **Raw** amount of xp your guild has.
539+
540+
`{guildWeeklyXP}` The amount of xp your guild has gotten in the past week.
541+
542+
`{guildMembers}` The amount of members currently in the guild.
543+
544+
`{discordMembers}` The amount of users in your discord.
545+
546+
`{discordChannels}` The amount of channels in your discord.
547+
548+
`{discordRoles}` The amount of roles in your discord.
549+
550+
</details>
551+
<br>
552+
553+
515554

516555
### Chat Triggers Module
517556

config.example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,5 +120,10 @@
120120
"autoUpdater": true,
121121
"autoUpdaterInterval": 12,
122122
"logToFiles": true
123+
},
124+
"statsChannels": {
125+
"enabled": false,
126+
"autoUpdaterInterval": 5,
127+
"channels": [{ "id": "CHANNEL_ID", "name": "Guild Level: {guildLevel}" }]
123128
}
124129
}

src/contracts/embedHandler.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class SuccessEmbed extends Embed {
6060
/**
6161
* Constructs a new SuccessEmbed instance.
6262
* @param {string} description - The description of the success.
63+
* @param {object} footer - The footer of the success.
6364
*/
6465
constructor(description, footer) {
6566
super();
@@ -70,6 +71,10 @@ class SuccessEmbed extends Embed {
7071
if (footer) this.setFooter(footer);
7172

7273
this.setDescription(description);
74+
75+
if (footer) {
76+
this.setFooter(footer);
77+
}
7378
}
7479
}
7580

src/discord/CommandHandler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class CommandHandler {
88
constructor(discord) {
99
this.discord = discord;
1010

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

1414
for (const file of commandFiles) {
@@ -20,6 +20,8 @@ class CommandHandler {
2020
commands.push(command);
2121
}
2222

23+
commands = commands.filter((command) => !command.channelsCommand);
24+
2325
const rest = new REST({ version: "10" }).setToken(config.discord.bot.token);
2426

2527
const clientID = Buffer.from(config.discord.bot.token.split(".")[0], "base64").toString("ascii");
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const hypixelRebornAPI = require("../../contracts/API/HypixelRebornAPI.js");
2+
const { replaceVariables } = require("../../contracts/helperFunctions.js");
3+
const { SuccessEmbed } = require("../../contracts/embedHandler.js");
4+
const config = require("../../../config.json");
5+
6+
module.exports = {
7+
name: "force-update-channels",
8+
description: "Update the stats Channels",
9+
moderatorOnly: true,
10+
channelsCommand: true,
11+
12+
execute: async (interaction, hidden = false) => {
13+
const hypixelGuild = await hypixelRebornAPI.getGuild("player", bot.username);
14+
const [channels, roles] = await Promise.all([guild.channels.fetch(), guild.roles.fetch()]);
15+
16+
config.statsChannels.channels.forEach(async (channelInfo) => {
17+
const channel = await guild.channels.fetch(channelInfo.id);
18+
channel.setName(
19+
replaceVariables(channelInfo.name, {
20+
guildName: hypixelGuild.name,
21+
guildLevel: hypixelGuild.level,
22+
guildXP: hypixelGuild.experience,
23+
guildWeeklyXP: hypixelGuild.totalWeeklyGexp,
24+
guildMembers: hypixelGuild.members.length,
25+
26+
discordMembers: guild.memberCount,
27+
discordChannels: channels.size,
28+
discordRoles: roles.size,
29+
}),
30+
"Updated Channels",
31+
);
32+
});
33+
34+
if (hidden) return
35+
const embed = new SuccessEmbed("The channels have been updated successfully.", {
36+
text: `by @kathund. | /help [command] for more information`,
37+
iconURL: "https://i.imgur.com/uUuZx2E.png",
38+
});
39+
await interaction.followUp({ embeds: [embed] });
40+
},
41+
};

src/discord/events/interactionCreate.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module.exports = {
3030
throw new HypixelDiscordChatBridgeError("Verification is disabled.");
3131
}
3232

33+
if (command.channelsCommand === true && config.statsChannels.enabled === false) {
34+
throw new HypixelDiscordChatBridgeError("Channel Stats is disabled.");
35+
}
36+
3337
if (command.moderatorOnly === true && isModerator(interaction) === false) {
3438
throw new HypixelDiscordChatBridgeError("You don't have permission to use this command.");
3539
}

src/discord/handlers/StateHandler.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ class StateHandler {
1212
activities: [{ name: `/help | by @duckysolucky` }],
1313
});
1414

15+
global.guild = await client.guilds.fetch(config.discord.bot.serverID);
16+
Logger.discordMessage("Guild ready, successfully fetched " + guild.name);
17+
1518
const channel = await this.getChannel("Guild");
1619
if (channel === undefined) {
1720
return Logger.errorMessage(`Channel "Guild" not found!`);
1821
}
22+
23+
if (config.statsChannels.enabled) require("../other/statsChannels.js");
24+
25+
global.guild = await client.guilds.fetch(config.discord.bot.serverID);
26+
if (guild === undefined) {
27+
return Logger.errorMessage(`Guild not found!`);
28+
}
29+
30+
Logger.discordMessage("Guild ready, successfully fetched " + guild.name);
31+
32+
if (config.verification.autoUpdater) {
33+
require("../other/updateUsers.js");
34+
}
1935

2036
global.guild = await client.guilds.fetch(config.discord.bot.serverID);
2137
if (guild === undefined) {

src/discord/other/statsChannels.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const updateChannels = require("../commands/forceUpdateChannelsCommand.js");
2+
const config = require("../../../config.json");
3+
const cron = require("node-cron");
4+
5+
if (config.statsChannels.enabled) {
6+
console.log("Stats channels enabled");
7+
cron.schedule(`*/${config.verification.autoUpdaterInterval} * * * *`, () => updateChannels.execute(null, true));
8+
}

0 commit comments

Comments
 (0)