Skip to content
Advertisement

Discord guilds.join OAuth2 in JS

I am making a bot for Discord (node.js) and I want to have it’s oauth2 so when the user adds the bot to their server and accepts the oauth app permissions, the user is automatically joined into my bot support server. I’ve seen https://dsc.gg do this, and I want to do something similar. How would I do this in Javascript? (hosted at replit.com) Thanks!

Advertisement

Answer

First, fetch the access token from Discord if you’re using response_type=code.

Second, add the user to the guild using Discord.JS’s addMember function:

/*
    You'll need to have already defined these variables:
      guild : The guild you're adding the member to.
      accessToken : The access token.
      userId : The user's ID.
      client: Your bot's client.
*/

// Fetch the user.
client.users.fetch(userId).then((user) => {
    // Add the user to the guild - make sure you pass the access token.
    guild.addMember(user, { accessToken });
});

Remember to get the user’s explicit permission before adding them to the server:

You may not use the APIs in any way to:

  • modify a Discord user’s account without explicit permission from the Discord user. For example, you may not add a Discord user to a Discord (also known as a “server”) unless that Discord user expressly approved joining that Discord (such as when using a “group finder” app);
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement