Skip to content

Commit

Permalink
feat(channel-stats): support for userWithRole parameters (#78)
Browse files Browse the repository at this point in the history
* feat(channel-stats): no warning on category channels

* fix(channel-stats): missing support for reloading configuration

* feat(channel-stats): support for userWithRole parameters

* fix(channel-stats): added missing jsdoc commit
  • Loading branch information
SCDerox authored Oct 7, 2022
1 parent 4603ac1 commit 0ab64ed
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
20 changes: 16 additions & 4 deletions modules/channel-stats/channels.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@
{
"name": "%currentTime%",
"description": "Current time and date"
},
{
"name": "%userWithRoleCount-<ID>%",
"description": "Count of members with a specific role (replace \"<ID>\" with an actual role-id)"
},
{
"name": "%onlineUserWithRoleCount-<ID>%",
"description": "Count of members with a specific role who are online (replace \"<ID>\" with an actual role-id)"
}
],
"humanname-de": "Kanalname",
Expand All @@ -98,10 +106,6 @@
},
{
"name": "%onlineUserCount%",
"description": "Anzahl an Nutzern auf dem Server, welche Online sind (Bitte nicht stören oder Online Status)"
},
{
"name": "%onlineMemberCount%",
"description": "Anzahl an Mitgliedern (keine Bots) auf dem Server, welche Online sind (Bitte nicht stören oder Online Status)"
},
{
Expand Down Expand Up @@ -151,6 +155,14 @@
{
"name": "%currentTime%",
"description": "Aktuelles Datum und Uhrzeit"
},
{
"name": "%userWithRoleCount-<ID>%",
"description": "Anzahl von Nutzern mit einer bestimmen Rolle (bitte \"<ID>\" mit einer echten Rollen-ID ersetzen)"
},
{
"name": "%onlineUserWithRoleCount-<ID>%",
"description": "Anzahl von Nutzern mit einer bestimmen Rolle, die online sind (bitte \"<ID>\" mit einer echten Rollen-ID ersetzen)"
}
],
"default-de": ""
Expand Down
27 changes: 25 additions & 2 deletions modules/channel-stats/events/botReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ const {formatDate} = require('../../../src/functions/helpers');
const {localize} = require('../../../src/functions/localize');

module.exports.run = async (client) => {
const channels = require(`${client.configDir}/channel-stats/channels.json`);
const channels = client.configurations['channel-stats']['channels'];
for (const channel of channels) {
const dcChannel = await client.channels.fetch(channel.channelID).catch(() => {
});
if (!dcChannel) continue;
if (dcChannel.type !== 'GUILD_VOICE') client.logger.warn(`[channel-stats] ` + localize('channel-stats', 'not-voice-channel-info', {
if (dcChannel.type !== 'GUILD_VOICE' && dcChannel.type !== 'GUILD_CATEGORY') client.logger.warn(`[channel-stats] ` + localize('channel-stats', 'not-voice-channel-info', {
c: dcChannel.name,
id: dcChannel.id,
t: dcChannel.type
Expand All @@ -32,6 +32,29 @@ module.exports.run = async (client) => {
async function channelNameReplacer(client, channel, input) {
const users = await channel.guild.members.fetch();
const members = users.filter(u => !u.user.bot);

/**
* Replaces the first member-with-role-count parameters of the input
* @private
*/
function replaceFirst() {
if (input.includes('%userWithRoleCount-')) {
const id = input.split('%userWithRoleCount-')[1].split('%')[0];
if (input.includes(`%userWithRoleCount-${id}%`)) {
input = input.replaceAll(`%userWithRoleCount-${id}%`, users.filter(f => f.roles.cache.has(id)).size.toString());
replaceFirst();
}
}
if (input.includes('%onlineUserWithRoleCount-')) {
const id = input.split('%onlineUserWithRoleCount-')[1].split('%')[0];
if (input.includes(`%onlineUserWithRoleCount-${id}%`)) {
input = input.replaceAll(`%onlineUserWithRoleCount-${id}%`, users.filter(f => f.roles.cache.has(id) && f.presence && (f.presence || {}).status !== 'offline').size.toString());
replaceFirst();
}
}
}

replaceFirst();
return input.split('%userCount%').join(users.size)
.split('%memberCount%').join(members.size)
.split('%onlineUserCount%').join(users.filter(u => u.presence && (u.presence || {}).status !== 'offline').size)
Expand Down

0 comments on commit 0ab64ed

Please sign in to comment.