Yacine Larbi
Yacine Larbi

Reputation: 1

Discord bot looping forever

So I'm making a discord bot using Python, and I want to make it loop until some conditions are fulfilled, this is a simple example just to make what I'm trying to do clear.

@bot.command(name='join')
async def Yacine(ctx):
user = bot.get_user(ID)
await user.send("What is your name ?")
@bot.event
async def on_message(message):
    if message.content == "Yacine":
        user = bot.get_user(ID)
        await user.send("Great name")

So after it answers me ("Great name") I want it to ask again, from the top, when I tried

@bot.command(name='join')
async def Yacine(ctx):
user = bot.get_user(ID)
await user.send("What is your name ?")
@bot.event
async def on_message(message):
    if message.content == "Yacine":
        user = bot.get_user(ID)
        await user.send("Great name")
Yacine(ctx)

it keeps asking forever ("what is your name ?") even before I answer. Hope you can help me with this.

Upvotes: 0

Views: 794

Answers (2)

DilIsPickle
DilIsPickle

Reputation: 113

You’re over complicating it. Do this:

@bot.command(name='join')
async def Yacine(ctx):
    user = bot.get_user(ID)
    await user.send("What is your name ?")
    msg = await client.wait_for('message', check=lambda message: message.author == ctx.author)
    if “Yacine” in msg.content:
        await user.send(“Great name”)

That’s generally it, wrote this on my phone so may be a couple syntax problems, but nothing major.

Upvotes: 1

Scobra Scope
Scobra Scope

Reputation: 29

you shoud be able to fix it by adding a return or incase of a loop, a break

async def Yacine(ctx):
user = bot.get_user(ID)
await user.send("What is your name ?")
@bot.event
async def on_message(message):
    if message.content == "Yacine":
        user = bot.get_user(ID)
        await user.send("Great name")
        return
Yacine(ctx)

Upvotes: 0

Related Questions