【问题标题】:DiscordJS Play audio file on voice channelDiscordJS 在语音通道上播放音频文件
【发布时间】:2020-08-22 23:03:51
【问题描述】:

我遵循了一些教程并混合了一些代码,它可以工作,但我很难理解与 message.guild.channels.forEach 的异步,无论我如何改变它。

我希望机器人仅加入用户键入“测试”的语音通道,而不在所有语音通道上运行。

谢谢

exports.run = async (client, message, args, level) => {

  async function play(channel) {
    await channel.join().then(async (connection) => {
      let dispatcher = await connection.playFile('./img/aniroze.mp3');
      await dispatcher.on('end', function () {
        message.channel.send("???? **x** ????").then(sentEmbed => {
          sentEmbed.react("????")
          sentEmbed.react("????")
        channel.leave();});
      });
  });
  }

  let timer = 10000;
       message.guild.channels.forEach(async (channel) => {
       if (channel.type == 'voice' && channel.members.size > 1) {
      const embed2 = new Discord.RichEmbed()
      .setTitle('???? ????')
      .setColor("#3498DB")
      .setThumbnail(`${message.author.displayAvatarURL}`)
      .setTimestamp()
      message.channel.send(embed2);
      setTimeout(function () {
        play(channel);
      }, timer);
    }});

  setTimeout(function () {
  }, timer);
};

exports.conf = {
  enabled: true,
  aliases: ['test'],
  guildOnly: true,
  permLevel: 'User'
}

【问题讨论】:

  • 你不是很清楚你的问题是什么。另外,请不要在一个问题中提出超过 1 个问题。你应该总是把它写成一个单独的问题。
  • 感谢您的回复,我编辑了我的问题,没有问题,机器人正在做它正在做的事情,我只是在寻找 forEach 的替代品,这将使机器人加入同一个语音频道用户键入“测试”而不在服务器上的所有语音通道上运行...

标签: javascript discord.js


【解决方案1】:

如果您希望机器人加入发送命令的用户的语音频道,请使用:

const voiceChannel = message.member.voiceChannel

if (!voiceChannel) return message.reply('you are not in a voice channel')

voiceChannel.join()

注意:这是一个 discord.js v11 答案,对于 v12,将 member.voiceChannel 替换为 member.voice.channel

【讨论】:

  • 但是当我用 foreach 行替换它时它不会加入并给我错误;频道没有定义,真的把我搞砸了,你能实现你在代码上写的吗?
  • 好的,我明白了,我错过的是我的 const 和 sync 功能不一样,所以我用 voicechanel 更改了频道,非常感谢我会尝试使用它来让它工作对于我的另一个问题,它会检测到某个用户,然后加入并播放他的某个文件...
猜你喜欢
  • 2019-09-14
  • 2020-11-09
  • 1970-01-01
  • 2020-09-13
  • 2019-04-07
  • 1970-01-01
  • 2022-07-10
  • 2020-05-30
  • 2020-01-03
相关资源
最近更新 更多