Skip to content

Commit 70ce40d

Browse files
committed
Allow for user privacy (opt out of leaderboard)
1 parent 3f1d65e commit 70ce40d

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/commandDetails/coin/leaderboard.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
import {
1111
getCurrentCoinLeaderboard,
1212
getCoinBalanceByUserId,
13+
getPrivateUserIdList,
1314
UserCoinEntry,
1415
getUserIdCurrentCoinPosition
1516
} from '../../components/coin';
@@ -27,6 +28,7 @@ const getCurrentCoinLeaderboardEmbed = async (
2728
// Initialise user's coin balance if they have not already
2829
const userBalance = await getCoinBalanceByUserId(currentUserId);
2930
const currentPosition = await getUserIdCurrentCoinPosition(currentUserId);
31+
const isUserPrivate = await getPrivateUserIdList(currentUserId);
3032

3133
const leaderboardArray: string[] = [];
3234
for (let i = 0; i < leaderboard.length && leaderboardArray.length < limit; i++) {
@@ -38,8 +40,10 @@ const getCurrentCoinLeaderboardEmbed = async (
3840
continue;
3941
}
4042
if (user.bot) continue;
41-
const userTag = user?.tag ?? '<unknown>';
42-
const userCoinEntryText = `${leaderboardArray.length + 1}. ${userTag} - ${userCoinEntry.balance} ${getCoinEmoji()}`;
43+
const userTag = user?.tag;
44+
const displayTag = userTag && !isUserPrivate ? userTag : '<unknown>';
45+
const userCoinEntryText =
46+
`${leaderboardArray.length + 1}. ${displayTag} - ${userCoinEntry.balance} ${getCoinEmoji()}`;
4347
leaderboardArray.push(userCoinEntryText);
4448
}
4549
const currentLeaderboardText = leaderboardArray.join('\n');

src/components/coin.ts

+8
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export const getCoinBalanceByUserId = async (userId: string): Promise<number> =>
8181
return _.get(res, 'balance', 0);
8282
};
8383

84+
export const getPrivateUserIdList = async (userId: string): Promise<number> => {
85+
const db = await openDB();
86+
// Query user privacy from DB.
87+
const res = await db.get('SELECT is_private FROM user_coin WHERE user_id = ?', userId);
88+
// If user doesn't have a privacy value, default to false (public).
89+
return _.get(res, 'is_private', 0);
90+
};
91+
8492
/*
8593
If user doesn't exist, create row with newBalance as the balance.
8694
Otherwise, update balance to newBalance.

0 commit comments

Comments
 (0)