supabase/functions/truffle-discord-bot-demo
is an example of a Supabase Edge
Function written in Deno that will:
- Use Discord slash commands and the Truffle API to display user data
To get setup on the Supabase side of things you can follow the guide from the Supabase official docs.
-
Install the
supabase
CLI. Docs -
Login to the CLI
supabase login
. Docs -
You can skip the
supabase init
step and repurpose thesupabase/
folder andconfig.toml
file from the forked@truffle/discord-bot-demo
package. If you skipsupabase init
you will need to create a project through the Supabase Admin Dashboard and update theproject_id
field inside of theconfig.toml
. -
Link to your remote Supabase project with
supabase link --project-ref <ref>
. You can find your project ref if you click on the project in the Supabase Admin and grab the ref from the url e.ghttps://app.supabase.com/project/<ref>
Docs -
Deploy the Supabase Edge Function
supabase functions deploy truffle-discord-bot-demo --no-verify-jwt
- Note: Make sure you deploy the function with the
--no-verify-jwt
flag so the function can receive requests from Discord API webhooks (we perform our own validation)
- Note: Make sure you deploy the function with the
-
Create a new Discord application. Use the name and avatar of your choice.
-
If you're using the Supabase Edge Function template, you will need to setup a couple of environment variables for your function. To setup the environment variables you can add them to
supabase/.env
and sync them with your remote Supabase project w/supabase secrets set --env-file ./supabase/.env
after you've deployed the function, otherwise they'll be overwritten.TRUFFLE_API_KEY
is used to sign requests the function makes to the Truffle Graphql API. You can copy the generated Truffle Package API key fromtruffle.secret.mjs
.DISCORD_PUBLIC_KEY
is used to verifiy requests coming from the Discord API. You can find this key on thePublic Key
section of your Discord application page,discord.com/developers/applications/:id/information
.DISCORD_TOKEN
is used to authorize requests to the Discord API, such as when deploying your commands in the following steps. On the same page from above, click theBot
tab. You may need to clickCreate Bot
. Make sure you copy and store the token, because it will not be shown again.
-
Add the bot to your testing Discord server by visiting the link:
https://discord.com/api/oauth2/authorize?client_id={CLIENT_ID}&scope=applications.commands%20bot
, replacing{CLIENT_ID}
with theApplication ID
from thediscord.com/.../information
page above. -
Load your env, and run the Deploy Commands script to register the
user
slash command with the Discord API. It may take a moment for the commands to reflect on the desktop UI. -
Test out executing the remote function by running
/ping
in and channel where the bot was added.