問題描述
我正在使用 presenceUpdate
事件,但是它觸發了兩次,我被告知它正在發射我與機器人共享的服務器數量.目前我的代碼輸出 online
兩次.我不確定如何讓它只輸出一次.
I am using the presenceUpdate
event however, it is triggering twice, I am told it is emitting for how many shared servers I have with the bot. Currently my code outputs online
twice. I am not sure on how to get it to output just once.
if (newPresence.userID === botid) {
if (newPresence.status === 'online') {
console.log(newPresence.status); // Should output 'online' currently outputs 'online online'
推薦答案
我注意到這個問題仍未解決,所以我會為您解決.所以上面的答案是正確的,但是,它們都沒有說明機器人為此需要的意圖.
I noticed that this remained unsolved, so I'll solve it for you. So the answers above are correct, however, none of them states the intents the bot requires for this.
你看,presenceUpdate
不會發出,除非你啟用了出席權限網關意圖.對于少于 100 個服務器中的機器人,可以通過翻轉開關在開發人員門戶上進行切換.否則,您需要為不和諧打開一張票以啟用存在意圖.
You see, presenceUpdate
won't emit unless you have enabled the presence privilege gateway intent. For bots in less than 100 servers, this can be toggled on the developer portal by flipping a switch. Otherwise you need to open a ticket for discord to enable the presence intent.
啟用 Intent 后,試試這個:
Once you enable intents, try this:
client.on(`presenceUpdate`,(member)=>{
console.log(`${member.user.username} has updated their presence.`)
})
然后嘗試將您的狀態更改為 dnd.如果你啟用它,你應該看到[您的標簽] 更新了他們的存在.
.否則你可能做錯了什么.
Then try changing your status to dnd. If you enabled it, you should see
[your tag] has updated their presence.
. Otherwise you may have done something wrong.
這篇關于檢測機器人帳戶是否離線/在線的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!