Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100744 > unrolled thread
| Started by | <arresteddevlopment@tuta.io> |
|---|---|
| First post | 2015-12-22 17:40 +0000 |
| Last post | 2015-12-22 17:40 +0000 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
How to use a variable to act as @rule in a Sopel IRC bot module? <arresteddevlopment@tuta.io> - 2015-12-22 17:40 +0000
| From | <arresteddevlopment@tuta.io> |
|---|---|
| Date | 2015-12-22 17:40 +0000 |
| Subject | How to use a variable to act as @rule in a Sopel IRC bot module? |
| Message-ID | <mailman.67.1450816532.2237.python-list@python.org> |
Hi everyone. I'm working with the Sopel (previously Willie and before that,
Jenni/Phenny) python IRC bot as I'd like to set up a trivia quiz for our IRC
channel.
With Sopel, the @rule decorator lets you set a string that the bot will
listen out for and which triggers a corresponding function when encountered.
So for the quiz I'd like the bot to confirm a correct answer by saying
"Correctamundo!" when someone gets the question right.
A. The first thing I tried was a nested function. After choosing a random
question from the q_and_as tuples list, it sets the answer (q[1]) as the rule
that should trigger the correct() function.
from sopel.module import commands, ruleimport randomq_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!')
For whatever reason the answer isn't triggering the correct() function when
done this way.
B. I also tried passing the answer (q[1]) to a separate answer function,
which would then set it as the rule that triggered the correct() function.
from sopel.module import commands, ruleimport randomq_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!')
Again though, the function isn't being triggered. Any ideas where I'm going
wrong? Or is there a better way of doing this? Thank you.
Back to top | Article view | comp.lang.python
csiph-web