1
我正在使用Sopel(之前的Willie和之前的Jenni/Phenny)python IRC bot,因为我想为我们的IRC频道设置一个小问答测验。如何在Sopel IRC bot模块中设置并更改@rule? (Python)
使用Sopel,@rule装饰器允许您设置一个字符串,bot将侦听并在遇到时触发相应的函数。因此,对于测验,我希望机器人通过说“Correctamundo!”来确认一个正确答案。当有人得到正确的问题时。
答:我试过的第一件事是嵌套函数。从q_and_as元组列表中选择一个随机问题后,它将回答(q [1])设置为应该触发正确()函数的规则。
from sopel.module import commands, rule
import random
q_and_as = [('Why?', 'because'), ('Can I kick it?', 'nope')]
@commands("quizme")
def ask_q(bot, trigger):
q = random.choice(q_and_as)
bot.say(q[0])
@rule(q[1])
def correct(bot, trigger):
bot.sat('Correctamundo!')
无论出于何种原因,回答在以这种方式完成时不会触发正确的()函数。
B。我还尝试将答案(q [1])传递给一个单独的答案函数,然后将其设置为触发正确函数的规则。
from sopel.module import commands, rule
import random
q_and_as = [('Why?', 'because'), ('Can I kick it?', 'nope')]
@commands("quizme")
def ask_q(bot, trigger):
q = random.choice(q_and_as)
bot.say(q[0])
answer(bot, trigger, q[1])
def answer(bot, trigger, answer):
@rule(answer)
def correct(bot, trigger):
bot.say(' correctamundo!')
虽然如此,函数并没有被触发。任何想法,我要去错了吗?或者有更好的方法来做到这一点?谢谢。