How To Get The Client Object In The Launchfile?
I have a Discord Bot which needs sharding now. I created a file named "botlaunch.js", which I start from the console (pm2 start botlaunch.js
). In this file is all stuff you need for sharding.
Here is how my botlaunch.js
looks like:
const Discord = require('discord.js');
const settings = require('./settings.json');
const chalk = require('chalk');
const shardingManager = new Discord.ShardingManager('./lenoxbot.js',
{
token: settings.token
});
shardingManager.spawn('auto', 500).then(() => {
console.log(chalk.green(`[ShardManager] Started ${shardingManager.totalShards} shards`));
}).catch(error => {
console.log(error);
});
I need to access the client object after this code. I tried it with the following function but this doesn't work either. No errors or something like this, it only returns undefined for everything I request, doesn't matter what:
function exec(script) {
let requestId = 0;
const currentRequestId = requestId++;
process.send({ cmd: 'exec', script: script, reqId: currentRequestId });
const promiseExec = new Promise(resolve => {
_promiseQueue[currentRequestId] = resolve;
});
const promiseTimer = new Promise((resolve, reject) => {
setTimeout(() => {
reject('Promise timed out before completion @ LenoxBotLauncher/exec');
}, 60 * 1000);
_promiseQueue.delete(currentRequestId);
});
return Promise.race([promiseExec, promiseTimer]);
}
Do you have any solutions on how I can use the client under the code of my botlaunch.js
?
Answer
You could use ShardingManager#broadcastEval()
if you want to obviously eval something, if you need some properties use ShardingManager#fetchClientValues()
like:
shardingManager.fetchClientValues('guilds.size')
.then(results => {
console.log(`${results.reduce((prev, guildCount) => prev + guildCount, 0)} total guilds`);
})
.catch(console.error);
If you want to broadcastEval/fetchClientValues from inside the Bot you can use client.shard.fetchClientValues()
/client.shard.broadcastEval()
There is also a nice guide about it here: https://discordjs.guide/sharding/#broadcasteval
Another way would be to use 2 Clients instead of the ShardingManager like:
const client1 = new Discord.Client({ shardId: 0, shardCount: 2});
const client2 = new Discord.Client({ shardId: 1, shardCount: 2});
This would mean that the bot will run in 1 process only and this might cause performance issues.
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM