Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

Incorrectly adding / removing roles #289

Open
trololo151 opened this issue Sep 3, 2018 · 8 comments
Open

Incorrectly adding / removing roles #289

trololo151 opened this issue Sep 3, 2018 · 8 comments

Comments

@trololo151
Copy link

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
Copy link

Peacerekam commented Sep 3, 2018

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
Copy link
Author

{ 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
Copy link
Author

how fix it ?

@trololo151
Copy link
Author

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

@trololo151
Copy link
Author

and then delete the currently selected roles and assign new ones

@cloudrac3r
Copy link

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
Copy link
Author

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
Copy link
Author

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;
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants