---
# System prepended metadata

title: Discord.js and HTTP requests
tags: [discord, ' discord.js', ' express', ' http']

---

---
title: Discord.js and HTTP requests
tags: discord, discord.js, http, express
---
## Discord.js and HTTP requests

The basic idea goes by that if discord commands were used, the `message` object would contain the information required like channel ID, guild ID, etc.
But this is not possible if a request is used to perform certain functionalities.

**What is available to us?**
> `client` object is available for making everything. `client` is a treasure of its own. From roles to channels to guild everything can be obtianed from it.

### Lets start how to use discord.js functions in server (Express) requests.
> **NOTE** We are using the discord.js@12.x.x version. A recent version has been released. Updates will come soon.

- **First step would to import the module**
    ```javascript
    const {Client} = require('discord.js')
    ```
- **Create a `client` object**
    ```javascript
    const client = new Client()
    ```
- **Now if your server code is broken down into modular format, you should make the `client` object available all around the modules**
    > Provided you have set up your server using Express.js framework and your code is on modular format.

    ```javascript
    app.set('discord', client)
    ```
- **Now in the request you want to use, just get the `discord`**
    ```javascript
    router.get('/some-endpoint', (req, res)=>{
        const client = req.app.get('discord')
        //now use the client object as per requirement.
    })
    ```
- **First we need to find the guild**
    > Guilds are nothing but the discord servers your bot is installed.
    
    To get all the guilds, use
    ```javascript
    client.guilds.cache 
    ```
    It returns a `collection`. You can find the documention for `collection` [here](https://discord.js.org/#/docs/collection/main/class/Collection).  
    
    Now lets use a method of collection to find your server.
    ```javascript
    client.guilds.cache.find((each) => each.name === "Your_Server_Name")
    ```
    Returns a `Guild` object. You can documentation of `Guild` class [here](https://discord.js.org/#/docs/main/stable/class/Guild).
    
- ### Adding a channel
    > Idea behind adding a channel is first find the guild then use that to create channels. But these channels will not be added under any category. To add them under a category, we need to find the category id and provide that while creating the channel.
    
    Now lets find the category first.
    >To find the category, either we need to know the name of the category or we need to find them in brute-force manner


    If we know the category name,
    ```javascript
    <guild>.channels.cache.find((each)=> each.name==="CATEGORY_NAME" && each.type === 'category')
    ```
    >Replace the `CATEGORY_NAME` with the name of the category in your server. Use the `guild` object obtained previouly in `<guild>`

    If you want to find the categories available,
    ```javascript
   console.log(<guild>.channels.cache.filter((each)=>each.type==='category'))
    ```
    >Use the `guild` object obtained previouly in `<guild>`

    After obtaining the name, you can use that to find the category.
    
    Now lets use the guild and category to create a channel inside that category.
    ```javascript
    <guild>.channels.create("CHANNEL_NAME", {
        type: 'DESIRED_TYPE',
        permissionOverwrites: [
		{
			id: <guild>.roles.everyone,
			allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY"],
		},
	],
	parent: <catergory>.id,
    })
    ```
    >Replace `CHANNEL_NAME` with your channel name, `DESIRED_TYPE` with the type of channel you want text/voice/category, `<guild>` with desired guild, `<category>` with the category obtained before. You can use own `permissionOverwrites`.

    **Example**
    > A full example how to create a public channel with all permissions 
    ```javascript
    exports.createChannel = async (req, res) => {
		const client = req.app.get("discord")
		const allGuilds = client.guilds.cache
		const result = allGuilds.find((each) => each.name === "Pratyay Server")
		const textCategory = result.channels.cache.find(
			(each) => each.name === "Text Channels"
		)
		result.channels.create("Random Channel", {
			type: "text",
			permissionOverwrites: [
				{
					id: result.roles.everyone,
					allow: ["VIEW_CHANNEL", "SEND_MESSAGES", "READ_MESSAGE_HISTORY"],
			},
		],
		parent: textCategory.id,
	    })
	    return res.json({ status: 'Channel Created'})
    }
    ```
- **Send text to a channel**
    