使用模块扩展 irc 机器人
我从头开始用 Python 创建了一个 IRC 机器人,只是为了好玩。我有一种方法可以将模块加载到其中,但是您必须手动键入代码来加载每个模块,如下所示:
if data.find('PRIVMSG %s :add_mod foo\r\n' % channel)
enabled_mods.append(foo)
if data.find('PRIVMSG %s :rm_mod foo\r\n' % channel)
enabled_mods.remove(foo)
if data.find('PRIVMSG %s :add_mod bar\r\n' % channel)
enabled_mods.append(bar)
if data.find('PRIVMSG %s :rm_mod bar\r\n' % channel)
enabled_mods.remove(bar)
for mod in enabled_mods:
mod(data,irc,channel,nick)
有没有什么方法可以使用 for 循环来加载每个模块?还有其他建议吗?如:
modules = [foo,bar]
for module in modules:
if data.find('PRIVMSG %s :add_mod %s\r\n' % (channel,module))
enabled_mods.append(module)
if data.find('PRIVMSG %s :rm_mod %s\r\n' % (channel,module))
enabled_mods.remove(module)
解决了。
I've created an IRC bot in Python from scratch just for the fun of it. I've got a way to load modules into it, but you have to manually type out the code to load each module as below:
if data.find('PRIVMSG %s :add_mod foo\r\n' % channel)
enabled_mods.append(foo)
if data.find('PRIVMSG %s :rm_mod foo\r\n' % channel)
enabled_mods.remove(foo)
if data.find('PRIVMSG %s :add_mod bar\r\n' % channel)
enabled_mods.append(bar)
if data.find('PRIVMSG %s :rm_mod bar\r\n' % channel)
enabled_mods.remove(bar)
for mod in enabled_mods:
mod(data,irc,channel,nick)
Is there any way to load each module using a for loop for example? Any other suggestions? Such as:
modules = [foo,bar]
for module in modules:
if data.find('PRIVMSG %s :add_mod %s\r\n' % (channel,module))
enabled_mods.append(module)
if data.find('PRIVMSG %s :rm_mod %s\r\n' % (channel,module))
enabled_mods.remove(module)
SOLVED.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否正在寻找 __import__ 函数?
Are you looking for the __import__ function?