Ejemplo n.º 1
0
def test_flask():
    app = Flask(__name__)
    app.config["DONT_VALIDATE_SIGNATURE"] = True
    app.config["DONT_REGISTER_WITH_DISCORD"] = True

    discord = DiscordInteractions(app)

    @discord.command()
    def ping(ctx, pong: str = "ping"):
        return f"Ping {pong}!"

    discord.set_route("/interactions")

    with app.test_client() as client:
        response = client.post(
            "/interactions",
            json={
                "type": InteractionType.APPLICATION_COMMAND,
                "id": 1,
                "channel_id": "",
                "guild_id": "",
                "token": "",
                "data": {
                    "id": 1,
                    "name": "ping",
                    "options": [{
                        "type": 1,
                        "name": "Pong"
                    }],
                },
                "member": {
                    "id": 1,
                    "nick": "",
                    "user": {
                        "id": 1,
                        "username": "******"
                    }
                },
            },
        )

        assert response.status_code == 200
        assert response.get_json(
        )["type"] == ResponseType.CHANNEL_MESSAGE_WITH_SOURCE

        assert response.get_json()["data"]["content"] == "Ping Pong!"
Ejemplo n.º 2
0
def test_followup():
    app = Flask(__name__)
    app.config["DONT_VALIDATE_SIGNATURE"] = True
    app.config["DONT_REGISTER_WITH_DISCORD"] = True

    discord = DiscordInteractions(app)

    ref_to_thread = None

    @discord.command()
    def ping(ctx):
        nonlocal ref_to_thread

        def do_followup():
            ctx.edit("hi")

        ref_to_thread = thread = threading.Thread(target=do_followup)
        thread.start()

        return Message(deferred=True)

    discord.set_route("/interactions")

    with app.test_client() as client:
        response = client.post(
            "/interactions",
            json={
                "type": InteractionType.APPLICATION_COMMAND,
                "id": 1,
                "channel_id": "",
                "guild_id": "",
                "token": "",
                "data": {
                    "id": 1,
                    "name": "ping",
                },
            },
        )

    # Make sure we wait for the thread to complete
    ref_to_thread.join()
discord = DiscordInteractions(app)

app.config["DISCORD_CLIENT_ID"] = os.environ["DISCORD_CLIENT_ID"]
app.config["DISCORD_PUBLIC_KEY"] = os.environ["DISCORD_PUBLIC_KEY"]
app.config["DISCORD_CLIENT_SECRET"] = os.environ["DISCORD_CLIENT_SECRET"]

discord.update_commands()


# Simple command to mention a friend
# The target user is passed as an argument
# It is also accessible as `ctx.target`
@discord.command(name="High Five", type=ApplicationCommandType.USER)
def highFive(ctx, target):
    return f"<@{ctx.author.id}> wants to say hello to <@{target.id}>"


# Simple message command to repeat a message in bold
# The target message is passed as an argument (and as `ctx.target`)
@discord.command(name="Make it bold", type=ApplicationCommandType.MESSAGE)
def boldMessage(ctx, message):
    return f"**{message.content}**"


discord.set_route("/interactions")

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

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