Code Monkey home page Code Monkey logo

Comments (1)

johnandreopoulos avatar johnandreopoulos commented on May 18, 2024

This segment of the code exclusively permits webhook URLs that commence with 'https://discord.com/api'. You can find the specific code here:

if (webhookURL) {
if (typeof (webhookURL) != "string" || webhookURL.indexOf("https://discord.com/api/")) return error(res, '<strong>[Webhook URL]</strong> is not a valid webhook URL.');
const segments = webhookURL.split("/").slice(4);
if (segments[0] == "webhooks") segments.unshift("v10")
else if (["v6", "v7", "v8", "v9"].includes(segments[0])) segments[0] = "v10"
else if (segments[0] != "v10") return error(res, '<strong>[Webhook URL]</strong> is not a valid webhook URL.');
let base64;
try {
if (segments[1] != "webhooks" || Number.isNaN(segments[2]) || segments[3]?.split("").find(letter => base64UrlAlphabet.indexOf(letter) == -1) || segments[4] || segments.length != 4) return error(res, '<strong>[Webhook URL]</strong> is not a valid webhook URL.');
webhookURL = `https://discord.com/api/${segments.join('/')}`;
const resp = await fetch(webhookURL);
if (!resp.ok) return error(res, '<strong>[Webhook URL]</strong> is not a valid webhook URL.');
} catch(e) {
console.log(e)
return error(res, '<strong>[Webhook URL]</strong> is not a valid webhook URL.');
}
}

Code explanation

  1. Check if webhookURL exists: It starts by checking if webhookURL exists. If it does, it proceeds with further validation. If not, it probably returns an error.
  2. Check if webhookURL is a string and has the correct format: It checks if webhookURL is a string and if it contains the base URL for Discord webhooks (https://discord.com/api/). If webhookURL is not a string or doesn't contain the correct base URL, it returns an error.
  3. Parsing segments of the URL: It splits the URL into segments using "/" as a delimiter and extracts certain parts of it.
  4. Adjusting version: It checks the version of the webhook URL. If it's using an older version (v6, v7, v8, or v9), it converts it to v10. If it doesn't start with v10, it returns an error.
  5. Further validation: It performs additional checks on the segments extracted from the URL. It ensures that the URL follows a specific pattern and contains the required segments. If any of the checks fail, it returns an error.
  6. Testing the webhook URL: It tries to fetch the URL using fetch and checks if the response is successful (resp.ok). If the response is not OK, it returns an error.
  7. Error handling: If any errors occur during the process (such as network errors during the fetch operation), it catches the error and returns an error response.
    Overall, this code snippet ensures that the provided Discord webhook URL is valid and conforms to a specific format expected by the application. If the URL is not valid, it returns an error message indicating that the webhook URL is not valid.

To generate a webhook, refer to the documentation outlined in the following steps provided by Discord's official documentation:

Making a Webhook

With that in mind, if you want to update your Discord server with GitHub commits and pull requests or track analytics from DataDog or anything similar, here's how!

  1. Open your Server Settings and head into the Integrations tab:
  2. Click the "Create Webhook" button to create a new webhook!

Screen_Shot_2020-12-15_at_4.41.53_PM.png

You'll have a few options here. You can:

  •  Edit the avatar: By clicking the avatar next to the Name in the top left
  • Choose what channel the Webhook posts to: By selecting the desired text channel in the  dropdown menu.
  • Name your Webhook: Good for distinguishing multiple webhooks for multiple different services.

You now have your own handy URL / pneumatic tube schoomp-er that you can link to more websites to receive messages from. 

Screen_Shot_2020-12-15_at_4.51.38_PM.png

Quick Example: GitHub Webhook Integration

Since webhooks can't really function on their own and need to be linked to another website to function as intended, I figured it'd be best to include a sample integration for any of you GitHub enthusiasts and code-pushers.

You can use Discord's webhooks to send any code merge or push updates in a repository of yours into a text channel in your server.  

1. Get the webhook URL for the server/channel you want to receive messages:

In my server, I've created a text channel labeled #general-chat and I want my repo updates to post messages here!

Therefore, I'll grab the webhook URL for this channel by pressing the Copy Webhook URL button:

Screen_Shot_2020-12-15_at_4.45.52_PM.png

from discord-bot-list-v3.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.