Python IRC 机器人问题
我想制作一个机器人,它允许我从 irc 定义函数,并将我的机器人视为 Python 解释器。
例如,我想制作一个可以执行以下操作的机器人:
<mynick> py print "test"
<bot> test
<mynick> define hi(): print "hi"
<mynick> hi()
<bot> hi
我的问题是如何做到这一点
I want to make a bot that will allow me to define functions from irc and treat my bot like a Python interpreter too.
For example, I want to make a bot that can do something like the following:
<mynick> py print "test"
<bot> test
<mynick> define hi(): print "hi"
<mynick> hi()
<bot> hi
MY QUESTION IS HOW TO DO THIS
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您不担心安全性(如果这是一个个人项目,那么这是一个有效的假设),那么 Python 有几个函数(
compile
、exec
、eval
)可以在这里提供帮助。请注意,Python 2 和 Python 3 之间存在差异,但以下示例适用于两者:如果您打算让其他人使用此机器人,您将需要非常仔细地关注您让人们调用的函数。例如,
os
模块中的大多数内容都有可能在 irc 机器人上下文中执行不良操作。If you're not worried about security (and if this is a personal project then that's a valid assumption), then Python has several functions (
compile
,exec
,eval
) that can help here. Note that there are differences between Python 2 and Python 3, but the following example works for both:If you're going to let other people use this bot, you will want to pay very careful attention to the functions you let people call. For example, most things in the
os
module have potential to do undesirable things in an irc bot context.我写了 skybot,它的功能接近你想要的。
Python 解释器在 GAE 上运行,是 http://shell.appspot.com/ 的克隆,已修改为不存储状态,以防止人们破坏机器人。
I wrote skybot, which has functionality close to what you want.
The Python interpreter runs on GAE and is a clone of http://shell.appspot.com/ modified to not store state, to prevent people from breaking the bot.
您可以将代码挂接到以下位置: http://tumbolia.appspot.com/py/
这个解决方案的优点是您不必担心服务器上执行的代码。这就是 IRC 机器人 phenny 执行“.py”命令的 python 代码的方式。
You could hook your code into this: http://tumbolia.appspot.com/py/
The nice thing about this solution is that you don't have to worry about the code being executed on your server. This is how the IRC bot phenny executes python code for the '.py' command.