discord怎么创建账号_如何使用Discord.js创建音乐机器人

discord怎么创建账号

The discord API provides you with an easy tool to create and use your own bots and tools. Today we are going to take a look at how we can create a basic music bot and add it to our server. The bot will be able to play, skip and stop the music and will also support queuing functionality.

discord API为您提供了一个简单的工具来创建和使用自己的机器人和工具。 今天,我们将研究如何创建一个基本的音乐机器人并将其添加到服务器中。 该机器人将能够播放,跳过和停止音乐,并且还将支持排队功能。

设置Discord机器人 (Setting up a discord bot)

First, we need to create a new application on the discord development portal.

首先,我们需要在不和谐的开发门户上创建一个新的应用程序。

We can do so by visiting the portal and clicking on new application.

我们可以通过访问门户并单击新应用程序来实现。

After that, we need to give our application a name and click the create button.

之后,我们需要给我们的应用程序起一个名字,然后单击创建按钮。

After that, we need to select the bot tab and click on add bot.

之后,我们需要选择bot标签,然后点击添加bot。

Now our bot is created and we can continue with inviting it to our server.

现在,我们的机器人已创建,我们可以继续将其邀请到我们的服务器。

将机器人添加到服务器 (Adding the bot to your server)

After creating our bot we can invite it using the OAuth2 URL Generator.

创建我们的漫游器后,我们可以使用OAuth2 URL Generator邀请它。

For that, we need to navigate to the OAuth2 page and select bot in the scope tap.

为此,我们需要导航到OAuth2页面,然后在范围点击中选择Bot。

After that, we need to select the needed permissions to play music and read messages.

之后,我们需要选择播放音乐和阅读消息所需的权限。

Then we can copy our generated URL and paste it into our browser.

然后,我们可以复制生成的URL,并将其粘贴到浏览器中。

After pasting it, we add it to our server by selecting the server and clicking the authorize button.

粘贴后,通过选择服务器并单击授权按钮将其添加到服务器中。

创建我们的项目 (Creating our project)

Now we can start creating our project using our terminal.

现在,我们可以使用终端开始创建项目。

First, we create a directory and move into it. We can do so by using these two commands.

首先,我们创建一个目录并移至该目录。 我们可以使用这两个命令来做到这一点。

mkdir musicbot && cd musicbot

After that, we can create our project modules using the npm init command. After entering the command you will be asked some questions just answer them and continue.

之后,我们可以使用npm init命令创建我们的项目模块。 输入命令后,系统将询问您一些问题,只需回答并继续。

Then we just need to create the two files we will work in.

然后,我们只需要创建将要使用的两个文件。

touch index.js && touch config.json

Now we just need to open our project in our text editor. I personally use VS Code and can open it with the following command.

现在,我们只需要在文本编辑器中打开项目即可。 我个人使用VS Code,可以使用以下命令将其打开。

code .

Discord JS基础 (Discord js basics)

Now we just need to install some dependencies before we can get started.

现在,我们只需要安装一些依赖项就可以开始。

npm install discord.js ffmpeg-binaries opusscript ytdl-core --save

After the installation finished we can continue with writing our config.json file. Here we save the token of our bot and the prefix he should listen for.

安装完成后,我们可以继续编写config.json文件。 在这里,我们保存了机器人的令牌以及他应该听的前缀。

To get your token you need to visit the discord developer portal again and copy it from the bot section.

要获取令牌,您需要再次访问Discord开发人员门户并从bot部分复制它。

That are the only things we need to do in our config.json file. So let’s start writing our javascript code.

这是我们需要在config.json文件中执行的唯一操作。 因此,让我们开始编写我们JavaScript代码。

First, we need to import all our dependencies.

首先,我们需要导入所有依赖项。

After that, we can create our client and login using our token.

之后,我们可以创建我们的客户端并使用我们的令牌登录。

Now let’s add some basic listeners that console.log when they get executed.

现在,让我们添加一些基本的监听器,这些监听器在执行时会显示console.log。

After that, we can start our bot using the node command and he should be online on discord and print “Ready!” in the console.

之后,我们可以使用node命令启动我们的机器人,他应该在不和谐状态下在线并显示“ Ready!”。 在控制台中。

node index.js

阅读讯息 (Reading messages)

Now that our bot is on our server and able to go online, we can start reading chat messages and responding to them.

既然我们的机器人已经在我们的服务器上并且可以上网了,我们就可以开始阅读聊天消息并对其进行回复。

To read messages we only need to write one simple function.

要阅读消息,我们只需要编写一个简单的函数。

Here we create a listener for the message event and get the message and save it into a message object if it is triggered.

在这里,我们为消息事件创建一个侦听器,并获取消息并将其保存到消息对象(如果已触发)。

Now we need to check if the message is from our own bot and ignore it if it is.

现在,我们需要检查消息是否来自我们自己的机器人,如果是,则将其忽略。

if (message.author.bot) return;

In this line, we check if the author of the message is our bot and return if it is.

在这一行中,我们检查消息的作者是否是我们的机器人,如果是,则返回。

After that, we check if the message starts with the prefix we defined earlier and return if it doesn’t.

之后,我们检查消息是否以我们之前定义的前缀开头,如果没有则返回。

if (!message.content.startsWith(prefix)) return;

After that, we can check which command we need to execute. We can do so using some simple if statements.

之后,我们可以检查我们需要执行哪个命令。 我们可以使用一些简单的if语句来做到这一点。

In this code block, we check which command to execute and call the command. If the input command isn’t valid we write an error message into the chat using the send() function.

在此代码块中,我们检查要执行的命令并调用该命令。 如果输入命令无效,则使用send()函数将错误消息写入聊天室。

Now that we know which command we need to execute we can start implementing these commands.

既然我们知道需要执行哪个命令,就可以开始实现这些命令了。

添加歌曲 (Adding songs)

Let's start by adding the play command. For that, we need a song and a guild (A guild represent an isolated collection of users and channels and is often referred to as a server). We also need the ytdl library we installed earlier.

让我们从添加play命令开始。 为此,我们需要一首歌和一个公会(公会代表用户和频道的隔离集合,通常被称为服务器)。 我们还需要之前安装的ytdl库。

First, we need to create a map with the name of the queue where we save all the songs we type in the chat.

首先,我们需要创建一个带有队列名称的地图,其中保存了我们在聊天中键入的所有歌曲。

const queue = new Map();

After that, we create an async function called execute and check if the user is in a voice chat and if the bot has the right permission. If not we write an error message and return.

之后,我们创建一个称为execute的异步函数,并检查用户是否在语音聊天中以及该漫游器是否具有正确的权限。 如果没有,我们将写一条错误消息并返回。

Now we can continue with getting the song info and saving it into a song object. For that, we use our ytdl library which gets the song information from the youtube link.

现在,我们可以继续获取歌曲信息并将其保存到歌曲对象中。 为此,我们使用ytdl库,该库从youtube链接获取歌曲信息。

This will get the information of the song using the ytdl library we installed earlier. Then we save the information we need into a song object.

这将使用我们之前安装的ytdl库获取歌曲的信息。 然后,我们将所需的信息保存到歌曲对象中。

After saving the song info we just need to create a contract we can add to our queue. To do so we first need to check if our serverQueue is already defined which means that music is already playing. If so we just need to add the song to our existing serverQueue and send a success message. If not we need to create it and try to join the voice channel and start playing music.

保存歌曲信息后,我们只需要创建合同即可将其添加到队列中。 为此,我们首先需要检查是否已经定义了serverQueue,这意味着音乐已经在播放。 如果是这样,我们只需要将歌曲添加到我们现有的serverQueue中并发送成功消息即可。 如果不是,我们需要创建它并尝试加入语音通道并开始播放音乐。

