【问题标题】:I want to control my Discord bot only from one voice channel我只想从一个语音通道控制我的 Discord 机器人
【发布时间】:2020-11-07 15:31:15
【问题描述】:

我只想从一个语音通道控制我的 Discord 机器人,但你可以将他从任何语音通道断开。只有当你在语音频道中与他在一起时,我才想让他断开连接。这是我的代码:

const { Command } = require('discord.js-commando');

module.exports = class LeaveCommand extends Command {
    constructor(client) {
        super(client, {
            name: 'leave',
            aliases: ['l'],
            group: 'music',
            memberName: 'leave',
            description: 'Ies din voice channel :(.',
            guildOnly: true,
        });
    }

    run(message) {
     var voiceChannel = message.member.voice.channel;
    if (!voiceChannel) {
      message.reply('Intra pe un voice channel si incearca din nou');
      return;
    }
        const connection = this.client.voice.connections.get(message.guild.id);
        if (!connection) return message.reply('Nu sunt intr-un voice channel.');
        connection.channel.leave();
        return message.reply(`Am iesit din voice channel-ul - "**__${connection.channel.name}__**"`);
    }
};

【问题讨论】:

  • 我无法理解您的问题。我认为您希望消息作者仅在它们位于同一 VoiceChannel 时才断开机器人的连接?对吗?
  • 是的,你是对的,只有当你和他在同一个语音频道时,我才想断开他的连接。
  • 如果我能解决它与跳过命令相同,您可以从任何语音通道跳过歌曲...

标签: javascript node.js discord discord.js


【解决方案1】:
if (!message.guild.voice || !message.guild.voice.channel) return message.channel.send(`I am not connected to any Voice channels.`); // Checking if the bot is connected to any voice channels.
if (!message.member.voice || !message.member.voice.channel) return message.channel.send(`You are not in any voice channels.`); // Checking if the message author is conencted to a voice channel.
if (message.member.voice.channelID !== message.guild.voice.channelID) return message.channel.send(`You need to be in the ${message.guild.voice.channel} channel.`); // Checking if the bot's voice channel is the same as message author's channel.

message.channel.send(`Command executed successfully.`); // Executing the command.

【讨论】:

  • 我无法打开我的机器人控制台,我随机崩溃,您可以将您的代码直接粘贴到我的代码中吗?可能我做错了什么......
猜你喜欢
  • 1970-01-01
  • 2020-07-16
  • 2021-04-24
  • 2021-04-05
  • 2020-12-16
  • 2021-03-22
  • 2023-03-28
  • 2020-01-04
  • 2021-07-04
相关资源
最近更新 更多