如何使自己的不和谐机器人

Discord has an excellent API for writing custom bots, and a very active bot community. Today we’ll take a look at how to get started making your own.

Discord具有出色的用于编写自定义机器人的API,以及非常活跃的机器人社区。 今天,我们将探讨如何开始制作自己的作品。

You will need a bit of programming knowledge to code a bot, so it isn’t for everyone, but luckily there are some modules for popular languages that make it very easy to do. We’ll be using the most popular one, discord.js.

您将需要一点编程知识来编写机器人程序,因此并非所有人都可以,但是幸运的是,有一些流行语言模块可以很容易地实现。 我们将使用最受欢迎的discord.js 。

入门 (Getting Started)

Head over to Discord’s bot portal, and create a new application.

转到Discord的机器人门户,并创建一个新应用程序。

You’ll want to make a note of the Client ID and secret (which you should keep a secret, of course). However, this isn’t the bot, just the “Application.” You’ll have to add the bot under the “Bot” tab.

您需要记下Client ID和机密(当然,您应该保守机密)。 但是,这不是机器人,而是“应用程序”。 您必须在“启动”标签下添加机器人。

Make a note of this token as well, and keep it a secret. Do not, under any circumstances, commit this key to Github. Your bot will be hacked almost immediately.

还要记下此令牌,并将其保密。 在任何情况下,请勿将此密钥提交给Github。 您的漫游器几乎会立即被黑客入侵。

安装Node.js并获取编码 (Install Node.js and Get Coding)

To run Javascript code outside of a webpage, you need Node. Download it, install it, and make sure it works in a terminal (or Command Prompt, as all of this should work on Windows systems). The default command is “node.”

要在网页外部运行Javascript代码,您需要Node 。 下载它,安装它,并确保它可以在终端上运行(或命令提示符,因为所有这些都应在Windows系统上运行)。 默认命令是“节点”。

We also recommend installing the nodemon tool. It’s a command line app that monitors your bot’s code and restarts automatically on changes. You can install it by running the following command:

我们还建议安装nodemon工具。 这是一个命令行应用程序,可监视您的机器人代码并在更改后自动重新启动。 您可以通过运行以下命令来安装它:

npm i -g nodemon

You’ll need a text editor. You could just use notepad, but we recommend either Atom or VSC.

您需要一个文本编辑器。 您可以只使用记事本,但是我们建议使用Atom或VSC 。

Here’s our “Hello World”:

这是我们的“ Hello World”:

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', msg => {
if (msg.content === 'ping') {
msg.reply('pong');
}
});
client.login('token');

This code is taken from the discord.js example. Let’s break it down.

此代码取自discord.js示例。 让我们分解一下。

  • The first two lines are to configure the client. Line one imports the module into an object called “Discord,” and line two initializes the client object.

    前两行用于配置客户端。 第一行将模块导入名为“ Discord”的对象,第二行初始化客户端对象。
  • The client.on('ready') block will fire when the bot starts up. Here, it’s just configured to log its name to the terminal.

    机器人启动时, client.on('ready')块将触发。 在这里,它只是配置为将其名称记录到终端。

  • The client.on('message') block will fire everytime a new message is posted to any channel. Of course, you’ll need to check the message content, and that’s what the if block does. If the message just says “ping,” then it will reply with “Pong!”

    每当有新消息发布到任何频道时, client.on('message')块都会触发。 当然,您需要检查消息的内容,这就是if块的作用。 如果该消息仅显示“ ping”,则将回复“ Pong!”。

  • The last line logs in with the token from the bot portal. Obviously, the token in the screenshot here is fake. Don’t ever post your token on the internet.

    最后一行使用Bot门户中的令牌登录。 显然,此屏幕截图中的令牌是伪造的。 永远不要将令牌发布到互联网上。

Copy this code, paste in your token at the bottom, and save it as index.js in a dedicated folder.

复制此代码,将其粘贴在底部的令牌中,并将其另存为index.js在专用文件夹中。

如何运行机器人 (How to Run the Bot)

Head over to your terminal, and run the following command:

转到您的终端,然后运行以下命令:

nodemon --inspect index.js

This starts up the script, and also fires up the Chrome debugger, which you can access by typing chrome://inspect/  into Chrome’s Omnibar and then opening “dedicated devtools for Node.”

这将启动脚本,并启动Chrome调试器,您可以通过在Chrome的Omnibar中键入chrome://inspect/ ,然后打开“ Node专用devtools”来访问该调试器。

Now, it should just say “Logged in as <bot-name>,” but here I’ve added a line that will log all message objects received to the console:

现在,它应该只说“以<bot-name>登录”,但是在这里我添加了一行,它将记录接收到控制台的所有消息对象:

So what makes up this message object? A lot of stuff, actually:

那么,什么构成了这个消息对象呢? 实际上有很多东西:

