亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我正在創建一個狀態,顯示我的機器人有多少公會,但它不會更新

我正在創建一個狀態,顯示我的機器人有多少公會,但它不會更新

眼眸繁星 2023-02-17 17:27:48
我正在嘗試為我的 Discord 機器人創建一個狀態,它顯示我的機器人所在的服務器數量。我希望它在每次將我的機器人添加到新服務器時刷新狀態。這是我當前的代碼:bot.on('ready', () => {  console.log(`${bot.user.username} is now ready!`);  status_list = ["stuff", `${bot.guilds.cache.size} servers`]  setInterval(() => {    var index = Math.floor(Math.random() * (status_list.length - 1) + 1);    bot.user.setActivity(status_list[index], { type: "LISTENING" });  }, 15000)});任何幫助將不勝感激,謝謝!
查看完整描述

3 回答

?
寶慕林4294392

TA貢獻2021條經驗 獲得超8個贊

與其依賴間隔,不如使用guildCreateguildDelete事件。每當機器人加入公會或從公會中移除時,它們就會分別被觸發??纯聪旅娴氖纠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);

    }

}


查看完整回答
反對 回復 2023-02-17
?
絕地無雙

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)

});


查看完整回答
反對 回復 2023-02-17
?
婷婷同學_

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);

});

這應該顯示機器人在每個時間間隔內有多少臺服務器。


查看完整回答
反對 回復 2023-02-17
  • 3 回答
  • 0 關注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號