问题描述
如何播放Discord机器人的音频文件?需要播放本地文件,在JS中,并且在发送某个消息时,它将加入键入消息的用户,并将文件播放到该频道。
How do you play an audio file from a Discord bot? Needs to play a local file, be in JS, and upon a certain message being sent it will join the user who typed the message, and will play the file to that channel.
推荐答案
GitHub项目:
GitHub Project: LINK
为了做到这一点,你必须先确定一些事项。
In order to do this there are a few things you have to make sure of first.
- 拥有FFMPEG安装&在Windows中为其设置的环境路径[]
- 安装Microsoft Visual Studio(VS)[]
- 安装了Node.js. []
- 在VS中安装了Discord.js. []
- Have FFMPEG installed & the environment path set for it in Windows [link]
- Have Microsoft Visual Studio (VS) installed [link]
- Have Node.js installed.[link]
- Have Discord.js installed in VS. [link]
从那里开始步骤非常简单。在您的项目 index.js
之后,您将开始输入一些代码。以下是步骤:
From there the steps are quite simple. After making your project index.js
you will start typing some code. Here are the steps:
-
将Discord.js依赖项添加到项目中;
Add the Discord.js dependency to the project;
var Discord = require('discord.js');
创建名为 bot 的客户端变量;
Create out client variable called bot;
var bot = new Discord.Client();
创建一个布尔变量,以确保系统不会超载请求;
Create a Boolean variable to make sure that the system doesn't overload of requests;
var isReady = true;
接下来使函数拦截正确的消息;
Next make the function to intercept the correct message;
bot.on('message',message => {ENTER CODE HERE});
创建 if语句以检查消息是否正确&如果机器人准备就绪;
Create an if statement to check if the message is correct & if the bot is ready;
if(isReady&& message.content ==='MESSAGE'){ENTER CODE HERE}
将机器人设置为未准备好,以便在完成之前无法处理事件;
Set the bot to unready so that it cannot process events until it finishes;
isReady = false;
为邮件发件人创建一个变量目前在;
Create a variable for the channel that the message-sender is currently in;
var voiceChannel = message.member.voiceChannel;
加入该频道并跟踪所有错误;
Join that channel and keep track of all errors;
voiceChannel.join()。then(connection = > {ENTER CODE HERE})。catch(err => console.log(err));
创建一个参与并播放音频文件;
Create a refrence to and play the audio file;
const dispatcher = connection.playFile('./ audiofile.mp3');
等待音频文件播放完毕的插槽;
Slot to wait until the audio file is done playing;
dispatcher.on(end,end => {ENTER CODE HERE});
在aud之后离开频道io已经完成播放;
Leave channel after audio is done playing;
voiceChannel.leave();
登录该应用程序;
bot.login('CLIENT TOKEN HERE');
完成此操作后,请务必检查是否有未关闭的括号或括号。我这样做是因为我花了几个小时才找到一个好的解决方案,所以我只是想和那些在那里寻找类似东西的人分享。
After you are all finished with this, make sure to check for any un-closed brackets or parentheses. i made this because it took my hours until I finally found a good solution so I just wanted to share it with anybody who is out there looking for something like this.
这篇关于如何将音频文件播放到频道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!