簡體   English   中英

為什么在用戶離開語音頻道時嘗試編輯用戶時會出現 DiscordAPIError?

[英]Why do I get a DiscordAPIError when trying to edit a user as they leave a voice channel?

首先讓我說這是我第一次嘗試編碼 Discord 機器人。 另外,這幾天我一直在為此苦苦思索,所以我的大腦感覺像糊狀一樣。 話雖這么說,如果這個問題的答案對其他人來說顯而易見,我深表歉意。

為了解釋我在這里做什么,我的機器人用於一個特定的游戲,在這個游戲中有一段時間我們不希望人們能夠聽到彼此的談話(或不由自主地對游戲中發生的事情做出反應) . 運行游戲的人使用命令,這將使語音通道中的每個人靜音或取消靜音。 該機器人在這方面工作得很好。 它已經過多人多次測試。

然而,我想添加一個應急措施,因為我們不希望過早離開游戲的人最終無限期地被服務器靜音(直到管理員可以對此采取措施)。 我對此的回答是有一個特定的角色,當他們被靜音時分配,當他們取消靜音時被刪除。 然后,機器人應該在該人離開語音頻道時檢查該角色,如果有,則確保該角色已被刪除並且該人已取消靜音。 (這確保了如果他們由於除此機器人之外的其他原因而被服務器靜音,他們將無法使用機器人功能來解決這個問題。)

所以這是我寫的(並重寫了很多次,試圖讓它發揮作用):

client.on('voiceStateUpdate', (oldState, newState) => {
    let oldServer = oldState.guild;
    let oldChannel = oldState.channel;
    let oldMember = oldState.member;
    // If user leaves a voice channel (ignores voiceStateUpdate caused by muting/unmuting in other functions).
    if (oldChannel && oldChannel !== newState.channel) {
        console.log(`${oldMember.user.tag} left channel ${oldChannel.name} (${oldServer.name}).`);
        // Check if they have the "Hushed" role.
        if (oldMember.roles.cache.some(role => role.name === 'Hushed')) {
            // Remove the "Hushed" role, if the user has it.
            let role = oldServer.roles.cache.find(role => role.name === 'Hushed');
            oldMember.roles.remove(role).catch(console.error);
            console.log(`- "Hushed" role removed from ${oldMember.user.tag}.`);
            // Unmute this member.
            oldMember.voice.setMute(false);
            console.log(`- User ${oldMember.user.tag} unmuted.`);
        }
    }
})

當有人離開語音通道時它會識別並知道他們是否有角色,因為我的 console.log 消息打印到控制台 window,但這似乎是功能停止的地方。 它不會刪除角色或取消靜音用戶。 這是我的 console.log(出於顯而易見的原因,我屏蔽了所有我認為是私人的信息):

MY_DISCORD_TAG left channel Testing (MY_DISCORD_SERVER).
- "Hushed" role removed from MY_DISCORD_TAG.
- User MY_DISCORD_TAG unmuted.
(node:17092) UnhandledPromiseRejectionWarning: DiscordAPIError: Target user is not connected to voice.
    at RequestHandler.execute (C:\Users\MY_NAME\Discord\Hush\node_modules\discord.js\src\rest\RequestHandler.js:170:25)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:17092) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:17092) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

我對編程或調試自己的代碼並不陌生,通常可以自己解決這些問題(通過閱讀文檔或在 Google 上搜索)。 但是,在為此苦苦掙扎了幾天之后,我沒有運氣。 所以我決定嘗試接觸社區。 如果能深入了解導致失敗的原因,我將不勝感激。

Discord 文檔說,當用戶不在語音通道中時嘗試設置用戶的靜音/耳聾會引發錯誤,因此一旦用戶離開通道,您就無法取消靜音。 相反,如果用戶以“Hushed”角色加入頻道(在不在頻道之后),您可以取消靜音:

client.on('voiceStateUpdate',(oldState, newState) => {
    let oldServer = oldState.guild;
    let oldChannel = oldState.channel;
    let oldMember = oldState.member;
    let newChannel = newState.channel;

    // If the user changed voice channels or the user joined a channel (after not being in one)
    if (oldChannel && newChannel && oldChannel !== newChannel || !oldChannel) {
        // Check if they have the "Hushed" role.
        if (oldMember.roles.cache.some(role => role.name === 'Hushed')) {
            // Remove the "Hushed" role, if the user has it.
            let role = oldServer.roles.cache.find(role => role.name === 'Hushed');
            oldMember.roles.remove(role)
                .then(() => {
                    // This will be logged after the role has been successfully removed.
                    // As removing roles is asynchronous, your code would have logged this
                    // regardless of whether the role was actually removed.
                    console.log(`- "Hushed" role removed from ${oldMember.user.tag}.`);
                    // Unmute this member.
                    return oldMember.voice.setMute(false);
                })
                .then(() => console.log(`- User ${oldMember.user.tag} unmuted.`))
                .catch(error => console.error(error));
            }
        }
    }
});

使用 ES2017 的async / await

client.on('voiceStateUpdate', async (oldState, newState) => {
    let oldServer = oldState.guild;
    let oldChannel = oldState.channel;
    let oldMember = oldState.member;
    let newChannel = newState.channel;

    // If the user changed voice channels or the user joined a channel (after not being in one)
    if (oldChannel && newChannel && oldChannel !== newChannel || !oldChannel) {
        // Check if they have the "Hushed" role.
        if (oldMember.roles.cache.some(role => role.name === 'Hushed')) {
            // Remove the "Hushed" role, if the user has it.
            let role = oldServer.roles.cache.find(role => role.name === 'Hushed');
            try {
                await oldMember.roles.remove(role);
                console.log(`- "Hushed" role removed from ${oldMember.user.tag}.`);
                // Unmute this member.
                await oldMember.voice.setMute(false);
                console.log(`- User ${oldMember.user.tag} unmuted.`);
            } catch (error) {
                console.error(error);
            }
        }
    }
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM