Client

The Client class can simulate calling a Command without connecting to Discord. This is useful for unit testing.

The Client must be initialized by passing a DiscordInteractions object:

from flask import Flask
from flask_discord_interactions import DiscordInteractions, Client


app = Flask(__name__)
discord = DiscordInteractions(app)

test_client = Client(discord)

From here, the Client can be used to simulate calling commands. Parameters can be passed in as keyword arguments.

@discord.command()
def ping(ctx, pong: str = "pong"):
    "Respond with a friendly 'pong'!"
    return f"Pong {pong}!"


print(test_client.run("ping"))
print(test_client.run("ping", pong="ping"))

Groups can be called by specifying additional positional arguments.

groupy = discord.command_group("groupy")

@groupy.command()
def group(ctx, embed: bool):
    if embed:
        return Message(embed={"title": "Groupy group"})
    else:
        return "Groupy group"


print(test_client.run("groupy", "group", True))
print(test_client.run("groupy", "group", False))

It’s possible to pass in a Member, Channel, or Role object as a parameter as well.

@discord.command()
def channel_name(ctx, channel: Channel):
    return channel.name

client.run("channel_name", channel=Channel(name="general"))

Finally, you can use the Client.context() context manager to specify the context of the interaction.

@discord.command()
def uses_context(ctx):
    return f"Your name is {ctx.author.display_name}"

context = Context(author=Member(username="Bob"))

with test_client.context(context):
    print(test_client.run("uses_context"))

Full API

class flask_discord_interactions.Client(discord)

A class to represent a mock client that can be used to execute Application Commands programatically without connecting to Discord.

discord

The DiscordInteractions object to connect to.

context(context=None, **kwargs)

Uses the specified context object for commands run. This is a context manager meant to be used with the with statement.

Parameters:
  • context – The Context object to use.

  • **kwargs – Keyword arguments to use to construct the Context object.

run(*names, **params)

Run a specified Application Command.

Parameters:
  • *names – The names of the command, subcommand group, and subcommand (if present). This may contain just one name (in the case of a root-level command), or up to three (for a subcommand inside a subcommand group).

  • **params – Options to pass to the command being called.

run_handler(custom_id: str, *args)

Run a specified custom ID handler.

Parameters:
  • custom_id – The ID of the handler function to run.

  • *args – Options to pass to the handler being called.