1

I would like to ask, how can I write the following as a module in Python.

    if message.content.startswith('!guess'):
        # Game Status updating
        now_playing = discord.Game(name='Guessing Game')
        await self.change_status(game=now_playing, idle=False)

        await self.send_message(message.channel, 'Guess a number between 1 to 10')

        def guess_check(m1):
            return m1.content.isdigit()

        guess = await self.wait_for_message(timeout=5.0, author=message.author, check=guess_check)
        answer = random.randint(1, 10)
        if guess is None:
            fmt = 'Sorry, you took too long. It was {}.'
            await self.send_message(message.channel, fmt.format(answer))
            return
        if int(guess.content) == answer:
            await self.send_message(message.channel, 'You are right!')
        else:
            await self.send_message(message.channel, 'Sorry. It is actually {}.'.format(answer))

        # Game Status updating
        now_playing = discord.Game(name='')
        await self.change_status(game=now_playing, idle=False)

So that I can call it by using say guessgame.guess()

lap00zza
  • 105
  • 1
  • 10
  • Possible duplicate of [How to write a Python module?](http://stackoverflow.com/questions/15746675/how-to-write-a-python-module) – GingerPlusPlus Jan 19 '16 at 19:53
  • @GingerPlusPlus the thing is I can write a simple module (forgive me I am very new). But when I tried to write a module for guessing game I came across a few error which I don't understand. – lap00zza Jan 19 '16 at 20:02
  • You're using `await`, so you have to define the function with `async def foo()`, more on that here https://www.python.org/dev/peps/pep-0492/#examples-of-await-expressions – bakkal Jan 19 '16 at 20:02

1 Answers1

5

Create a Python module file called guessgame.py then define in it:

"""
This is the module guessgame, it lives in the file guessgame.py
Put some documentation about your module here
"""
def guess(message):
   # Put your code here

Then from another module e.g. sample.py (or a Python/IPython shell session) you can do:

import guessgame
guessgame.guess(message='something')       # What you wanted

RuntimeWarning: coroutine 'guess' was never awaited guessgame.guess()

# 'await' can only be used inside a coroutine
# if you want guess to be a coroutine, define it like below
async def guess(message):
   # Put your code that uses await
   # Now you can use await expressions

Note:

  • You're using await, that takes some extra care, read more about those here: https://www.python.org/dev/peps/pep-0492/#examples-of-await-expressions
  • Don't redefine guess name in your guessname.guess() code, because your function is already called guess, you will be shawdowing it if you redefine guess = ...
  • Make sure your guessgame.guess() is passed all the required parameters, just for illustration purpose I've included only one parameter message
  • I see you use self in that code which suggests this should be a method of a class rather than a standalone function? Just something to keep in mind!
bakkal
  • 54,350
  • 12
  • 131
  • 107
  • I get this error :- `C:/Users/dell/Desktop/Python Projects/lapzbot/bot/lapzbot.py:208: RuntimeWarning: coroutine 'guess' was never awaited guessgame.guess()` – lap00zza Jan 19 '16 at 19:57
  • thank you for taking the time to help me out. I have added the full code here http://pastebin.com/NbxasMDD . Hope you can look at it once. Ty so much again @bakkal – lap00zza Jan 19 '16 at 20:05
  • No problem glad I could help! Here's what I would do. I would create a blank Python module of that code/classes etc and another module that uses it. Then I would _slowly_ move the copy/paste the code into the module, deal with problems one by one. Repeat until the whole program is inplace and runs. – bakkal Jan 19 '16 at 20:09