0
votes

I want to make a discord bot in python command that starts a game in where u have to write a given word and stops once a user spells it correctly, but return won t work in my case

async def joc(ctx):

     a=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
     '0','1','2','3','4','5','6','7','8','9']

     cuv=random.sample(a,10)
     cuvant=''
     for x in cuv:
         cuvant+=x

     await ctx.send('The game begins in: ')
     await ctx.send('3')
     time.sleep(1)
     await ctx.send('2')
     time.sleep(1)
     await ctx.send('1')
     time.sleep(1)
     await ctx.send('Start!')
     await ctx.send(cuvant)



     @bot.event
     async def on_message(message):
         if message.content==cuvant:
             await message.channel.send(message.author)
             await message.channel.send("Nice")
             return 
     return

I can do this with accesing the history but i want to know how to do it like this

3
if you want to stop the program, then use exit() to quit, if you want it to repeat again, then you can write all of your working code inside a while True: loop.shakhyar.codes
i want to only quit this function and if they call it again execute it againwill
then use a break at the endshakhyar.codes

3 Answers

1
votes

You're not supposed to put an event inside of a command, it doesn't work like that. What you're looking for is wait_for, an example of how to use it is included in the link. Basically you create a check to see if the message is in a certain format or meets your requirements, in your case to check whether or not the message is the right word.

1
votes

OH i also had a same trouble

fix by adding this to last line of on_message()

await client.process_commands(message)
# Replace the client with own bot or something that you have started with
0
votes

Here is the revised code with wait_for as stijndcl told

import asyncio
async def joc(ctx):

     a=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
     '0','1','2','3','4','5','6','7','8','9']

     cuv=random.sample(a,10)
     cuvant=''
     for x in cuv:
         cuvant+=x

     await ctx.send('The game begins in: ')
     await ctx.send('3')
     await asyncio.sleep(1)
     await ctx.send('2')
     await asyncio.sleep(1)
     await ctx.send('1')
     await asyncio.sleep(1)
     await ctx.send('Start!')
     await ctx.send(cuvant)
     content = ""
     while content != cuvant:
          msg = await bot.wait_for("message", check=lambda m: m.author == ctx.author and m.channel.id == ctx.channel.id)
          content = msg.content
     await ctx.send(message.author)
     await ctx.send(cuvant)