.net irc 是什么_使用Node.js构建IRC Bot

.net irc 是什么

One of the tasks on my WebDev bucket list has always been creating a functional IRC bot.  I have no clue why it's been high on my list, but ... it just has.  IRC bots are used for a variety of things:  Google search, keyword detection and information retrieval (i.e. a message with a bug number in Mozilla's IRC will fetch the title and link of the given bug), and more.  I recently found a Node.js library which allowed me to listen to common IRC events and respond to them;  better yet, it's incredibly easy to do.  Let me show you how to create your own IRC bot with JavaScript!

WebDev存储桶列表上的任务之一一直是创建功能正常的IRC机器人。 我不知道为什么它一直排在我的榜首,但是……只是。 IRC机器人用于多种用途:Google搜索,关键字检测和信息检索(即Mozilla IRC中带有错误号的消息将获取给定错误的标题和链接)等等。 最近,我找到了一个N​​ode.js库,该库使我能够侦听常见的IRC事件并对其进行响应。 更好的是,这非常容易做到。 让我向您展示如何使用JavaScript创建自己的IRC机器人!

节点IRC安装 (Node IRC Install)

Use npm to install the IRC library:

使用npm安装IRC库


npm install irc


This the only external library you will need!

这是您唯一需要的外部库!

机器人配置与检索 (Bot Configuration and Retrieval)

Setting up a config object is recommended as many settings may be shared throughout the bot's event listeners:

建议设置配置对象,因为可能在整个机器人事件监听器中共享许多设置:


// Create the configuration
var config = {
	channels: ["#davidwalshblog", "#mootools"],
	server: "irc.freenode.net",
	botName: "walshbot"
};


Then require the IRC library and create your bot:

然后需要IRC库并创建您的机器人:


// Get the lib
var irc = require("irc");

// Create the bot name
var bot = new irc.Client(config.server, config.botName, {
	channels: config.channels
});


Now you're setup is complete;  time to make things happen!

现在您已完成设置; 是时候让事情发生!

Bot侦听器和响应 (Bot Listeners and Responses)

Once a configuration is created, the library is available, and the bot has been created, the possibilities are endless.  The majority of interactions will start with an event listener and a basic response:

一旦创建了配置,可用的库以及已创建的bot,则无限的可能性。 大多数交互将从事件侦听器和基本响应开始:


// Listen for joins
bot.addListener("join", function(channel, who) {
	// Welcome them in!
	bot.say(channel, who + "...dude...welcome back!");
});


Whenever a user enters the room, they'll be greeted with the response above.  If I want to listen to each message and respond accordingly, I could add:

每当用户进入房间时,都会收到上面的回复。 如果我想听每条消息并做出相应回应,则可以添加:


// Listen for any message, PM said user when he posts
bot.addListener("message", function(from, to, text, message) {
	bot.say(from, "¿Que?");
});

// Listen for any message, say to him/her in the room
bot.addListener("message", function(from, to, text, message) {
	bot.say(config.channels[0], "¿Public que?");
});


There are many more events to listen for too;  the IRC API is quite extensive.  In the case of the Mozilla bug bot I mentioned above, one would simply need to parse the message for a 6 digit number to trigger a bug detail lookup, then message the response when the desired information was retrieved. With these event listeners in place, you can use any other Node.js lib or custom code to perform any function you'd like.

还有更多的事件需要听。 IRC API相当广泛 。 对于上面提到的Mozilla Bug机器人,只需解析消息中的6位数字即可触发Bug详细信息查找,然后在检索到所需信息时向响应发送消息。 有了这些事件侦听器后,您可以使用任何其他Node.js库或自定义代码来执行所需的任何功能。

运行机器人 (Running the Bot)

Running the bot is simple too -- simply open a new shell and execute:

运行机器人也很简单-只需打开一个新的shell并执行:


node bot.js


Thanks to this awesome NodeJS IRC library and its incredibly easy to use API, I can cross off creating an IRC bot from my WebDev bucket list.  Better yet, I got to create the bot using JavaScript.  If you want to create a useful bot, or simply one to annoy people, give the IRC library a try!

得益于这个出色的NodeJS IRC库及其易于使用的API,我可以从WebDev存储桶列表中创建IRC机器人。 更好的是,我必须使用JavaScript创建该机器人。 如果您想创建一个有用的机器人,或者只是一个令人讨厌的机器人,请尝试一下IRC库!

翻译自: https://davidwalsh.name/nodejs-irc

.net irc 是什么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值