开发者

extending an irc bot with modules

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.


Are you looking for the __import__ function?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