如何使自己的不和諧機器人

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.pswp.cn/news/279181.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/279181.shtml
英文地址,請注明出處:http://en.pswp.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…

java復合賦值運算符_Java 之復合賦值運算符

1.引入問題切入正題&#xff0c;看下面代碼&#xff0c;結果應該是怎么樣的public class App{public static void main( String[] args ){byte a1 ;int b 10;a ab;System.out.println(a);ab;System.out.println(a);}}這段代碼的執行結果是什么&#xff1f;&#xff1f;2. 執行…

程序代碼初學者_初學者:如何使用熱鍵在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;僅僅安裝一個擴…

java 重置定時器_可重置Java定時器

我想有一個java.utils.Timer與一個可重置時間在java.I需要設置一次off事件發生在X秒。如果在創建定時器的時間和X秒之間沒有發生任何事情&#xff0c;則事件會正常發生。然而&#xff0c;如果在X秒之前&#xff0c;我決定該事件應該發生在Y秒后&#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…

java 輸入流關閉順序_Java IO流中先關閉輸出流還是先關閉輸入流?為什么?

java中需要手動釋放的資源bai常見的有以下兩個&#xff1a;流相du關資zhi源流相關資源一般遵循&#xff1a;1)先開后關dao&#xff0c;先開的輸入流&#xff0c;再開的輸出流&#xff0c;通過讀取輸入流寫入輸出流中&#xff0c;那么應該先關輸出流&#xff0c;再關輸入流&…

解析Linux操作系統文件目錄

隨著Linux的不斷發展&#xff0c;越來越多的人開始使用Linux&#xff0c;對于那些剛剛接觸的人來說&#xff0c;恐怕最先感到困惑的就是那些“不明不白”的目錄了。如果想熟練使用Linux&#xff0c;讓Linux聽命于自己&#xff0c;就必須掌握這些目錄&#xff0c;下面就以Xteam公…

智能家居設備_您的智能家居設備正在監視您嗎?

智能家居設備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…

java讀取excel某個單元格的值_java poi怎么獲取excel單元格的內容

展開全部package edu.sjtu.erplab.poi;import java.io.InputStream&chww.xqy.chain" target"_blank" class"link-baike">FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;impor…

科研繪圖工具軟件_如何在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;相對于瀏覽…

SQL Server編程(06)觸發器

SQL Server 通過觸發器用來保證業務邏輯和數據的完整性。在SQL Server中&#xff0c;觸發器是一種特殊類型的存儲過程&#xff0c;可在執行語言事件時自動觸發。SQL Server中觸發器包括三種&#xff1a;DML觸發器、DDL觸發器和登錄觸發器。 DML觸發器&#xff1a;執行DML語句觸…

網站運行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在使用時從元素中選擇選項時遇到問…