如何获取启动文件中的客户端对象?
How to get the client Object in the launchfile?
我有一个 Discord Bot,现在需要分片。我创建了一个名为“botlaunch.js”的文件,我从控制台 (pm2 start botlaunch.js
) 开始。此文件中包含分片所需的所有内容。
这是我的 botlaunch.js
的样子:
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);
});
我需要在这段代码之后访问客户端对象。我用下面的函数试过了,但是这个也不起作用。没有错误或类似的东西,它只是 returns undefined 对于我要求的一切,无关紧要:
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]);
}
关于我如何使用我的 botlaunch.js
代码下的客户端,您有什么解决方案吗?
您可以像这样使用 ShardingManager#broadcastEval()
if you want to obviously eval something, if you need some properties use ShardingManager#fetchClientValues()
:
shardingManager.fetchClientValues('guilds.size')
.then(results => {
console.log(`${results.reduce((prev, guildCount) => prev + guildCount, 0)} total guilds`);
})
.catch(console.error);
如果你想从机器人内部broadcastEval/fetchClientValues,你可以使用client.shard.fetchClientValues()
/client.shard.broadcastEval()
这里还有一个很好的指南:https://discordjs.guide/sharding/#broadcasteval
另一种方法是使用 2 个客户端而不是 ShardingManager,例如:
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.
我有一个 Discord Bot,现在需要分片。我创建了一个名为“botlaunch.js”的文件,我从控制台 (pm2 start botlaunch.js
) 开始。此文件中包含分片所需的所有内容。
这是我的 botlaunch.js
的样子:
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);
});
我需要在这段代码之后访问客户端对象。我用下面的函数试过了,但是这个也不起作用。没有错误或类似的东西,它只是 returns undefined 对于我要求的一切,无关紧要:
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]);
}
关于我如何使用我的 botlaunch.js
代码下的客户端,您有什么解决方案吗?
您可以像这样使用 ShardingManager#broadcastEval()
if you want to obviously eval something, if you need some properties use ShardingManager#fetchClientValues()
:
shardingManager.fetchClientValues('guilds.size')
.then(results => {
console.log(`${results.reduce((prev, guildCount) => prev + guildCount, 0)} total guilds`);
})
.catch(console.error);
如果你想从机器人内部broadcastEval/fetchClientValues,你可以使用client.shard.fetchClientValues()
/client.shard.broadcastEval()
这里还有一个很好的指南:https://discordjs.guide/sharding/#broadcasteval
另一种方法是使用 2 个客户端而不是 ShardingManager,例如:
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.