Skip to content

Instantly share code, notes, and snippets.

@whirlxd
Created October 2, 2021 16:49
Show Gist options
  • Save whirlxd/c76c46203be4f9e9edd2cf0d30033836 to your computer and use it in GitHub Desktop.
Save whirlxd/c76c46203be4f9e9edd2cf0d30033836 to your computer and use it in GitHub Desktop.
Webhook Sender API
/*Code for a webhook api
Example usage:*/
// http://localhost:3000/webhook?url=https://canary.discord.com/api/webhooks/hook-id/hook-token&message=xdd&username=yoo```
js
const express = require("express");
const app = express();
const { WebhookClient: hook } = require("discord.js"); //webhook client sounds shit so i renamed it to hook!
app.get("/webhook", async (req, res) => {
//destructred query params
const { url, message, avatarUrl, username } = req.query;
//since it makes no sense to set a default value for them
if (!url || !message || !username) {
return res.status(400).json({
error: "Missing parameters",
});
}
const client = new hook({
url: url,
});
const av = avatarUrl ?? "https://i.imgur.com/AfFp7pu.png"; //yes it makes sense
try {
await client.send({
username: username,
content: message,
avatar: av,
});
res.status(200).json({
message: "Message sent",
});
} //POV: Some user sent more then like 2k chars
catch (err) {
res.status(500).json({
error: err,
});
}
});
app.get("/", (req, res) => { //idk just for decoration or a homepage
return res.sendFile("index.html", { root: __dirname });
});
app.listen(3000, () => console.log("Ez Ready"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment