Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Followup messages after delay #32

Closed
cgeopapa opened this issue Mar 28, 2021 · 6 comments
Closed

Followup messages after delay #32

cgeopapa opened this issue Mar 28, 2021 · 6 comments

Comments

@cgeopapa
Copy link

What The command I want to add to my bot goes somewhat like this:

  1. The bot receives the the request to do whatever it has to do (so far so good)
  2. It now checks something on my remote server and chooses whether it will do action A or B
  3. Once it has chosen I want it to inform the user on its choice.
  4. Do action A or B
  5. Send a message that the action is done

Of course there will be a delay between steps 3 and 5, until step 4 is done. If I use followup messages it just waits until step 4 is done and then sends all messages at the same time.
Here is a sample code where I use time.sleep for testing:

@bot.interaction.on("ping")
async def ping(ctx: IncomingDiscordInteraction) -> DiscordResponse:
    response = DiscordResponse(content="pong")
    loop = asyncio.get_event_loop()
    loop.create_task(follow(ctx))

    return response


async def follow(ctx: IncomingDiscordInteraction):
    time.sleep(2)
    followup = FollowUpMessages(bot=bot, interaction=ctx)
    responseF = DiscordResponse(follow_up_message=False)
    responseF.content = "pong follow up"
    await followup.async_create_follow_up_message(responseF)

I'm pretty sure I am doing something wrong.I am very unfamiliar with asyncio and event loops. Any help with that?

@ms7m
Copy link
Owner

ms7m commented Mar 29, 2021

You're on the right track.

There is an helper method available with the Dispike object called .background. Based on your steps, you could probably have something like this.

async def pong(ctx: IncomingDiscordInteraction):
    
    followup_message = FollowUpMessages(bot, interaction=ctx)
    await asyncio.sleep(2)

    response_followup = DiscordResponse(content="pong")
    await follow_up_message.async_create_follow_up_message(response_followup)


@bot.interaction.on("ping")
async def ping(ctx: IncomingDiscordInteraction) -> DiscordResponse:
    response = DiscordResponse(content="pong")


    # background 
    bot.background(pong, ctx)

    return response

I wouldn't use time for anything in an async function, but if you could probably use it if you replace async_create_follow_up_message with sync_create_follow_up_message.

I should write some documentation about this..

@cgeopapa
Copy link
Author

bot.background still requires to be awaited, and if I await it I just get the same result.

@ms7m
Copy link
Owner

ms7m commented Mar 29, 2021

Ah, I see what you mean. I'll push an update to fix this behavior for .background..

ms7m added a commit that referenced this issue Mar 29, 2021
@ms7m ms7m mentioned this issue Mar 29, 2021
@ms7m
Copy link
Owner

ms7m commented Mar 29, 2021

Just pushed version 0.9.0a..

Testing with this example

async def later_pong(time_recieved, interaction: IncomingDiscordInteraction):
    now_time = datetime.datetime.now()
    time_diff = (now_time - time_recieved).total_seconds() * 1000

    _followup_object = FollowUpMessages(bot, interaction=interaction)
    await asyncio.sleep(10)

    pong_message  = DiscordResponse(show_user_input=False)
    pong_message.content = f"Pong: {time_diff}ms"
    await _followup_object.async_create_follow_up_message(pong_message)


@bot.interaction.on("ping")
async def ping(ctx: IncomingDiscordInteraction) -> DiscordResponse:
    await bot.background(later_pong, datetime.datetime.now(), ctx)

    logger.info("sending response ?")
    return DiscordResponse(
        content="Ping: ..."
    )

Let me know if you works out for you

@cgeopapa
Copy link
Author

There we go! Thank you!
Is there any discord server where I can join and get involved with this project? I see a lot of potential here.

@ms7m
Copy link
Owner

ms7m commented Mar 30, 2021

No problem!

There isn't a discord server yet. That sounds like a good idea for the future :)

@EthanC EthanC mentioned this issue Jul 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants