Skip to content

Commit

Permalink
Merge pull request #82 from yoggys/master
Browse files Browse the repository at this point in the history
feat: Webhook URLs (#17)
  • Loading branch information
sylveon authored Jul 21, 2024
2 parents d9d3c0f + f818d8e commit f0532d2
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 21 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,17 @@ Users that spam requests can be blocked by creating an environment variable call
![](https://i.imgur.com/jNKgS2B.png)

6. Redeploy the site with **Deploys** -> **Trigger deploy** -> **Deploy site**.

## Using webhooks

When you use the `DISCORD_WEBHOOK_URL`, you don't need to specify the `DISCORD_BOT_TOKEN`, `GUILD_ID`, and `APPEALS_CHANNEL` in the environment variables. The message will be sent using the webhook without an unban button. To do this:

1. On your [Netlify dashboard](https://app.netlify.com), click **Deploys** and navigate to **Deploy settings**, and then to the **Environment** option.

2. Under **Environment variables**, click **Edit variables**.

3. Right-click on any channel and click **Edit Channel** -> **Integrations** -> **Webhooks** -> **Copy Webhook URL**.

4. Click **New variable**, and create an environment variable with `DISCORD_WEBHOOK_URL` as its key. For the value, paste the Webhook URL you copied in the previous step.

5. Redeploy the site with **Deploys** -> **Trigger deploy** -> **Deploy site**.
22 changes: 12 additions & 10 deletions build.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,21 @@ async function main() {
}
});

if (process.env.DISABLE_UNBAN_LINK) {
if (process.env.DISABLE_UNBAN_LINK || process.env.DISCORD_WEBHOOK_URL) {
fs.unlink(path.resolve(func, "unban.js"), assertSuccess);
}

// Make sure the bot connected to the gateway at least once.
const bot = new Eris(process.env.DISCORD_BOT_TOKEN);
bot.on("ready", () => bot.disconnect());

try {
await bot.connect();
} catch (e) {
console.log(e);
process.exit(1);
if(!process.env.DISCORD_WEBHOOK_URL) {
// Make sure the bot connected to the gateway at least once.
const bot = new Eris(process.env.DISCORD_BOT_TOKEN);
bot.on("ready", () => bot.disconnect());

try {
await bot.connect();
} catch (e) {
console.log(e);
process.exit(1);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion func/oauth-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function handler(event, context) {
};
}

if (process.env.GUILD_ID && !process.env.SKIP_BAN_CHECK) {
if (process.env.GUILD_ID && !process.env.SKIP_BAN_CHECK && !process.env.DISCORD_WEBHOOK_URL) {
const ban = await getBan(user.id, process.env.GUILD_ID, process.env.DISCORD_BOT_TOKEN);
if (ban === null) {
return {
Expand Down
31 changes: 22 additions & 9 deletions func/submission-created.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export async function handler(event, context) {
}
}

if (process.env.GUILD_ID) {
if (process.env.GUILD_ID && !process.env.DISCORD_WEBHOOK_URL) {
try {
const ban = await getBan(userInfo.id, process.env.GUILD_ID, process.env.DISCORD_BOT_TOKEN);
if (ban !== null && ban.reason) {
Expand Down Expand Up @@ -98,14 +98,27 @@ export async function handler(event, context) {
}
}

const result = await fetch(`${API_ENDPOINT}/channels/${encodeURIComponent(process.env.APPEALS_CHANNEL)}/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bot ${process.env.DISCORD_BOT_TOKEN}`
},
body: JSON.stringify(message)
});
let result;
if (!process.env.DISCORD_WEBHOOK_URL) {
result = await fetch(`${API_ENDPOINT}/channels/${encodeURIComponent(process.env.APPEALS_CHANNEL)}/messages`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Authorization": `Bot ${process.env.DISCORD_BOT_TOKEN}`
},
body: JSON.stringify(message)
});
} else {
result = await fetch(`${process.env.DISCORD_WEBHOOK_URL}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
embeds: [message.embed]
})
});
}

if (result.ok) {
if (process.env.USE_NETLIFY_FORMS) {
Expand Down
2 changes: 1 addition & 1 deletion public/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ <h2 id="username" class="ml-3 mb-0 align-self-center"></h2>
const jsonPayload = decodeURIComponent(atob(base64).split('').map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2)).join(''));

return JSON.parse(jsonPayload);
};
}

const params = new URLSearchParams(document.location.search.substring(1));
if (!params.has("token")) {
Expand Down

0 comments on commit f0532d2

Please sign in to comment.