Most notably, you have the author info and the channel info, which you can access with msg.author and msg.channel. I recommend this method of logging objects to the Chrome Node devtools, and just looking around to see what makes it work. You may find something interesting. Here, for example, the bot logs its replies to the console, so the bot’s replies trigger client.on('message'). So, I made a spambot:

最值得注意的是,您具有作者信息和频道信息,可以使用msg.author和msg.channel访问这些信息。 我建议您使用这种方法将对象记录到Chrome Node devtools,然后四处看看是否能使它起作用。 您可能会发现一些有趣的东西。 例如,在这里,僵尸程序将其答复记录到控制台,因此,僵尸程序的答复会触发client.on('message') 。 所以,我做了一个垃圾邮件机器人:

Note: Be careful with this, as you don’t really want to deal with recursion.

注意:请谨慎操作,因为您确实不想处理递归。

如何将Bot添加到服务器 (How to Add the Bot to Your Server)

This part is harder than it should be. You have to take this URL:

这部分比应做的要难。 您必须使用以下网址:

https://discordapp.com/oauth2/authorize?client_id=CLIENTID&scope=bot

And replace CLIENTID with your bot’s client ID, found on the general information tab of the application page. Once this is done though, you can give the link to your friends to have them add the bot to their servers as well.

并将CLIENTID替换为bot的客户端ID,该ID可在应用程序页面的常规信息标签上找到。 一旦完成,您就可以将链接提供给您的朋友,让他们也将机器人添加到他们的服务器中。

好吧,那我还能做什么? (Alright, So What Else Can I Do?)

Beyond basic setup, anything else is entirely up to you. But, this wouldn’t be much of a tutorial if we stopped at hello world, so let’s go over some of the documentation, so you have a better idea of what’s possible. I suggest you read through as much as you can, as it’s very well documented.

除了基本设置外,其他任何事情都完全由您决定。 但是,如果我们停滞不前,那么本教程就不会多了,所以让我们看一下一些文档,这样您就更好地了解了可能。 我建议您尽可能多地通读,因为它有充分的记录。

I would recommend adding console.log(client) to the start of your code, and taking a look at the client object in the console:

我建议将console.log(client)添加到代码的开头,并在控制台中查看client对象:

From here, you can learn a lot. Since you can add a bot to multiple servers at once, servers are part of the Guilds map object. In that object are the individual Guilds (which is the API’s name for “server”) and those guild objects have channel lists that contain all the info and lists of messages. The API is very deep, and may take a while to learn, but at least it’s easy to set up and get started learning.

从这里,您可以学到很多东西。 由于您可以一次将漫游器添加到多个服务器,因此服务器是Guilds映射对象的一部分。 在该对象中是各个公会(这是“服务器”的API名称),并且那些公会对象具有包含所有信息和消息列表的通道列表。 该API非常深入,可能需要花费一些时间来学习,但至少它很容易设置并开始学习。

翻译自: https://www.howtogeek.com/364225/how-to-make-your-own-discord-bot/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/279181.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

​css3属性选择器总结

css3属性选择器总结 &#xff08;1&#xff09;E[attr]只使用属性名&#xff0c;但没有确定任何属性值 <p miaov"a1">111111</p> <p miaov"a2">111111</p> p[miaov]{background: red;} /*所有属性为miaov的元素都会被背景变红&a…

程序代码初学者_初学者:如何使用热键在Windows中启动任何程序

程序代码初学者Assigning shortcut keys to launch programs in Windows is probably one of the oldest geek tricks in the book, but in true geek fashion we are going to show you how to do it in Windows 8. 分配快捷键以在Windows中启动程序可能是本书中最古老的怪胎技…

stevedore——启用方式

2019独角兽企业重金招聘Python工程师标准>>> setuptools维护的入口点注册表列出了可用的插件&#xff0c;但是并没有为最终用户提供使用或启用的方法。 下面将描述用于管理要使用的扩展集的公共模式。 通过安装方式启用 对于许多应用程序&#xff0c;仅仅安装一个扩…

C# -- 文件的压缩与解压(GZipStream)

文件的压缩与解压 需引入 System.IO.Compression; 1.C#代码&#xff08;入门案例&#xff09; 1 Console.WriteLine("压缩文件...............");2 using (FileStream fr File.OpenRead("d:\\test.txt"))3 {4 …

win7屏保文件.scr_如何将屏保添加到Ubuntu 12.04

win7屏保文件.scrUbuntu 12.04 doesn’t ship with any screen savers, just a black screen that appears when your system is idle. If you’d rather have screensavers, you can swap gnome-screensaver for XScreenSaver. Ubuntu 12.04没有附带任何屏幕保护程序&#xff…

简单读写XML文件

IPAddress.xml 文件如下&#xff1a; <?xml version"1.0" encoding"utf-8"?><IP><IPAddress>192.168.0.120</IPAddress></IP> 在 Form 窗体(读取XML配置.Designer.cs)中有如下控件&#xff1a; 代码 privateSystem.Wind…

