This is a very basic Discord Interaction designed to run on Azure Function Apps using the Python runtime.
- Head to Discord and create a new application, take note of the Client ID, Client Secret and Interactions Public Key.
- Create a new Azure function app with the code in
azure_function.py
, make sure to add an app setting calledINTERACTION_PUBLIC_KEY
with the value taken from Discord. Make sure to adddiscord_interactions
to yourrequirements.txt
file, the function depends on it! - Place the URL of your App Function HTTP trigger into the Discord Developer portal. If things are working it will allow you to save.
- Add the application to your server by visiting the OAuth2 URL generator in the Developer portal and creating a link with the
application.commands
scope. - Run
client_credentials.py
and input your Client ID and Secret, take note of the returned access token in theaccess_token
field. - Run the
register_command.py
script with the token fetched through the client credentials script. - Run the
/dice
command in your Discord server!
At some point you'll want to have the commands appear by default in your users Discord servers without having to manually add them to the guild. To do this swap the URL in register_command.py
for one that looks like the following:
url = f"https://discord.com/api/v8/applications/{APP_ID}/commands"
After you run the script once more the new command will begin to propagate across your applications Discord servers over a period of an hour.
All code in this project is licensed under MIT. The full license can be found in the file named LICENSE
.
Hey @jsleep 🙂!
Interesting, nice spot, updated now.
This was always the slight problem, the Functions team took a look at it but sadly Discord has something like a 5 second response timeout and for cold-boots of Python functions it just wasn't performant enough. If the worker was already booted and responded then it was generally fine and would work on, say, the next call, but the initial often timed out if the function went to sleep.
Cloudflare solve this with Workers via the v8 hot-starts by removing the need to boot a runtime, a few other serverless things based on v8 do this, but obviously no nice solution for Azure Functions w/ Python.
There's a chance that using the pre-warming features of Functions premium resolves this, I haven't tested that since it was added to the product: https://learn.microsoft.com/en-us/azure/azure-functions/functions-premium-plan?tabs=portal#eliminate-cold-starts
Obviously, that does come at an increased cost compared to other serverless offerings (and eventually, compared to just using a compute instance/app service and running it there)