3 回答
TA贡献2021条经验 获得超8个赞
与其依赖间隔,不如使用guildCreate
和guildDelete
事件。每当机器人加入公会或从公会中移除时,它们就会分别被触发。看看下面的示例代码。
client.on("ready", () => {
client.user.setActivity("Serving " + client.guilds.cache.size + " servers");
});
client.on("guildCreate", () => {
// Fired every time the bot is added to a new server
client.user.setActivity("Serving "+ client.guilds.cache.size +" servers");
});
client.on("guildDelete", () => {
// Fired every time the bot is removed from a server
client.user.setActivity("Serving "+ client.guilds.cache.size +" servers");
});
现在,如果您想将其与选择随机状态配对,您也可以执行以下操作:
const statusMessages = ['First status messages', 'Serving {guildSize} servers', 'Third possible message'];
let chosenMessageIndex = 0;
client.on("ready", () => {
setInterval(() => {
setRandomStatus();
}, 15000);
setRandomStatus();
});
client.on("guildCreate", () => {
// Fired every time the bot is added to a new server
updateStatus();
});
client.on("guildDelete", () => {
// Fired every time the bot is removed from a server
updateStatus();
});
function setRandomStatus() {
chosenMessageIndex = Math.floor(Math.random() * statusMessages.length);
// Set the random status message. If "guildSize" is in the status,
// replace it with the actual number of servers the bot is in
let statusMessage = statusMessages[chosenMessageIndex].replaceAll('{guildSize}', client.guilds.cache.size);
client.user.setActivity(statusMessage);
}
function updateStatus() {
// Check if the displayed status contains the number of servers joined.
// If so, the status needs to be updated.
if (statusMessages[chosenMessageIndex].includes('{guildSize}') {
let statusMessage = statusMessages[chosenMessageIndex].replaceAll('{guildSize}', client.guilds.cache.size);
client.user.setActivity(statusMessage);
}
}
TA贡献1946条经验 获得超4个赞
你的问题在这里:
var index = Math.floor(Math.random() * (status_list.length - 1) + 1);
这将每次都声明相同的,这就是它不更新的原因
每次将我的机器人添加到新服务器时,我都希望它刷新状态。
这就是您(无意)错过的。你想要做的是,正如你已经做过的那样,使用setInterval(). 无论数字是否更改,这都会每隔一段时间刷新一次状态。您不必确保它已更改,因为它对您的实例的负载很小。
现在,尝试遵循以下代码:
bot.on('ready', async () => { // async is recommended as discord.js generally uses async/await.
// Logs in the console that the bot is ready.
console.log(`${bot.user.username} is now ready!`);
// Rather than using an array, which is generally harder to use, manually set the variables instead.
const status = `stuff on ${bot.guilds.cache.size} servers.`
// Set the Interval of Refresh.
setInterval(async () => { // Again, async is recommended, though does not do anything for our current purpose.
// Set the activity inside of `setInterval()`.
bot.user.setActivity(status, { type: "LISTENING" });
}, 15000) // Refreshes every 15000 miliseconds, or 15 seconds.
});
这应该可以满足您的需要,但正如我从您的代码中看到的那样,您试图随机状态?如果是这样,请改为执行以下操作:
bot.on('ready', async () => {
console.log(`${bot.user.username} is now ready!`);
// In this one, you will need to make an array. Add as many as you want.
const status_list = [`stuff on ${bot.guilds.cache.size} servers.`, `stuff on ${bot.channels.cache.size} channels.`];
// Now randomize a single status from the status_list. With this, you have singled out a random status.
const status = Math.floor(Math.random() * status_list.length);
setInterval(async () => {
bot.user.setActivity(status, { type: "LISTENING" });
}, 15000)
});
现在,如果你想要一个不断变化的(不是随机的)状态,你可以使用下面的代码:
bot.on('ready', async () => {
console.log(`${bot.user.username} is now ready!`);
const status_list = [`stuff on ${bot.guilds.cache.size} servers.`, `stuff on ${bot.channels.cache.size} channels.`];
// Create a new `let` variable, which can be assigned to, say, `count`.
// We start from 0 so that it doesn't mess up.
let count = 0;
setInterval(async () => {
// Check if the count is already the length of the status_list, if it is, then return to 0 again. This has to be done before the `status` variable has been set.
if (count === status_list.length + 1) count = 0;
// Define Status by using the counter
const status = status_list[count];
// Add the counter by 1 every time the interval passed, which indicates that the status should be changed.
count = count + 1;
bot.user.setActivity(status, { type: "LISTENING" });
}, 15000)
});
TA贡献1844条经验 获得超8个赞
我认为它应该是这样的:
bot.on('ready', () => {
console.log(`${bot.user.username} is now ready!`);
setInterval({
bot.user.setPresence({
activity: {
name: `Running in ${bot.guilds.cache.size} servers.`,
type: "LISTENING"
}
});
}, 15000);
});
这应该显示机器人在每个时间间隔内有多少台服务器。
添加回答
举报