def test_update_slash_commands(): app = Flask(__name__) app.config["DONT_VALIDATE_SIGNATURE"] = True app.config["DONT_REGISTER_WITH_DISCORD"] = True discord = DiscordInteractions(app) with pytest.deprecated_call(): discord.update_slash_commands()
def test_register_options(): app = Flask(__name__) app.config["DONT_VALIDATE_SIGNATURE"] = True app.config["DONT_REGISTER_WITH_DISCORD"] = True discord = DiscordInteractions(app) @discord.command() def ping(ctx, option1: str, option2: float, option3: str = ""): return f"pong" discord.update_commands()
def test_register_subcommand(): app = Flask(__name__) app.config["DONT_VALIDATE_SIGNATURE"] = True app.config["DONT_REGISTER_WITH_DISCORD"] = True discord = DiscordInteractions(app) group = discord.command_group("group") @group.command() def subcommand(ctx): return "pong" discord.update_commands()
def quart_discord(): app = Quart(__name__) app.config["DONT_VALIDATE_SIGNATURE"] = True app.config["DONT_REGISTER_WITH_DISCORD"] = True discord = DiscordInteractions(app) return discord, Client(discord)
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!"
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!"
def test_register_message_command(): app = Flask(__name__) app.config["DONT_VALIDATE_SIGNATURE"] = True app.config["DONT_REGISTER_WITH_DISCORD"] = True discord = DiscordInteractions(app) @discord.command(type=ApplicationCommandType.MESSAGE) def ping(ctx): return "pong" @discord.command(type=ApplicationCommandType.MESSAGE) def PING(ctx): return "pong" @discord.command(name="user test", type=ApplicationCommandType.MESSAGE) def ping(ctx): return "pong" discord.update_commands()
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()
import os import sys from flask import Flask sys.path.insert(1, ".") from flask_discord_interactions import DiscordInteractions from flask_discord_interactions.context import ApplicationCommandType app = Flask(__name__) 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`)
def discord(): app = Flask(__name__) return DiscordInteractions(app)
import os import sys from flask import Flask sys.path.insert(1, ".") from flask_discord_interactions import DiscordInteractions, Autocomplete app = Flask(__name__) 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() @discord.command() def autocomplete_example(ctx, country: Autocomplete(str), city: Autocomplete(str)): return f"You selected **{city}, {country}**!" COUNTRIES = ["Germany", "Canada", "United States", "United Kingdom"] CITIES = { "Germany": ["Berlin", "Munich", "Frankfurt"], "Canada": ["Toronto", "Montreal", "Vancouver"], "United States": ["New York", "Chicago", "Los Angeles"], "United Kingdom": ["London", "Manchester", "Liverpool"],
import os import sys from flask import Flask sys.path.insert(1, ".") from flask_discord_interactions import DiscordInteractions, Permission app = Flask(__name__) 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() @discord.command(default_permission=False, permissions=[Permission(role="786840072891662336")]) def command_with_perms(ctx): "You need a certain role to access this command" return "You have permissions!" @discord.command(default_permission=False) def locked_command(ctx): "Secret command that has to be unlocked"
import os import sys from flask import Flask, send_file sys.path.insert(1, ".") from flask_discord_interactions import DiscordInteractions # noqa: E402 app = Flask(__name__) 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() from echo import bp as echo_bp # noqa: E402 from reverse import bp as reverse_bp # noqa: E402 from subcommands import bp as subcommands_bp # noqa: E402 discord.register_blueprint(echo_bp) discord.register_blueprint(reverse_bp) discord.register_blueprint(subcommands_bp) discord.set_route("/interactions") discord.update_commands(guild_id=os.environ["TESTING_GUILD"]) # Normal Flask routes work too!
import os import sys import time import threading from flask import Flask sys.path.insert(1, ".") from flask_discord_interactions import DiscordInteractions, Message app = Flask(__name__) 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() # You can use a decorator syntax to define subcommands comic = discord.command_group("comic") @comic.command() def xkcd(ctx, number: int): return f"https://xkcd.com/{number}/" @comic.command() def homestuck(ctx, number: int):
import os from flask import Flask, redirect from flask_discord_interactions import DiscordInteractions import reddit import card app = Flask(__name__) 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.command() def ping(ctx): "Respond with a friendly 'pong'!" return "Pong!" discord.set_route("/interactions") @app.route("/") def index(): return redirect(os.environ["OAUTH_URL"]) discord.register_blueprint(reddit.bp)
import asyncio import threading import warnings import aiohttp from quart import Quart sys.path.insert(1, ".") import quart.flask_patch from flask_discord_interactions import DiscordInteractions, Message warnings.simplefilter("always", DeprecationWarning) app = Quart(__name__) 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() # You can now use async functions! @discord.command() async def ping(ctx): "Respond with a friendly 'pong'!" return "Pong!"
# to make sure that the imports work # (you don't actually need it in your code) sys.path.insert(1, ".") from flask_discord_interactions import ( DiscordInteractions, Client, Message, CommandOptionType, Context, Member, ) app = Flask(__name__) discord = DiscordInteractions(app) test_client = Client(discord) # Simplest type of command: respond with a string @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"))
-c examples/multiworker/automatic_conf.py \\ examples.multiworker.automatic:app """ import os import sys from flask import Flask sys.path.insert(1, ".") from flask_discord_interactions import DiscordInteractions # noqa: E402 app = Flask(__name__) 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.command() def multiworkerping(ctx): "Respond with a friendly 'pong', served by one of many worker threads!" return "Pong!" discord.set_route("/interactions") if __name__ == "__main__":