第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

我正在創(chuàng)建一個(gè)狀態(tài),顯示我的機(jī)器人有多少公會,但它不會更新

我正在創(chuàng)建一個(gè)狀態(tài),顯示我的機(jī)器人有多少公會,但它不會更新

眼眸繁星 2023-02-17 17:27:48
我正在嘗試為我的 Discord 機(jī)器人創(chuàng)建一個(gè)狀態(tài),它顯示我的機(jī)器人所在的服務(wù)器數(shù)量。我希望它在每次將我的機(jī)器人添加到新服務(wù)器時(shí)刷新狀態(tài)。這是我當(dāng)前的代碼: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貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

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

});

現(xiàn)在,如果您想將其與選擇隨機(jī)狀態(tài)配對,您也可以執(zhí)行以下操作:


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

    }

}


查看完整回答
反對 回復(fù) 2023-02-17
?
絕地?zé)o雙

TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超4個(gè)贊

你的問題在這里:


var index = Math.floor(Math.random() * (status_list.length - 1) + 1);

這將每次都聲明相同的,這就是它不更新的原因


每次將我的機(jī)器人添加到新服務(wù)器時(shí),我都希望它刷新狀態(tài)。


這就是您(無意)錯(cuò)過的。你想要做的是,正如你已經(jīng)做過的那樣,使用setInterval(). 無論數(shù)字是否更改,這都會每隔一段時(shí)間刷新一次狀態(tài)。您不必確保它已更改,因?yàn)樗鼘δ膶?shí)例的負(fù)載很小。

現(xiàn)在,嘗試遵循以下代碼:


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.

});

這應(yīng)該可以滿足您的需要,但正如我從您的代碼中看到的那樣,您試圖隨機(jī)狀態(tài)?如果是這樣,請改為執(zhí)行以下操作:


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)

});

現(xiàn)在,如果你想要一個(gè)不斷變化的(不是隨機(jī)的)狀態(tài),你可以使用下面的代碼:


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)

});


查看完整回答
反對 回復(fù) 2023-02-17
?
婷婷同學(xué)_

TA貢獻(xiàn)1844條經(jīng)驗(yàn) 獲得超8個(gè)贊

我認(rèn)為它應(yīng)該是這樣的:


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

});

這應(yīng)該顯示機(jī)器人在每個(gè)時(shí)間間隔內(nèi)有多少臺服務(wù)器。


查看完整回答
反對 回復(fù) 2023-02-17
  • 3 回答
  • 0 關(guān)注
  • 133 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號