discord-serverless


Namediscord-serverless JSON
Version 2023.1005a0 PyPI version JSON
download
home_page
SummarySimplified serverless Discord bot interactions
upload_time2023-08-02 18:18:53
maintainer
docs_urlNone
author
requires_python>=3.10
licenseCopyright 2023 Ross Bamford & Contributors Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords feed reader tutorial
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            ## Utility library for serverless Discord apps

![PyPI](https://img.shields.io/pypi/v/discord-serverless)

This is a (currently very simple) library that eases development of serverless Discord
app and bots.

Currently, it targets AWS Lambda only, and provides a decorator and some utility
functions that make discord signature verification and ping handling automatic.

I will grow it with things I end up needing in my work, but if you have different
needs and want to extend the library to support them, then PRs would be gratefully
received!

### MVP Example

This will accept any slash command you've configured for your bot, and just greet 
the user who calls it, tagging them in the reply. 

```python
from discord_serverless import discord_command_lambda, discord_command_response

@discord_command_lambda(discord_key=YOUR_PUBLIC_KEY)
def command_handler(payload):
    return discord_command_response(4, f'Hi there <@{payload["member"]["user"]["id"]}>')
```

### General usage

#### AWS (Tested)

> **Note**: If using with API Gateway, you'll need to set up proxy integration.
> Failing to do this will likely lead to none of the requests from Discord passing
> signature verification, which is based on the raw original payload.
> 
> This _shouldn't_ be a problem if you're using function URLs (but I still don't recommend
> that for reasons that are way beyond the scope of this README 😅).

For AWS, simply create a handler function that takes a single `payload` argument,
and decorate it with `discord_command_lambda` decorator. You need to pass in the
Discord public key for your application, e.g:

```python
@discord_command_lambda(discord_key=YOUR_PUBLIC_KEY)
def command_handler(payload):
    # ... do stuff ...
```

This function should return a regular AWS response (compatible with your chosen 
integration) with the payload format outlined in the Discord interaction webhook
docs.

The payload that gets passed in is the standard Discord interaction payload.

To check the actual slash command that was called (in case you have a single bot 
doing multiple commands), look in `payload["data"]["name"]`.

If your command supports options, you'll find them in a list of dicts under
`payload["data"]["options"]` with entries like:

```python
{
 "name": "the option name",
 "value": "the value, appropriately typed" 
}
```

See [Example Interaction](https://discord.com/developers/docs/interactions/application-commands#slash-commands-example-interaction)
in the Discord docs for full layout.

There are a couple of helper functions for responses:

* `discord_command_response` - Create a regular success response. Response type `4`
  will send the `content` you supply as the reply message to the user.
* `discord_unknown_command_response` - Create an error response. This will get sent
  as a HTTP 400, and will cause discord to display "The interaction failed" to the
  the user (but _only_ the user - not the rest of the channel).

For help with the interaction response types, see [Responding to an interaction](https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction)
in the Discord docs.

#### Other Cloud Providers

Things will be a bit more manual here (but I'll happily add comfort wrappers if
there's enough call and a PR that contributes them 😉).

The easiest thing to do is use the `discord_command_webhook` decorator, which 
is similar to the `lambda` one above but needs you pass in a couple of `Callable`s
the code will use to get access to the data in a provider-specific way.

You could also go full manual mode and directly call `discord_verify_signature` and
`handle_discord_command`, passing things in as appropriate - this is how the decorator
works under the hood and gives you full flexibility if that's your thing.

Take a look at the code in `discord_serverless.py` for examples to get you started.

### Developing

Don't forget to set up `pre-commit` if you're developing things, especially if you
plan to push a PR.

### Legal Mumbo-Jumbo

Copyright (c)2023 Ross Bamford (and contributors)

License: MIT (see `LICENSE.md` for details).

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "discord-serverless",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10",
    "maintainer_email": "",
    "keywords": "feed,reader,tutorial",
    "author": "",
    "author_email": "Ross Bamford <roscopeco@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/11/3e/811ec19fa2c3b96a13b3307b7f1d670d0e2ad01e2f9eac1ebd8456d6e18c/discord-serverless-2023.1005a0.tar.gz",
    "platform": null,
    "description": "## Utility library for serverless Discord apps\n\n![PyPI](https://img.shields.io/pypi/v/discord-serverless)\n\nThis is a (currently very simple) library that eases development of serverless Discord\napp and bots.\n\nCurrently, it targets AWS Lambda only, and provides a decorator and some utility\nfunctions that make discord signature verification and ping handling automatic.\n\nI will grow it with things I end up needing in my work, but if you have different\nneeds and want to extend the library to support them, then PRs would be gratefully\nreceived!\n\n### MVP Example\n\nThis will accept any slash command you've configured for your bot, and just greet \nthe user who calls it, tagging them in the reply. \n\n```python\nfrom discord_serverless import discord_command_lambda, discord_command_response\n\n@discord_command_lambda(discord_key=YOUR_PUBLIC_KEY)\ndef command_handler(payload):\n    return discord_command_response(4, f'Hi there <@{payload[\"member\"][\"user\"][\"id\"]}>')\n```\n\n### General usage\n\n#### AWS (Tested)\n\n> **Note**: If using with API Gateway, you'll need to set up proxy integration.\n> Failing to do this will likely lead to none of the requests from Discord passing\n> signature verification, which is based on the raw original payload.\n> \n> This _shouldn't_ be a problem if you're using function URLs (but I still don't recommend\n> that for reasons that are way beyond the scope of this README \ud83d\ude05).\n\nFor AWS, simply create a handler function that takes a single `payload` argument,\nand decorate it with `discord_command_lambda` decorator. You need to pass in the\nDiscord public key for your application, e.g:\n\n```python\n@discord_command_lambda(discord_key=YOUR_PUBLIC_KEY)\ndef command_handler(payload):\n    # ... do stuff ...\n```\n\nThis function should return a regular AWS response (compatible with your chosen \nintegration) with the payload format outlined in the Discord interaction webhook\ndocs.\n\nThe payload that gets passed in is the standard Discord interaction payload.\n\nTo check the actual slash command that was called (in case you have a single bot \ndoing multiple commands), look in `payload[\"data\"][\"name\"]`.\n\nIf your command supports options, you'll find them in a list of dicts under\n`payload[\"data\"][\"options\"]` with entries like:\n\n```python\n{\n \"name\": \"the option name\",\n \"value\": \"the value, appropriately typed\" \n}\n```\n\nSee [Example Interaction](https://discord.com/developers/docs/interactions/application-commands#slash-commands-example-interaction)\nin the Discord docs for full layout.\n\nThere are a couple of helper functions for responses:\n\n* `discord_command_response` - Create a regular success response. Response type `4`\n  will send the `content` you supply as the reply message to the user.\n* `discord_unknown_command_response` - Create an error response. This will get sent\n  as a HTTP 400, and will cause discord to display \"The interaction failed\" to the\n  the user (but _only_ the user - not the rest of the channel).\n\nFor help with the interaction response types, see [Responding to an interaction](https://discord.com/developers/docs/interactions/receiving-and-responding#responding-to-an-interaction)\nin the Discord docs.\n\n#### Other Cloud Providers\n\nThings will be a bit more manual here (but I'll happily add comfort wrappers if\nthere's enough call and a PR that contributes them \ud83d\ude09).\n\nThe easiest thing to do is use the `discord_command_webhook` decorator, which \nis similar to the `lambda` one above but needs you pass in a couple of `Callable`s\nthe code will use to get access to the data in a provider-specific way.\n\nYou could also go full manual mode and directly call `discord_verify_signature` and\n`handle_discord_command`, passing things in as appropriate - this is how the decorator\nworks under the hood and gives you full flexibility if that's your thing.\n\nTake a look at the code in `discord_serverless.py` for examples to get you started.\n\n### Developing\n\nDon't forget to set up `pre-commit` if you're developing things, especially if you\nplan to push a PR.\n\n### Legal Mumbo-Jumbo\n\nCopyright (c)2023 Ross Bamford (and contributors)\n\nLicense: MIT (see `LICENSE.md` for details).\n",
    "bugtrack_url": null,
    "license": "Copyright 2023 Ross Bamford & Contributors  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \u201cSoftware\u201d), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \u201cAS IS\u201d, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
    "summary": "Simplified serverless Discord bot interactions",
    "version": "2023.1005a0",
    "project_urls": {
        "Homepage": "https://github.com/roscopeco/discord-serverless"
    },
    "split_keywords": [
        "feed",
        "reader",
        "tutorial"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "43d217442cde0c1031745c79e6de36911ec78953df14333ba230f19a43bdeffd",
                "md5": "262ef0ecabd99598b5c984ebea7e30aa",
                "sha256": "9e684ad91c916d21694c9e50022ded211737c73859414816b0b35c69efbe7385"
            },
            "downloads": -1,
            "filename": "discord_serverless-2023.1005a0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "262ef0ecabd99598b5c984ebea7e30aa",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10",
            "size": 6881,
            "upload_time": "2023-08-02T18:18:51",
            "upload_time_iso_8601": "2023-08-02T18:18:51.529381Z",
            "url": "https://files.pythonhosted.org/packages/43/d2/17442cde0c1031745c79e6de36911ec78953df14333ba230f19a43bdeffd/discord_serverless-2023.1005a0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "113e811ec19fa2c3b96a13b3307b7f1d670d0e2ad01e2f9eac1ebd8456d6e18c",
                "md5": "faec790375fdd410b23f14fcd694d9dc",
                "sha256": "e8e544824dd31e4a755a4f2bf2106a83a7c7fbcb423b3036e1d2661fd632d832"
            },
            "downloads": -1,
            "filename": "discord-serverless-2023.1005a0.tar.gz",
            "has_sig": false,
            "md5_digest": "faec790375fdd410b23f14fcd694d9dc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10",
            "size": 6078,
            "upload_time": "2023-08-02T18:18:53",
            "upload_time_iso_8601": "2023-08-02T18:18:53.187996Z",
            "url": "https://files.pythonhosted.org/packages/11/3e/811ec19fa2c3b96a13b3307b7f1d670d0e2ad01e2f9eac1ebd8456d6e18c/discord-serverless-2023.1005a0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-02 18:18:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "roscopeco",
    "github_project": "discord-serverless",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "discord-serverless"
}
        
Elapsed time: 0.09776s