【问题标题】:Discord bot: How to play an mp3 file in a voice channelDiscord bot:如何在语音频道中播放 mp3 文件
【发布时间】:2020-11-29 04:07:31
【问题描述】:

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

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

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

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

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

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

【问题讨论】:

    标签: bots discord.js


    【解决方案1】:
    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))
    };
    

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

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

    【讨论】:

    • 嘿@Jakye,当我打开 CMD 并输入这些内容时,它们出现了错误。我是否有一种特定的方式将这些放入 CMD?我刚刚下载了ffmpeg并安装了它,但我想仔细检查以确保P:也谢谢你的帮助:)
    • 这是其中一些的截图:cdn.discordapp.com/attachments/354118132626882570/…另一个太大了,所以我用了一个hatbin,如果可以的话:hatebin.com/orjrkatwzn
    • 我收到的另一个错误是机器人说它无法读取未定义 P 的属性“通道”:
    • 在您提供的屏幕截图中,没有错误。忽略警告。至于hatbin,请确保您使用的是最新的稳定版node,即12.18.3
    • 我是,但我仍然遇到错误,这里可能会更好:cdn.discordapp.com/attachments/354118132626882570/… 这是我尝试使用命令播放音乐时得到的结果。我不确定我在哪里出错了 tbh P:
    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 2022-07-10
    • 2021-10-29
    • 2021-03-02
    • 2020-11-04
    • 2022-12-11
    • 2021-11-18
    • 2021-08-23
    相关资源
    最近更新 更多