Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Newsgroups: comp.lang.python Subject: How to use a variable to act as @rule in a Sopel IRC bot module? Date: Tue, 22 Dec 2015 17:40:41 +0000 (UTC) Lines: 47 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de ZSzDbzyJmHiOY/iWPsxM0w4bk+ZMI59rJIg0QNFSK3UQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'irc': 0.05; 'subject:How': 0.09; 'subject:module': 0.09; 'url:github': 0.09; 'python': 0.10; 'def': 0.13; "skip:' 30": 0.15; 'channel.': 0.16; 'encountered.': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:@ 20': 0.16; 'subject:variable': 0.16; 'trivia': 0.16; 'string': 0.17; 'nested': 0.18; 'saying': 0.22; 'commands,': 0.22; 'decorator': 0.22; 'function,': 0.22; 'tuples': 0.22; 'passing': 0.23; 'sets': 0.23; 'tried': 0.24; 'import': 0.24; 'right.': 0.27; 'separate': 0.27; 'question': 0.27; 'correct': 0.28; 'function': 0.28; 'random': 0.29; "i'm": 0.30; "i'd": 0.31; 'though,': 0.32; 'choosing': 0.33; 'everyone.': 0.33; 'lets': 0.33; 'rule': 0.33; 'this?': 0.34; 'that,': 0.34; 'gets': 0.35; 'skip:& 20': 0.35; 'done': 0.35; 'subject:use': 0.35; "isn't": 0.35; 'list,': 0.36; 'should': 0.36; 'there': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'being': 0.37; 'doing': 0.38; 'someone': 0.38; 'thank': 0.38; 'whatever': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'from:no real name:2**0': 0.60; 'confirm': 0.62; 'our': 0.64; 'you.': 0.64; 'bot': 0.84; 'quiz': 0.84; 'triggering': 0.84 X-Mailman-Approved-At: Tue, 22 Dec 2015 15:35:30 -0500 X-Content-Filtered-By: Mailman/MimeDel 2.1.20+ X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100744 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.