Exemple #1
0
async def test_full_server(mock_flask):
    from flask_discord_interactions import (
        DiscordInteractions,
        InteractionType,
        ResponseType,
    )

    app = Quart("test_quart")
    app.config["DONT_VALIDATE_SIGNATURE"] = True
    app.config["DONT_REGISTER_WITH_DISCORD"] = True

    discord = DiscordInteractions(app)

    @discord.command()
    async def wait(ctx):
        await asyncio.sleep(0.01)
        return "Hi!"

    discord.set_route_async("/interactions")

    client = app.test_client()

    response = await client.post(
        "/interactions",
        json={
            "type": InteractionType.APPLICATION_COMMAND,
            "id": 1,
            "channel_id": "",
            "guild_id": "",
            "token": "",
            "data": {
                "id": 1,
                "name": "wait"
            },
        },
    )

    assert response.status_code == 200

    json = await response.get_json()

    assert json["type"] == ResponseType.CHANNEL_MESSAGE_WITH_SOURCE

    assert json["data"]["content"] == "Hi!"
Exemple #2
0
        await asyncio.sleep(seconds)
        ctx.edit("Done!")  # We are passed a Context, not an AsyncContext

    asyncio.create_task(do_followup())
    return Message(deferred=True)


# Async subcommands also work, and they can access context
toplevel = discord.command_group("toplevel", is_async=True)
secondlevel = toplevel.subgroup("secondlevel", is_async=True)


@secondlevel.command()
async def thirdlevel(ctx):
    async def do_followup():
        print(type(ctx))
        await asyncio.sleep(1)
        await ctx.edit(f"Hello, {ctx.author.display_name}!")

    asyncio.create_task(do_followup())
    return Message(deferred=True)


# Use set_route_async if you want to use Quart
discord.set_route_async("/interactions")

discord.update_commands(guild_id=os.environ["TESTING_GUILD"])

if __name__ == "__main__":
    app.run()