Ejemplo n.º 1
0
def test_author(discord, client):
    @discord.command()
    def display_name(ctx):
        return ctx.author.display_name

    with client.context(Context(author=Member(username="******"))):
        assert client.run("display_name").content == "Bob"

    with client.context(Context(author=Member(nick="Dale"))):
        assert client.run("display_name").content == "Dale"
def test_member(discord, client):
    @discord.command()
    def ship_them(ctx, other: Member):
        return f"{ctx.author.display_name} <3 {other.display_name}"

    with client.context(author=Member(username="******")):
        assert (client.run(
            "ship_them",
            other=Member(username="******")).content == "Romeo <3 Juliet")

        assert (client.run("ship_them",
                           other=Member(username="******",
                                        nick="J")).content == "Romeo <3 J")
Ejemplo n.º 3
0
def test_permissions(discord, client):
    @discord.command()
    def is_admin(ctx):
        return "Yes" if (ctx.author.permissions & 8) else "No"

    context = Context(author=Member(permissions="0"))

    with client.context(context):
        assert client.run("is_admin").content == "No"

    context = Context(author=Member(permissions="8"))

    with client.context(context):
        assert client.run("is_admin").content == "Yes"
Ejemplo n.º 4
0
def test_user_command_argument(discord, client):
    @discord.command(type=ApplicationCommandType.MESSAGE)
    def greet(ctx, target):
        return f"Hello, {target.display_name}!"

    with client.context(Context(target=Member(username="******"))):
        assert client.run("greet").content == "Hello, Test User!"
def test_multiple(discord, client):
    @discord.command()
    def has_role(ctx, role: Role, user: Member = None):
        if user is None:
            user = ctx.author

        return str(role.id in user.roles)

    with client.context(author=Member(roles=["2", "4", "8", "16"])):
        assert client.run("has_role", role=Role(id="2")).content == "True"
        assert client.run("has_role", role=Role(id="7")).content == "False"

        user = Member(roles=["7"])
        assert client.run("has_role", user=user,
                          role=Role(id="2")).content == "False"
        assert client.run("has_role", user=user,
                          role=Role(id="7")).content == "True"
Ejemplo n.º 6
0
def test_ids(discord, client):
    @discord.command()
    def all_ids(ctx):
        return f"{ctx.author.id}/{ctx.channel_id}/{ctx.guild_id}"

    context = Context(author=Member(id="01"), channel_id="02", guild_id="2003")

    with client.context(context):
        assert client.run("all_ids").content == "01/02/2003"
Ejemplo n.º 7
0
def test_context_with_subcommand(discord, client):
    group = discord.command_group("group")

    @group.command()
    def subcommand(ctx):
        return ctx.author.display_name

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

    with client.context(context):
        assert client.run("group", "subcommand").content == "Bob"
Ejemplo n.º 8
0
        return "Groupy group"


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


@discord.command(
    options=[
        {"type": CommandOptionType.SUB_COMMAND, "name": "subcommand_1"},
        {"type": CommandOptionType.SUB_COMMAND, "name": "subcommand_2"},
    ]
)
def manual_style_group(ctx, sub):
    return f"subcommand is {sub}"


print(test_client.run("manual_style_group", "subcommand_1"))
print(test_client.run("manual_style_group", "subcommand_2"))


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


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

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