Rename a Discord Channel

I’m trying to rename a Discord Channel using the Discord → Use any Discord API in Node.js step, but I keep getting a 401 Unauthorized error.

I successfully connected my Discord account, the Pipedream discord bot joined my server as an administrator, and it is able to send messages in channels using the Discord → Send Message step earlier in the workflow.

I also successfully tested the default code in the Use any Discord API in Node.js step, so I don’t think it’s having trouble connecting to Discord or authorizing.

import { axios } from "@pipedream/platform"
export default defineComponent({
  props: {
    discord: {
      type: "app",
      app: "discord",
    }
  },
  async run({steps, $}) {
    return await axios($, {
      url: `https://discord.com/api/users/@me`,
      headers: {
        Authorization: `Bearer ${this.discord.$auth.oauth_access_token}`,
        "accept": `application/json`,
      },
    })
  },
})

Here is the code I am trying to test unsuccessfully because it is returning a 401 Unauthorized error:

import { axios } from "@pipedream/platform"

export default defineComponent({
  props: {
    discord: {
      type: "app",
      app: "discord",
    }
  },
  async run({ steps, $ }) {
    // Get the current day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
    const currentDayIndex = new Date().getDay();

    // Define an array of channel names for each day of the week
    const channelNames = [
      "Sunday Channel",
      "Monday Channel",
      "Tuesday Channel",
      "Wednesday Channel",
      "Thursday Channel",
      "Friday Channel",
      "Saturday Channel",
    ];

    // Use the current day index to get the corresponding channel name
    const newChannelName = channelNames[currentDayIndex];

    // Replace with the actual channel ID
    const channelId = "1192532661877604462";

    // Update the channel name using the Discord API
    const response = await axios($, {
      method: "patch",
      url: `https://discord.com/api/channels/${channelId}`,
      headers: {
        Authorization: `Bearer ${this.discord.$auth.oauth_access_token}`,
        "accept": "application/json",
        "content-type": "application/json",
      },
      data: {
        name: newChannelName,
      },
    });

    return response.data;
  },
});

Hi @sean1,

Based on the information provided, it seems that you’ve set up the Discord bot correctly and it has the necessary permissions since it can send messages. However, you’re encountering a 401 Unauthorized error when trying to rename a Discord channel.

The 401 Unauthorized error indicates that the request has not been applied because it lacks valid authentication credentials for the target resource. Since you’ve confirmed that the default code works, the issue might be related to the specific endpoint or the way the request is being made.

In Discord, bot tokens are used for authentication instead of OAuth tokens when performing actions on a server. The Authorization header should use the bot token with the prefix Bot, rather than Bearer. This is different from the Bearer token used in the test request that fetches the user profile.

Here’s how you can modify your code to use the bot token for authorization:

import { axios } from "@pipedream/platform"

export default defineComponent({
  props: {
    discord: {
      type: "app",
      app: "discord",
    }
  },
  async run({ steps, $ }) {
    // ... (rest of your code)

    // Update the channel name using the Discord API
    const response = await axios($, {
      method: "patch",
      url: `https://discord.com/api/channels/${channelId}`,
      headers: {
        // Use the bot token with "Bot" prefix for authorization
        Authorization: `Bot ${this.discord.$auth.oauth_access_token}`,
        "accept": "application/json",
        "content-type": "application/json",
      },
      data: {
        name: newChannelName,
      },
    });

    return response.data;
  },
});

Please replace the Authorization header value with the correct bot token prefixed by Bot. If you’re still facing issues, ensure that the bot has the necessary permissions to manage channels.

If you need further assistance, you can visit Pipedream’s support page.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

I replaced Bearer with Bot and simplified the code, but still received the 401 Unauthorized error.

Here is the code:

import { axios } from "@pipedream/platform"

export default defineComponent({
  props: {
    discord: {
      type: "app",
      app: "discord",
    }
  },
  async run({ steps, $ }) {
    // ... (rest of your code)
    const channelId = "1192532661877604462";

    // Update the channel name using the Discord API
    const response = await axios($, {
      method: "patch",
      url: `https://discord.com/api/channels/${channelId}`,
      headers: {
        // Use the bot token with "Bot" prefix for authorization
        Authorization: `Bot ${this.discord.$auth.oauth_access_token}`,
        "accept": "application/json",
        "content-type": "application/json",
      },
      data: {
        name: "Test",
      },
    });

    return response.data;
  },
});