Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

javascript - How to Play Audio File Into Channel?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

GitHub Project: LINK

In order to do this there are a few things you have to make sure of first.

  1. Have FFMPEG installed & the environment path set for it in Windows [link]
  2. Have Microsoft Visual Studio (VS) installed [link]
  3. Have Node.js installed.[link]
  4. Have Discord.js installed in VS.

From there the steps are quite simple. After making your project index.js you will start typing some code. Here are the steps:

  1. Add the Discord.js dependency to the project;

var Discord = require('discord.js');

  1. Create out client variable called bot;

var bot = new Discord.Client(); 3. Create a Boolean variable to make sure that the system doesn't overload of requests;

var isReady = true;

  1. Next make the function to intercept the correct message;

bot.on('message', message =>{ENTER CODE HERE});

  1. Create an if statement to check if the message is correct & if the bot is ready;

if (isReady && message.content === 'MESSAGE'){ENTER CODE HERE}

  1. Set the bot to unready so that it cannot process events until it finishes;

isReady = false;

  1. Create a variable for the channel that the message-sender is currently in;

var voiceChannel = message.member.voice.channel;

  1. Join that channel and keep track of all errors;

voiceChannel.join().then(connection =>{ENTER CODE HERE}).catch(err => console.log(err));

  1. Create a refrence to and play the audio file;

const dispatcher = connection.play('./audiofile.mp3');

  1. Slot to wait until the audio file is done playing;

dispatcher.on("end", end => {ENTER CODE HERE});

  1. Leave channel after audio is done playing;

voiceChannel.leave();

  1. Login to the application;

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...