3 回答

TA貢獻2021條經驗 獲得超8個贊
與其依賴間隔,不如使用guildCreate
和guildDelete
事件。每當機器人加入公會或從公會中移除時,它們就會分別被觸發??纯聪旅娴氖纠a。
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);
});
這應該顯示機器人在每個時間間隔內有多少臺服務器。
添加回答
舉報