izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

Incorrectly adding / removing roles #289

Open trololo151 opened 6 years ago

trololo151 commented 6 years ago

before starting the code, I delete all the roles that I can get further bot.removeFromRole({ serverID: id server, userID: id user, roleID: id role, }); ..... After that, I have a condition: if (avgStats.avgDamageDealt > 250) { bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485142162262982666', // 250+ }); } else if (avgStats.avgDamageDealt > 200) { bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485142170844659730', // 200+ }); } if (avgStats.avgKills > 2) { bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484902728976433152', // 2+ }); } else if (avgStats.avgKills > 1.5) { bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484854918348406787', // 1.5+ }); If you write a command in a discord chat !Stats. The bot must first delete all roles and assign a new one depending on the avgStats.avgDamageDealt . But the bot sometimes does not delete or add. I have already seen in the problems of Discord and this situation and nothing has changed.

Peacerekam commented 6 years ago

listen for errors with

bot.addToRole({
  xx: xx,
  yy: yy
}, function(err){
  console.log(err)
})

Im betting you're getting ratelimited, but I've never assigned roles as fast as you so I can't be sure.

trololo151 commented 6 years ago

{ ResponseError: Could not add role at handleResCB (C:\Program Files\nodejs\node_modules\statg-bot-master\node_modules\discord.io\lib\index.js:1403:10) at C:\Program Files\nodejs\node_modules\statg-bot-master\node_modules\discord.io\lib\index.js:1178:3 at C:\Program Files\nodejs\node_modules\statg-bot-master\node_modules\discord.io\lib\index.js:1489:13 at Gunzip.onError (zlib.js:116:5) at emitOne (events.js:116:13) at Gunzip.emit (events.js:211:7) at Gunzip.zlibOnError (zlib.js:156:8) name: 'ResponseError', statusCode: 429, statusMessage: 'TOO MANY REQUESTS', response: { global: false, message: 'You are being rate limited.', retry_after: 1144 } } { ResponseError: Could not add role at handleResCB (C:\Program Files\nodejs\node_modules\statg-bot-master\node_modules\discord.io\lib\index.js:1403:10) at C:\Program Files\nodejs\node_modules\statg-bot-master\node_modules\discord.io\lib\index.js:1178:3 at C:\Program Files\nodejs\node_modules\statg-bot-master\node_modules\discord.io\lib\index.js:1489:13 at Gunzip.onError (zlib.js:116:5) at emitOne (events.js:116:13) at Gunzip.emit (events.js:211:7) at Gunzip.zlibOnError (zlib.js:156:8) name: 'ResponseError', statusCode: 429, statusMessage: 'TOO MANY REQUESTS', response: { global: false, message: 'You are being rate limited.', retry_after: 1141 } }

trololo151 commented 6 years ago

how fix it ?

trololo151 commented 6 years ago

can be somehow you can find out what roles the user now has?

trololo151 commented 6 years ago

and then delete the currently selected roles and assign new ones

cloudrac3r commented 6 years ago

There are three endpoints in the API for assigning roles to members.

  1. "Add guild member role." This takes a member and a role and adds that role.
  2. "Remove guild member role." This takes a member and a role and removes that role.
  3. "Modify guild member." This takes a member and an array of roles. This array replaces all of the user's roles at once.

discord.io uses endpoints 1 and 2, but for assigning many roles at once it's easier to use endpoint 3. Unfortunately discord.io does not expose a method for this endpoint, so we have to manually make a request to the API.

npm install request and const request = require("request").

request({
    url: "https://discordapp.com/api/v6/guilds/482986292997783562/members/"+INSERT_USER_ID_HERE,
    headers: {
        "User-Agent": "DiscordBot (Custom API request, 1.0)",
        "Authorization": "Bot "+INSERT_TOKEN_HERE
        "Content-Type": "application/json"
    },
    method: "PATCH",
    body: JSON.stringify({roles: ["array", "of", "role", "IDs"]})
}, function(error, response, body) {
    // All done. response.statusCode should be 204 on success, 4XX or 5XX on failure.
});

For the role array, all previous roles will be wiped and replaced with that array. Therefore you should adapt your code to, instead of performing bot.addToRole, simply add that role ID to an empty array. Then after all your ifs, use my code block to add that role array to the user.

This way you are only making one request, which should be (a) faster, (b) more reliable, and (c) not hit the rate limit errors.

trololo151 commented 6 years ago

I do not understand until the end how to order me to implement your code :( Thanks for the help, but I can not adapt to my code, I'm new at this

trololo151 commented 6 years ago

request({ url: "https://discordapp.com/api/v6/guilds/482986292997783562/members/"+cmd.discordUser.id, headers: { "User-Agent": "DiscordBot (Custom API request, 1.0)", "Authorization": "Bot "+ "TONEDXXXXXXXX", "Content-Type": "application/json" }, method: "PATCH", body: JSON.stringify({roles: ["485158224962781184", "485142165345796096", "485142162262982666", "485142170844659730", "485142059934810133", "485141736012644414", "485140996087218178", "485871367397441536", "484742900220166164", "484902728976433152", "484854918348406787", "484745480572633099", "484866966297772047"]}) }, function(error, response, body) { // All done. response.statusCode should be 204 on success, 4XX or 5XX on failure. });

var kill; kill = avgStats.avgKills; switch (true) { case kill > 3: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485871367397441536', // 3+ }); break; case kill >= 2.5 && kill < 3: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484742900220166164', // 2.5+ }); break; case kill >= 2 && kill < 2.5: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484902728976433152', // 2+ }); break; case kill > 1.5 && kill < 2: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484854918348406787', // 1.5+ }); break; case kill > 1 && kill < 1.5: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484745480572633099', // 1+ }); break; case kill > 0.5 && kill < 1: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '484866966297772047', // 0.5+ }); break;

}

var damage; damage = avgStats.avgDamageDealt; switch (true) { case damage >= 350: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485158224962781184', // 350+ }); break; case damage >= 300 && damage < 350: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485142165345796096', // 300+ }); break; case damage >= 250 && damage < 300: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485142162262982666', // 250+ }); break; case damage >= 200 && damage < 250: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485142170844659730', // 200+ }); break; case damage >= 150 && damage < 200: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485142059934810133', // 150+ }); break; case damage >= 100 && damage < 150: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485141736012644414', // 100+ }); break; case damage >= 50 && damage < 100: bot.addToRole({ serverID: '482986292997783562', userID: cmd.discordUser.id, roleID: '485140996087218178', // 50+ }); break;
}