开发者

extending an irc bot with modules

开发者 https://www.devze.com 2023-01-21 13:10 出处:网络
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:

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

精彩评论

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