【问题标题】:Move voice channel discord js移动语音通道不和谐js
【发布时间】:2021-11-17 10:35:57
【问题描述】:

我一直在用 Python 为 Discord 编写代码,但我想切换到 JS,因为还有更多。我遇到了一些麻烦,机器人没有加入或离开命令。所以我创造了一个。我可以让加入工作,但机器人不能离开。

const util = require("../util");

var arr = []
module.exports = {
    name: "join",
    aliases: ["j"],
    exec: async (msg, args) => {
        const { music } = msg.guild;
        if (!msg.member.voice.channel)
            return msg.channel.send(`:x: You need to be in a voice channel to run this command!`);
        if (msg.guild.me.voice.channel && !msg.guild.me.voice.channel.equals(msg.member.voice.channel))
            return msg.channel.send(`:thumbsup: **I've left** ${msg.guild.me.voice.channel} **and joining** ${msg.member.voice.channel}!`);
            await music.join(msg.member.voice.channel);
        const missingPerms = util.missingPerms(msg.guild.me.permissionsIn(msg.member.voice.channel), ["CONNECT", "SPEAK"]);
        if ((!music.player || !music.player.playing) && missingPerms.length)
            return msg.channel.send(`Hmm :thinking: I am unable to join you're voice chat! I am missing the permission: ${missingPerms.map(x => `\`${x}\``).join(", ")}. Do I have access to this voice chat?`);
        else {
            await music.join(msg.member.voice.channel);
        }


    }
}

如何让机器人移动和离开?

【问题讨论】:

  • 其中哪一部分没有按预期工作?有什么错误吗?
  • 嘿@MrMythical 感谢您的回复。在 python 中,我可以做 voice.disconnect() 但我不知道如何在 JS 中断开与语音的连接。在第 10 行,我要么希望机器人断开连接并重新加入新频道,要么喜欢 music.move_to() 之类的东西。谢谢

标签: node.js discord discord.js


【解决方案1】:

Discord.js v12

嗯,我发现您的问题很简单,您可以在 Official Documentation 中找到它。

您可以退出当前频道并使用<ThisChannel>.leave() 然后<NewChannel>.join() 加入新频道,而不是尝试switch

PS:你的代码有点乱,所以如果你想让它更易读,你可以使用msg.member.voice.channel作为会员频道,msg.guild.me.voice.channel作为你所在的频道,如果目标不存在,加入会员的语音频道是msg.member.voice.channel.join(),而不在()中放任何东西

更准确地说,你的新代码看起来像

        const { music } = msg.guild;
        if (!msg.member.voice.channel)
            return msg.channel.send(`:x: You need to be in a voice channel to run this command!`);
        if (msg.guild.me.voice.channel && !msg.guild.me.voice.channel.equals(msg.member.voice.channel))
            return msg.channel.send(`:thumbsup: **I've left** ${msg.guild.me.voice.channel} **and joining** ${msg.member.voice.channel}!`);
            await msg.guild.me.voice.channel.leave()
            await msg.member.voice.channel.join()
        const missingPerms = util.missingPerms(msg.guild.me.permissionsIn(msg.member.voice.channel), ["CONNECT", "SPEAK"]);
        if ((!music.player || !music.player.playing) && missingPerms.length)
            return msg.channel.send(`Hmm :thinking: I am unable to join you're voice chat! I am missing the permission: ${missingPerms.map(x => `\`${x}\``).join(", ")}. Do I have access to this voice chat?`);
        else await music.join(msg.member.voice.channel);

【讨论】:

  • 感谢您的回复,请试一试!谢谢
猜你喜欢
  • 2021-09-27
  • 2023-04-07
  • 2021-02-08
  • 2021-01-18
  • 1970-01-01
  • 2020-05-30
  • 2021-05-27
  • 2021-03-11
  • 2023-04-05
相关资源
最近更新 更多