1
votes

I'm setting up a api connection. I have already an OAuth-authentication and a bot for my app. Now I want to make changes on discord Guild Member Role.

I make a PATCH and it return an 204. According to discord: "Returns a 204 empty response on success". But the don't get an update. No changes at Role.

Here is my "PATCH function":

function patchGuildMemberRole( userId, callback) {
  var API_ModifyGuildMember_URL = 'https://discordapp.com/api/guilds/500000000000000000/members/' + userId;
  data = {
    "roles": ["600000000000000000", "600000000000000000"]
  };
  header = {
    'method' : 'PATCH',
    'Authorization': 'Bot' + ' ' + 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
    'followRedirects' : true,
    'Content-Type': 'application/x-www-form-urlencoded',
    'payload' : data
  };
  var result = UrlFetchApp.fetch(API_ModifyGuildMember_URL, {headers: header});
  console.log("result");
  console.log(result);
  if (result.getResponseCode() == 204 || result.getResponseCode() == 200) {
    var params = JSON.parse(result.getContentText());
    return callback(params);
  } else {
    return callback(error('API fetch error'));
  }
}

Here is my Response: "params":

{
  "nick": "Kevin - Kevin",
  "user": {
    "username": "Kevin",
    "discriminator": "6666",
    "id": "500000000000000000", "avatar": "zzzzzzzzzzzzzz80acd5bb1a005"
  },
  "roles": ["500000000000000000"],
  "premium_since": null,
  "deaf": false,
  "mute": false,
  "joined_at": "2016-04-00T00:00:00.000000+00:00"
}

We want to update the roles to : ["600000000000000000", "600000000000000000"]

but we only have the old roles: ["500000000000000000"]

1

1 Answers

1
votes

How about this modification?

From:

header = {
  'method' : 'PATCH',
  'Authorization': 'Bot' + ' ' + 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
  'followRedirects' : true,
  'Content-Type': 'application/x-www-form-urlencoded',
  'payload' : data
};
var result = UrlFetchApp.fetch(API_ModifyGuildMember_URL, {headers: header});

To:

var params = {
  method: 'PATCH',
  headers: {Authorization: 'Bot' + ' ' + 'XXXXXXXXXXXXXXXXXXXXXXXXXX'},
  contentType: 'application/json',
  payload : JSON.stringify(data)
};
var result = UrlFetchApp.fetch(API_ModifyGuildMember_URL, params);
  • Please put out method and payload from the headers.
  • You can use contentType instead of Content-Type. In this case, this is not required to be in the headers.
  • In your case, contentType is application/json.

Reference:

If this didn't resolve your issue, I apologize.

Edit:

When the error of {"message": "Missing Permissions", "code": 50013} with the status code of 403 occurs, please check the following point.

  • At "Server settings", open "Roles".

    • The order of roles are important for your situation.
    • When it supposes that the role of Bot and the roles you want to give is "ForBot" and "sampleRole1" and "sampleRole2", respectively, if the order of Roles are as follows, the error of {"message": "Missing Permissions", "code": 50013} with the status code of 403 occurs.

      • From

        enter image description here

    • If the order of Roles are as follows, no error with the status code of 204 occurs. The request works.

      • To

        enter image description here

From above point, please confirm the order of Roles of your settings. And for example, please move the role of bot to the top of all roles by the drag and drop, and try to run the script again. When you modify the order of role, please don't forget to save it.

Reference: