【问题标题】:DiscordJS music bot disconnects immediately after starting to play the songDiscordJS 音乐机器人在开始播放歌曲后立即断开连接
【发布时间】:2021-12-16 03:42:04
【问题描述】:

我尝试播放 Youtube URL,但当它开始播放时,它会在 1 秒后停止。 mp3 文件可以正常工作,但当我尝试播放 Youtube 网址时它就无法正常工作。

这是我的代码:

case 'play':
  {
    const Channel = ['903334978199388271'];

    for (const channelId of Channel) {
      joinChannel(channelId);
    }

    function joinChannel(channelId) {
      Client.channels
        .fetch(channelId)
        .then((channel) => {
          const VoiceConnection = joinVoiceChannel({
            channelId: channel.id,
            guildId: channel.guild.id,
            adapterCreator: channel.guild.voiceAdapterCreator,
          });

          const player = createAudioPlayer();
          VoiceConnection.subscribe(player);
          player.play(
            createAudioResource(
              ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'),
            ),
          );
        })
        .catch(console.error);
    }
  }
  break;

【问题讨论】:

  • 控制台有错误吗?机器人发出了什么?
  • 不,我没有任何错误

标签: javascript discord.js ytdl


【解决方案1】:

我之前也遇到过同样的问题,问题是ytdl-core,所以你可以改用 ytdl-core-discord。这是一个例子:

const { Client, Intents, MessageEmbed } = require('discord.js')
const ytdl = require('ytdl-core-discord')

const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource,
    StreamType
} = require('@discordjs/voice')

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MEMBERS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES /// <= Don't miss this :)
    ]
});

var prefix = '!'

client.on('ready', async () => {
    console.log('Client is Ready...');
});

client.on('messageCreate', async (message) => {

    if (message.content.trim().toLocaleLowerCase() === prefix + 'play') {

        const channel = client.channels.cache.get('Channel ID Here')

        const connection = joinVoiceChannel({
            channelId: channel.id,
            guildId: channel.guildId,
            adapterCreator: channel.guild.voiceAdapterCreator
        });

        const player = createAudioPlayer();
        const resource = createAudioResource(await ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'), { inputType: StreamType.Opus });

        player.play(resource);

        connection.subscribe(player);
    }
});


client.login('Bot Token Here!');

【讨论】:

    猜你喜欢
    • 2020-05-24
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 2021-08-08
    • 2016-05-02
    • 2021-05-05
    相关资源
    最近更新 更多