Discord bot:如何在语音频道中播放 mp3 文件 [英] Discord bot: How to play an mp3 file in a voice channel

查看:18
本文介绍了Discord bot:如何在语音频道中播放 mp3 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为粉丝服务器制作了一个自定义的不和谐机器人,它由 Heroku 托管,因此它可以在我不在线时保持在线.我想要做的是让机器人在有人为它发出特定命令时播放其文件夹中的 mp3 文件,但我希望音乐在特定的语音频道中播放.

I have made a custom discord bot for a fan server, which is hosted by Heroku so it can stay online when I'm not. What I want to do is to have the bot play an mp3 file from its folder when someone gives a specific command for it, but I want the music to play in a specific voice channel.

首先,我尝试将其视为图像文件.例如:

First, I tried treating it, the same as an image file. For example:

if (message.content == "Play the Funky Tune.")
{ 
    message.reply("Of course, I hope you enjoy!", { files: ["./Music/FunkyTune.mp3"] });
}

但是,这会导致机器人将音乐文件放在那里,虽然您可以点击它并播放它,但当您点击另一个频道时,音乐就会暂停.我希望它在特定的语音频道中播放歌曲,以便在用户点击另一个频道时继续播放.

However this causes the bot to put the music file there, and while you can click on it and play it, the moment you click on another channel, the music is paused. I want it to play the song in a specific voice channel so that it continues playing when the user clicks on another channel.

在为此进行研究时,每个主题总是要求提供 YouTube 视频链接,然后将其转换为音频,但这不是我想要的,因为 YouTube 视频可能会发生任何事情.

When doing research for this, every topic is always asking for a YouTube video link, which converts it into audio, but this is not what I want, since anything can happen to YouTube videos.

附:我对使用已经制作的机器人不感兴趣,我想将此音乐功能集成到我自己的机器人中.

推荐答案

if (message.content == "Play the Funky Tune.") {
    // Checking if the message author is in a voice channel.
    if (!message.member.voice.channel) return message.reply("You must be in a voice channel.");
    // Checking if the bot is in a voice channel.
    if (message.guild.me.voice.channel) return message.reply("I'm already playing.");

    // Joining the channel and creating a VoiceConnection.
    message.member.voice.channel.join().then(VoiceConnection => {
        // Playing the music, and, on finish, disconnecting the bot.
        VoiceConnection.play("./Music/FunkyTune.mp3").on("finish", () => VoiceConnection.disconnect());
        message.reply("Playing...");
    }).catch(e => console.log(e))
};


请注意,您必须运行以下命令才能使其正常工作:


Note that you must run the following commands in order for this to work:

  • npm install ffmpeg-static
  • npm install @discordjs/opus

这篇关于Discord bot:如何在语音频道中播放 mp3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