Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preventing Bots from having Top 10 coin holder role #470

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

probro27
Copy link
Contributor

@probro27 probro27 commented Apr 4, 2023

Summary of Changes

Added a check in cron job to see if the member being assigned the role is a bot.

Related Issues

resolves #467

Steps to Reproduce

Check if a bot is in leaderboard but does not get the role.

Demonstration of Changes

Cannot demonstrate since my bot for some reason is not getting added to the leaderboard.

Further Information and Comments

@probro27 probro27 changed the title Preventing B Preventing Bots from having Top 10 coin holder role Apr 4, 2023
@probro27 probro27 self-assigned this Apr 4, 2023
Copy link
Contributor

@mcpenguin mcpenguin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Contributor

@Picowchew Picowchew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per the description in the issue:

There should still be ten people with the Top 10 Coin Holder role, though they should all be non-bot users.

This code looks like less than ten people would have the role.

@@ -137,7 +137,8 @@ export const createCoffeeChatCron = (client: Client): CronJob =>
// Gives Codey coin role to those on the leaderboard list everyday
export const assignCodeyRoleForLeaderboard = (client: Client): CronJob =>
new CronJob('0 0 0 */1 * *', async function () {
const leaderboard = await getCoinLeaderboard(NUMBER_USERS_TO_ASSIGN_ROLE);
// extra 5 members in case there are 5 bots in the leaderboard (as per the production environment)
const leaderboard = await getCoinLeaderboard(NUMBER_USERS_TO_ASSIGN_ROLE + 5);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might give more users than 10 if there are less than 5 bots that are in the top 10 codey coin users. I would make sure that in the code that is responsible for displaying the leaderboard, you only display the top ten people (right now it displays everyone in the leaderboard).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcpenguin So the code for getCoinLeaderboard uses a SQL query and I am not sure how I can change the SQL query itself to remove the bots.
What I can do is something like this -

let users = 0;
while (users < 10) {
  const leaderboard = await getCointLeaderboard(NUMBER_USERS_TO_ASSIGN_ROLE + 5);
 ... // the rest of the code 
    if (memberToUpdate && !memberToUpdate.user?.bot) {
        await updateMemberRole(memberToUpdate, await getRoleName(CODEY_COIN_ROLE_ID), true);
       users++; // add that we have added 1 user
      }
}

This can ensure that only 10 users get the role. Let me know your thoughts.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we should get the leaderboard before the while loop, but yeah, that was how I was thinking we could proceed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay cool, yeah I will do that and update the pull request.

Copy link
Contributor

@mcpenguin mcpenguin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@probro27
Copy link
Contributor Author

@Picowchew Please confirm :)

@probro27 probro27 requested a review from Picowchew June 15, 2023 03:45
Copy link
Contributor

@Picowchew Picowchew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code seems to give an infinite loop. Since you have asynchronous code inside the forEach loop, the forEach loop finishes iterating before the code inside an iteration finishes executing. Then, the users variable remains to be 0, so you get an infinite loop because the condition of the while loop is always satisfied.

I would recommend iterating through leaderboardIds with a for loop, and then break out of this loop once 10 non-bot users have the role.

To test your code, I would recommend having more than 10 users in the user_coin table (e.g. you can give coins to more than 10 users), assigning less than 10 users the role, and shortening the time interval of the cron job. Then, ensure that the result is that only the top 10 non-bot users have the role.

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

Successfully merging this pull request may close these issues.

Bots should not be able to have the Top 10 Coin Holder role
3 participants