Skip to content

Commit

Permalink
Merge pull request #38 from williamhatcher/patch-1: Added support for…
Browse files Browse the repository at this point in the history
… new option types
  • Loading branch information
breqdev authored Aug 11, 2021
2 parents 666bb7a + 99fb9b6 commit 84e45b5
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions examples/options.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import sys
import enum
import math

from flask import Flask

Expand Down Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions flask_discord_interactions/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
4 changes: 3 additions & 1 deletion flask_discord_interactions/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class CommandOptionType:
USER = 6
CHANNEL = 7
ROLE = 8
MENTIONABLE = 9
NUMBER = 10


class ChannelType:
Expand Down Expand Up @@ -667,4 +669,4 @@ async def overwrite_permissions(self, permissions, command=None):
})

async def close(self):
await self.session.close()
await self.session.close()
9 changes: 9 additions & 0 deletions flask_discord_interactions/tests/test_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 84e45b5

Please sign in to comment.