Skip to content
This repository has been archived by the owner on Sep 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #120 from mariusbegby/load-test-functionality
Browse files Browse the repository at this point in the history
 Added options for load testing to automatically play music in specified channels
  • Loading branch information
mariusbegby authored Jul 25, 2023
2 parents 771429b + 8e04d59 commit a916e93
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 3 deletions.
7 changes: 7 additions & 0 deletions src/config_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,10 @@ module.exports.ffmpegFilterOptions = {
}
]
};

// Options for load testing the bot. If enabled, the bot will join the specified channels and play specified track source.
module.exports.loadTestOptions = {
enabled: false,
trackUrl: 'https://www.youtube.com/watch?v=tTR4D9h3zAE',
channelIdsToJoin: []
};
7 changes: 6 additions & 1 deletion src/events/client/ready.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const logger = require('../../services/logger');
const { embedOptions, systemOptions, presenceStatusOptions } = require('../../config');
const { embedOptions, systemOptions, presenceStatusOptions, loadTestOptions } = require('../../config');
const { postBotStats } = require('../../utils/other/postBotStats.js');
const { startLoadTest } = require('../../utils/other/startLoadTest');
const { Events, EmbedBuilder } = require('discord.js');

module.exports = {
Expand All @@ -12,6 +13,10 @@ module.exports = {
await client.user.setPresence(presenceStatusOptions);
await client.user.setPresence(presenceStatusOptions);

if (loadTestOptions.enabled) {
await startLoadTest(client);
}

const channel = await client.channels.cache.get(systemOptions.systemMessageChannelId);

// Check if the channel exists in curent shard and send a message
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const manager = new ShardingManager('./src/bot.js', {

const readyShards = new Set();
manager.on('shardCreate', (shard) => {
logger.debug(`[Shard ${shard.id}] Launched shard with id ${shard.id}.`);
logger.info(`[Shard ${shard.id}] Launched shard with id ${shard.id}.`);

// When all shards are ready, emit event 'allShardsReady' to all shards
shard.on(ShardEvents.Ready, () => {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/factory/createPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ exports.createPlayer = async (client) => {
});

await player.extractors.loadDefault();
logger.debug(`[Shard ${client.shard.ids[0]}] discord-player loaded dependencies:\n${player.scanDeps()}`);
logger.trace(`[Shard ${client.shard.ids[0]}] discord-player loaded dependencies:\n${player.scanDeps()}`);

return player;
} catch (error) {
Expand Down
35 changes: 35 additions & 0 deletions src/utils/other/startLoadTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const logger = require('../../services/logger');
const { loadTestOptions } = require('../../config');

exports.startLoadTest = async (client) => {
if (!loadTestOptions.enabled) {
return;
}

const channelIds = loadTestOptions.channelIdsToJoin;
const track = loadTestOptions.trackUrl;
logger.info(`Starting load test in ${channelIds.length} channels.`);

channelIds.forEach((each) => {
client.shard.broadcastEval(
async (shardClient, { channelId, track }) => {
const channel = shardClient.channels.cache.get(channelId);
if (channel) {
/* eslint-disable no-undef */
await player.play(channel.id, track, {
nodeOptions: {
leaveOnEmpty: false,
leaveOnEnd: false,
leaveOnStop: false,
metadata: {
channel: channel,
client: shardClient
}
}
});
}
},
{ context: { channelId: each, track: track } }
);
});
};

0 comments on commit a916e93

Please # to comment.