Skip to content

Commit

Permalink
Refactor voice activity update logic for better performance
Browse files Browse the repository at this point in the history
  • Loading branch information
chimpdev committed Jun 23, 2024
1 parent a421310 commit 2bd4d2b
Showing 1 changed file with 42 additions and 26 deletions.
68 changes: 42 additions & 26 deletions server/src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,34 +182,50 @@ module.exports = class Client {
}

async checkVoiceActivity() {
const servers = await Server.find({ voice_activity_enabled: true });
for (const server of servers) {
const guild = client.guilds.cache.get(server.id);
if (!guild) continue;

const totalMembersInVoice = guild.members.cache.filter(member => member.voice.channel).size;
const voiceActivity = await VoiceActivity.findOne({ 'guild.id': server.id });

if (voiceActivity) {
const data = voiceActivity.data;
if (data.length === 24) data.shift();

data.push({
createdAt: new Date(),
data: totalMembersInVoice
});
await voiceActivity.updateOne({ $set: { data: data } });
} else await new VoiceActivity({
guild: { id: server.id },
data: [{
createdAt: new Date(),
data: totalMembersInVoice
}]
}).save();

logger.info(`Voice activity for server ${server.id} updated.`);
try {
const servers = await Server.find({ voice_activity_enabled: true });
await Promise.all(servers.map(async server => {
const guild = client.guilds.cache.get(server.id);
if (!guild) return;

const totalMembersInVoice = guild.members.cache.filter(member => member.voice.channel).size;
const voiceActivity = await VoiceActivity.findOneAndUpdate(
{ 'guild.id': server.id },
{
$push: {
data: {
$each: [
{
createdAt: new Date(),
data: totalMembersInVoice
}
],
$slice: -24
}
}
},
{ new: true, upsert: true }
);

if (!voiceActivity) {
await new VoiceActivity({
guild: { id: server.id },
data: [
{
createdAt: new Date(),
data: totalMembersInVoice
}
]
}).save();
}

logger.info(`Voice activity for server ${server.id} updated.`);
}));
} catch (error) {
logger.error('Error checking voice activity:', error);
}
}


async updatePanelMessages() {
const panels = await Panel.find();
Expand Down

0 comments on commit 2bd4d2b

Please sign in to comment.