From dbbf12789c512ae3f17f99c03d4ad3022b8efd5b Mon Sep 17 00:00:00 2001 From: Luna Date: Sat, 30 Nov 2024 19:57:40 +0100 Subject: [PATCH] add typescript & bot examples --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/README.md b/README.md index 605f1a0..3483f84 100644 --- a/README.md +++ b/README.md @@ -65,3 +65,54 @@ As authorization header, you need to provide the `Bearer` token that you set in curl -X POST http://localhost:8080/refresh \ -H "Authorization: Bearer " ``` + +## TypeScript example +```ts +// blahaj.ts +export interface BlahajResponse { + url: string; +} + +export async function getBlahaj() { + const blahaj = await fetch(this.url) + .then((r) => r.json()) + .catch(() => null) as BlahajResponse | null; + + return blahaj; +} + +// index.ts +import { getBlahaj } from './blahaj.ts'; + +const blahaj = await getBlahaj(); +console.log(blahaj.url); +``` + +## JavaScript discord bot example +```js +const { Client, GatewayIntent } = require('discord.js'); + +const client = new Client({ + intents: [ + GatewayIntent.GuildMessages, + GatewayIntent.MessageContent, + ] +}); + +client.on('messageCreate', async (message) => { + if (message.content === '!blahaj') { + const blahaj = await fetch('http://localhost:8080') + .then((r) => r.json()) + .catch(() => null); + + if (!blahaj) { + message.channel.send({ content: 'Failed to get a random blahaj' }); + return; + } + + message.channel.send({ content: blahaj.url }); + } +}); + +client.login('your-token'); +``` \ No newline at end of file