Szymon Bednorz
Szymon Bednorz

Reputation: 475

How to modify channel of Discord webhook with Python

I have a Python script which sends messages using webhook assigned to my Discord server. I would like to make my program able to change webhook's channel sometimes.

On Discord Developer Portal I read there are two ways of modifying webhooks using requests (1. Modify webhook, 2. Modify webhook with token).

I have tested the easier second one but it allows only to change avatars and usernames:

 r = requests.patch("https://discordapp.com/api/webhooks/.../...", json={ "name":"New Name" })
 # status code 200

Changing webhook channels are offered by the first one but it requires something called MANAGE_WEBHOOKS permission so the method below of course does not work:

 r = requests.patch("https://discordapp.com/api/webhooks/...", json={ "channel_id":12345 })
 # status code 401 (unauthorized)

I have created Discord application and started reading about dealing with authorization for webhooks over here but I stuck and I don't understand how to actually make it work.

Upvotes: 0

Views: 4240

Answers (1)

Szymon Bednorz
Szymon Bednorz

Reputation: 475

After a fresh look at the Discord documentation I finally solved my problem. Below I write how to achieve such result in simple Python script:

  1. Open Discord Developer portal, create new app, add bot and URI for redirection in OAuth2 section.
  2. Create authorization URL using generator in OAuth2 section. For Scopes select bot and discord.incoming. For Bot Permissions select Manage Webhooks. You will get something like that:

    https://discordapp.com/api/oauth2/authorize?client_id=<MY_CLIENT_ID>&permissions=536870912&redirect_uri=<MY_URI>&response_type=code&scope=bot%20webhook.incoming
    
  3. Open this link in your browser to connect the bot and webhook to your server. On acceptance, you will be redirected to your URI address.

  4. The address will contain the code querystring parameter. I just copied this code manually and pasted it to the script in the following step (note that the code is valid for a short time so if you do everything too slow the authentication may not be succeed).
  5. Here is the code which make necessary authentication:

    import requests
    
    CLIENT_ID = # Your client id 
    CLIENT_SECRET = # Your client secret
    REDIRECT_URI = # Your redirect URI
    CODE = # Your code from step 4.
    
    data = {
        'client_id': CLIENT_ID,
        'client_secret': CLIENT_SECRET,
        'grant_type': 'authorization_code',
        'code': CODE,
        'redirect_uri': REDIRECT_URI,
        'scope': 'bot webhook.incoming',
        'permissions': "536870912",
    }
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded'
    }
    r = requests.post('https://discordapp.com/api/oauth2/token', data=data, headers=headers)
    r.raise_for_status()
    

    The authentication is success if no exceptions are thrown.

  6. If everything works well, run this code to change current webhook channel:

    ...
    
    BOT_TOKEN = # Your bot's token
    WEBHOOK_ID = # Your webhook's id
    
    json = { "channel_id": 12345 } # Replace with new webhook's channel id
    headers = { "Authorization": "Bot " + BOT_TOKEN }
    
    r = requests.patch('https://discordapp.com/api/webhooks/' + WEBHOOK_ID, json=json, headers=headers)
    

Upvotes: 2

Related Questions