使用模块扩展 irc 机器人

发布于 2024-09-28 16:20:18 字数 825 浏览 11 评论 0原文

我从头开始用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老街孤人 2024-10-05 16:20:18

您是否正在寻找 __import__ 函数?

Are you looking for the __import__ function?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文