如何与Ubuntu One同步配置文件

Ubuntu One lets you easily synchronize files and folders, but it isn’t clear how to sync configuration files. Using Ubuntu One’s folder synchronization options or some symbolic links, you can synchronize configuration files across all your computers. Ubu…

智能家居设备_您的智能家居设备正在监视您吗?

智能家居设备In a world where we’re all paranoid about devices spying on us (and rightfully so), perhaps no other devices receive more scrutiny than smarthome products. But is that scrutiny warranted? 在一个我们都对监视设备的人都抱有偏执的世界(理应如此)&a…

Jenkins忘记admin密码处理方法

1、先找到enkins/config.xml文件&#xff0c;并备份。 此文件位于Jenkins系统设置的主目录&#xff0c;根据自己的配置情况而定。我的位置如下 /data/temp/jenkins/config.xml2、然后编辑config.xml删除<useSecurity>true</useSecurity>至</securityRealm>&a…

科研绘图工具软件_如何在Windows 10 Mail中使用绘图工具

科研绘图工具软件Microsoft recently released a new feature for the Windows 10 Mail app that lets you convey messages with drawings right inside the body of an email. This is a great way to quickly sketch things like graphs or tables to get your point across…

子元素相对于父元素垂直居中对齐

记个笔记 1. 元素相对于浏览器居中 <style>.window-center {/* 将position设置为fixed&#xff0c;使元素相对于浏览器窗口定位 */position: fixed;/* 将margin设置为auto&#xff0c;使浏览器自动推算元素外边距 */margin: auto;/* 将上下左右边距&#xff08;相对于浏览…

网站运行java_定制化Azure站点Java运行环境(5)

Java 8下PermGen及参数设置在上一章节中&#xff0c;我们定制化使用了Java 8环境&#xff0c;使用我们的测试页面打印出了JVM基本参数&#xff0c;但如果我们自己观察&#xff0c;会发现在MXBeans中&#xff0c;没有出现PermGen的使用数据&#xff0c;初始大小等信息&#xff0…

三阶魔方魔方公式_观看此魔方的自我解决

三阶魔方魔方公式Finally: a Rubik’s cube that can solve itself. A maker named Human Controller built it in Japan, and you can see it in action right now. 最后&#xff1a;一个可以解决自身问题的魔方。 一家名为Human Controller的制造商在日本制造了它&#xff0…

pc样式在ie8中的bug

2019独角兽企业重金招聘Python工程师标准>>> pc样式在ie8中的bug 1,box-sizing:border-box: 在ie中,此属性的使用有限制: (在IE8中&#xff0c;min-width属性适用于content-box即使box-sizing设置为border-box。 Chrome select在使用时从元素中选择选项时遇到问…

下载: 虾米音乐_您所说的内容:如何组织凌乱的音乐收藏

下载: 虾米音乐Earlier this week we asked you to share your tips, tricks, and tools, for managing a messy music collection. Now we’re back to share so great reader tips; read on to find ways to tame your mountain of music. 本周早些时候&#xff0c;我们要求您…

Django form表单

Django form表单 目录 普通方式手写注册功能 views.pylogin.html使用form组件实现注册功能 views.pylogin2.html常用字段与插件 initialerror_messagespasswordradioSelect单选Select多选Select单选checkbox多选checkboxDjango Form所有内置字段校验补充进阶 应用Bootstrap样式…

java 多线程 优先级_java多线程之线程的优先级

在操作系统中&#xff0c;线程可以划分优先级&#xff0c;优先级较高的线程得到CPU资源较多&#xff0c;也就是CPU优先执行优先级较高的线程对象中的任务(其实并不是这样)。在java中&#xff0c;线程的优先级用setPriority()方法就行&#xff0c;线程的优先级分为1-10这10个等级…

PyQt5应用与实践

2015-01-16 19:00 by 吴秦, 69476 阅读, 5 评论, 收藏, 编辑 一个典型的GUI应用程序可以抽象为&#xff1a;主界面&#xff08;菜单栏、工具栏、状态栏、内容区域&#xff09;&#xff0c;二级界面&#xff08;模态、非模态&#xff09;&#xff0c;信息提示&#xff08;Toolti…

plex实现流媒体服务器_Plex继续远离服务器,提供网络节目

plex实现流媒体服务器() Plex now offers a “Web Shows” feature in certain versions of their interface, providing access to shows from brands like TWiT, GQ, and Popular Science. Plex现在在其界面的某些版本中提供了“网络节目”功能&#xff0c;可以访问TWiT&…

MIME协议(三) -- MIME邮件的组织结构

一封MIME邮件可以由多个不同类型的MIME消息组合而成&#xff0c;一个MIME消息表示邮件中的一个基本MIME资源或若干基本MIME消息的组合体。每个MIME消息的数据格式与RFC822数据格式相似&#xff0c;也包括头和体两部分&#xff0c;分别称为MIME消息头和MIME消息体&#xff0c;它…