7
const guildMembersResponse = fetch(`http://discordapp.com/api/guilds/440494010595803136/members/278628366213709824`,
            {
              method: 'PUT',
              headers: {
                Authorization: `Bearer TOKEN`,
              },
            });
            setTimeout(() => {
                console.log(guildMembersResponse)
            }, 500);

I want to join a user to my Discord server with his userid and his token in nodejs, but if I request the Dicord api I get an error:

Promise {
 Response {
  size: 0,
  timeout: 0,
  [Symbol(Body internals)]: { body: [PassThrough], disturbed: false, error: 
  null },
  [Symbol(Response internals)]:
  { url:
  'https://discordapp.com/api/guilds/440494010595803136/members/278628366213709824',
   status: 401,
   statusText: 'UNAUTHORIZED',
   headers: [Headers] } } }

I am using the node-fetch library!

PLASMA chicken
  • 2,777
  • 2
  • 15
  • 25
G33RY
  • 141
  • 1
  • 2
  • 10

4 Answers4

4

I just had to replace the "ContentType" to "Content-Type" in the headers!

headers: {
  "Authorization": "Bot [botToken]",
  "Content-Type": "application/json",
}
Spacehold
  • 106
  • 2
  • 13
G33RY
  • 141
  • 1
  • 2
  • 10
2

You should add user token into the body, Like this:

{
  method: 'PUT',
  access_token: "Bearer TOKEN"
  headers: {
    Authorization: `Bot TOKEN`,
}

user token should be like this : TiaRZjWv5YAp80MpTFRkhi1GhXqddB

Lin Du
  • 88,126
  • 95
  • 281
  • 483
Deep
  • 19
  • 2
1

One thing I did to make it work was to use Discord's REST. unfortunately this documentation is very poorly prepared.

const { REST } = require('@discordjs/rest');

const rest = new REST({ version: '10' }).setToken('TOKEN');

rest.put(`/guilds/${guildID}/members/${userID}`, {
    body: {
        'access_token': accessToken
    }
})
Zikkado
  • 11
  • 1
0

A 401 error would mean that you haven't provided the correct scope for the oauth link. Read more about Discord's OAuth scopes over at https://discordapp.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes. Quoting the docs, you would need the guilds.join scope.

Moreover, the docs quote:

guilds.join and bot require you to have a bot account linked to your application. Also, in order to add a user to a guild, your bot has to already belong to that guild.

So make sure your app fulfills the above requirement.

If you have made sure that you have followed the above steps, and you still cannot get it working, you would have to share the OAuth link with us to help you investigate further

  • Yeah i already have a bot account linked to my application and I i enabled the guilds.join scope but I still get this error. Should I use my bot to join the user to the server or should i create a selfbot with the user's token and join the user or I dont know – G33RY Dec 28 '18 at 18:42
  • Is the bot you have linked with the application in the guild you wanted to add a user? –  Dec 28 '18 at 18:43
  • Please do see https://stackoverflow.com/questions/43269517/discord-add-guild-member-401-error-despite-apparently-valid-access-token . Talks about C#, but should help with JS too –  Dec 28 '18 at 18:46
  • Yeah thanks it worked but still get an error a BAD REQUEST error. I changed **Authorization** to `Bot BOT-TOKEN` and the **ContentType** to `application/json`, Can you tell me where do i need to add the user's token? – G33RY Dec 28 '18 at 18:55
  • needs to be in the body. see the question I linked above. –  Dec 28 '18 at 19:00