Giter Site home page Giter Site logo

discord-interactions-python's People

Contributors

bennyscripts avatar creallfluharty avatar dependabot[bot] avatar izxxr avatar j-human avatar maskduck avatar trilleplay avatar typpo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

discord-interactions-python's Issues

Decorator triggers exception when receiving a request from a source other than Discord.

Description

Currently, if the method wrapped by the verify_key_decorator decorator receives a request that doesn't contain either of the X-Signature-* headers, or a body without the type key, it will result in an exception that will trigger a 500 error. This isn't particularly problematic given that requests from any source other than Discord should be rejected anyway, but it would be preferable to respond with a 403 error instead so that these errors are not included in the logs of the application.

Steps to reproduce

  1. Handle slash commands with something like the code included in the project's current README:
@verify_key_decorator(CLIENT_PUBLIC_KEY)
def interactions():
  if request.json['type'] == InteractionType.APPLICATION_COMMAND:
    return jsonify({
        'type': InteractionResponseType.CHANNEL_MESSAGE_WITH_SOURCE,
        'data': {
            'content': 'Hello world'
        }
    })
  1. Manually make a request to the bot's interactions endpoint (Using for example a web browser).
  2. Observe that a 500 error is produced.

Additional Details

In the case of the missing headers, the exception may look something like this (Though the traceback probably won't include the google cloud stuff):

Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 66, in view_func
    return function(request._get_current_object())
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/discord_interactions/__init__.py", line 44, in __decorator
    if not verify_key(request.data, signature, timestamp, client_public_key):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/discord_interactions/__init__.py", line 24, in verify_key
    message = timestamp.encode() + raw_body
AttributeError: 'NoneType' object has no attribute 'encode'

The NoneType in this case is the None returned by request.headers.get('X-Signature-Timestamp') since that header is not present in the request.

Add decorator to register/update slash commands with Discord?

Would it be in scope for this project to add a decorator to update Discord with changes to slash commands when the flask service is started/reloaded in debug mode for faster development? There would likely need to be some way to store bot token as authentication in the flask app, maybe a wrapper of some kind. as an example:

from discord_interactions import verify_key_decorator, SlashCommand

app = Flask(__name__)
sc = SlashCommand(app)
sc.token = environ["BOT_TOKEN"] # also retrieves application client ID
sc.guild_id = guild_id # optional, limits all commands on this service to a guild, would also be configurable from each decorator

def get_opts(name):
    with open("options.json") as f
        return json.load(f)[name]

@app.route("/", methods=["POST"])
@verify_key_decorator(PUBKEY) # could be implicit with inclusion of SlashCommand.command
@sc.command(name="watch", description=long_desc, options=get_opts("watch")) # , guild_id=guild_id)
def watch():
    pass

If this type of behaviour is not in scope I'll start up a separate project to provide this functionality.

AttributeError: 'tuple' object has no attribute 'get'

I'm trying to add my interactions endpoint url to my app in discord, using a python flask app.

I'm running the example in the README.md and there is a failure when verifying the content of the type :

x.x.x.x - - [26/Aug/2021 16:26:43] "POST /interactions HTTP/1.1" 401 -
[2021-08-26 16:26:43,604] ERROR in app: Exception on /interactions [POST]
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/usr/local/lib/python3.7/site-packages/discord_interactions/__init__.py", line 48, in __decorator
    if request.json and request.json.get('type') == InteractionType.PING:
AttributeError: 'tuple' object has no attribute 'get'
x.x.x.x - - [26/Aug/2021 16:26:43] "POST /interactions HTTP/1.1" 500 -

How can I add my interactions endpoint url if discord can't verify it ?

Tested with Python 3.7 and 3.8.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.