diff --git a/docs/options.rst b/docs/options.rst index d70e896..5044da1 100644 --- a/docs/options.rst +++ b/docs/options.rst @@ -26,8 +26,8 @@ for more information about the decorator. Primitives ---------- -You can use ``str``, ``int``, or ``bool`` for string, integer, and boolean -options. +You can use ``str``, ``int``, ``float``, or ``bool`` for string, integer, +number, and boolean options. .. code-block:: python diff --git a/examples/options.py b/examples/options.py index ded8503..16d1049 100644 --- a/examples/options.py +++ b/examples/options.py @@ -1,6 +1,7 @@ import os import sys import enum +import math from flask import Flask @@ -43,6 +44,11 @@ def and_gate(ctx, a: bool, b: bool): return f"{a} AND {b} is {a and b}" +@discord.command() +def sin(ctx, x: float): + return f"sin({x}) = {math.sin(x)}" + + # For using the "choices" field, you can use an Enum class Animal(enum.Enum): Dog = "dog" diff --git a/flask_discord_interactions/command.py b/flask_discord_interactions/command.py index e62c3d8..b37aca0 100644 --- a/flask_discord_interactions/command.py +++ b/flask_discord_interactions/command.py @@ -92,6 +92,8 @@ def __init__(self, command, name, description, options, annotations, ptype = CommandOptionType.BOOLEAN elif annotation == str: ptype = CommandOptionType.STRING + elif annotation == float: + ptype = CommandOptionType.NUMBER # Discord Models elif annotation in [User, Member]: diff --git a/flask_discord_interactions/context.py b/flask_discord_interactions/context.py index cadf4ad..db4b0c1 100644 --- a/flask_discord_interactions/context.py +++ b/flask_discord_interactions/context.py @@ -23,6 +23,8 @@ class CommandOptionType: USER = 6 CHANNEL = 7 ROLE = 8 + MENTIONABLE = 9 + NUMBER = 10 class ChannelType: @@ -667,4 +669,4 @@ async def overwrite_permissions(self, permissions, command=None): }) async def close(self): - await self.session.close() \ No newline at end of file + await self.session.close() diff --git a/flask_discord_interactions/tests/test_options.py b/flask_discord_interactions/tests/test_options.py index cb4537c..30a6973 100644 --- a/flask_discord_interactions/tests/test_options.py +++ b/flask_discord_interactions/tests/test_options.py @@ -28,6 +28,15 @@ def and_gate(ctx, a: bool, b: bool): assert client.run("and_gate", a=True, b=True).content == "True" +def test_number(discord, client): + @discord.command() + def round_to_nearest(ctx, number: float): + return str(round(number)) + + assert client.run("round_to_nearest", number=1.5).content == "2" + assert client.run("round_to_nearest", number=1.25).content == "1" + + def test_default(discord, client): @discord.command() def square(ctx, number: int = 5):