Discord API examples & templates
Use these vals as a playground to view and fork Discord API examples and templates on Val Town. Run any example below or find templates that can be used as a pre-built solution.
stevekrouse
discordWebhook
Script
Send a Discord message Send a message to a Discord channel from Val Town. It's useful for notifying your team or community when someone interesting happens, like a user signup, Stripe payment, mention on social media, etc. import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
await discordWebhook({
url: Deno.env.get("engDiscord"),
content: "Hi from val town!",
});
Example val: https://www.val.town/v/stevekrouse.discordWebhookEx Setup 1. Create a Discord Webhook Follow the instructions here: https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks It really only takes 2 minutes. 2. Copy webhook URL Paste it into your secrets as discordWebhook . 3. Send a message! import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
await discordWebhook({
url: Deno.env.get("engDiscord"),
content: "Hi from val town!",
}); Example val: https://www.val.town/v/stevekrouse.discordWebhookEx
1
dglazkov
discordBot
Script
A simple Discord Bot scaffolding, a slight rev on the one in the valtown guide . The discordBot function takes in an object where each key is a Discord command and the value is a function to handle the command. If the function returns a Promise , it will be handled as a deferred interaction with a followup message . Usage:
import { discordBot } from "https://esm.town/v/dglazkov/discordBot";
const echo = async (data) => {
await new Promise((r) => setTimeout(r, 5000));
return {
type: 4,
data: {
content: data.data.options[0].value,
},
};
};
export default discordBot({
ping: () => ({
type: 4,
data: {
content: `Pong! It is ${new Date()}`,
},
})
echo,
});
2
mattx
verify_discord_signature
Script
verify_discord_signature Verify HTTP signatures coming from Discord. public_key should be the Ed25519 public key from Discord, as a hex string body should be the request body as a string. If you have a JSON object as the request body, use JSON.stringify. signature should be the X-Signature-Ed25519 header timestamp should be the X-Signature-Timestamp header
You must return a 401 error and return early if this function returns false, otherwise you will pretty quickly get a big scary warning from Discord that your endpoint has been removed. Note that you'll only be able to add one once you've integrated this correctly. As this function only deals with strings, it doesn't matter whether you use an Express or web endpoint.
1
artivilla
slothdaobot
HTTP
SlothDAObot Allow your discord guild to run the following actions in their own channels: /links --2 to print a list of links in the last 2 weeks /summary --2 to print a summary of all the content shared in the last 2 weeks How to deploy Create a new discord bot new appplication: https://discord.com/developers/applications Under applications > OAuth2 , header OAuth2 URL Generator , select Bot Under Bot Permissions , select all options that are relevant (see example for this bot below) Copy Generated URL from the bottom of the page and open and install for your guild/community. Note, the permissions are not saved on the page upon refreashing the page.
0