Here we check if the serverQueue is empty and add the song to it if it’s not. Now we just need to create our contract if the serverQueue is null.

在这里,我们检查serverQueue是否为空,如果不是,则将歌曲添加到其中。 现在,如果serverQueue为空,我们只需要创建合同。

In this code block, we create a contract and add our song to the songs array. After that, we try to join the voice chat of the user and call our play() function we will implement after that.

在此代码块中,我们创建合同并将歌曲添加到songs数组。 之后,我们尝试加入用户的语音聊天,然后调用我们将在之后实现的play()函数。

播放歌曲 (Playing songs)

Now that we can add our songs to our queue and create a contract if there isn’t one yet we can start implementing our play functionality.

现在,我们可以将歌曲添加到队列中,如果还没有合同,可以创建合同,我们就可以开始实现播放功能了。

First, we will create a function called play which takes two parameters (the guild and the song we want to play) and checks if the song is empty. If so we will just leave the voice channel and delete the queue.

首先,我们将创建一个名为play的函数,该函数带有两个参数(行会和我们要播放的歌曲),并检查歌曲是否为空。 如果是这样,我们将离开语音通道并删除队列。

After that, we will start playing our song using the playStream() function and the URL of our song.

之后,我们将开始使用playStream()函数和歌曲的URL播放歌曲。

Here we create a stream and pass it the URL of our song. We also add two listeners that handle the end and error event.

在这里,我们创建一个流,并将其传递给歌曲的URL。 我们还添加了两个用于处理end和error事件的侦听器。

Note: This is a recursive function which means that it calls itself over and over again. We use recursion so it plays the next song when the song is finished.

注意:这是一个递归函数,这意味着它会一遍又一遍地调用自身。 我们使用递归,以便在歌曲结束后播放下一首歌曲。

Now we are ready to play a song by just typing !play URL in the chat.

现在,我们只需在聊天中输入!play URL即可播放歌曲。

跳过歌曲 (Skipping songs)

Now we can start implementing the skipping functionality. For that, we just need to end the dispatcher we created in our play() function so it starts the next song.

现在我们可以开始实现跳过功能。 为此,我们只需要结束在play()函数中创建的调度程序,即可启动下一首歌曲。

Here we check if the user that typed the command is in a voice channel and if there is a song to skip.

在这里,我们检查键入命令的用户是否在语音通道中,以及是否有要跳过的歌曲。

停止歌曲 (Stoping songs)

The stop() function is almost the same as the skip() except that we clear the songs array which will make our bot delete the queue and leave the voice chat.

stop()函数与skip()几乎相同,只是我们清除了songs数组,这将使我们的机器人删除队列并离开语音聊天。

index.js的完整源代码: (Complete source code for the index.js:)

Here you can get the complete source code for our music bot:

在这里,您可以获取我们音乐bot的完整源代码:

An introduction to the JavaScript DOMThe Javascript DOM (Document Object Model) is an interface that allows developers to manipulate the content, structure…

JavaScript DOM简介 Javascript DOM(文档对象模型)是允许开发人员操作内容,结构…的接口。

结论 (Conclusion)

You made it all the way until the end! Hope that this article helped you understand the Discord API and how you can use it to create a simple bot. If you want to see an example of a more advanced discord bot you can visit my Github repository.

一路走到最后! 希望本文能帮助您了解Discord API以及如何使用它来创建简单的机器人。 如果您想查看更高级的Discord机器人的示例,则可以访问我的Github存储库

If you want to read more articles just like this one you can visit my website or start following my newsletter.

如果您想像这样阅读更多文章,则可以访问我的网站或开始关注我的新闻通讯

If you have any questions or feedback, let me know in the comments down below.

如果您有任何疑问或反馈,请在下面的评论中告诉我。

翻译自: https://www.freecodecamp.org/news/how-to-create-a-music-bot-using-discord-js-4436f5f3f0f8/

discord怎么创建账号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值