Skip to content

WIP: Allow for user privacy (opt out of leaderboard) #332

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/commandDetails/coin/leaderboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
getCurrentCoinLeaderboard,
getCoinBalanceByUserId,
getUserPrivacy,
UserCoinEntry,
getUserIdCurrentCoinPosition
} from '../../components/coin';
Expand All @@ -27,6 +28,7 @@ const getCurrentCoinLeaderboardEmbed = async (
// Initialise user's coin balance if they have not already
const userBalance = await getCoinBalanceByUserId(currentUserId);
const currentPosition = await getUserIdCurrentCoinPosition(currentUserId);
const isUserPrivate = await getUserPrivacy(currentUserId);

const leaderboardArray: string[] = [];
for (let i = 0; i < leaderboard.length && leaderboardArray.length < limit; i++) {
Expand All @@ -38,8 +40,11 @@ const getCurrentCoinLeaderboardEmbed = async (
continue;
}
if (user.bot) continue;
const userTag = user?.tag ?? '<unknown>';
const userCoinEntryText = `${leaderboardArray.length + 1}. ${userTag} - ${userCoinEntry.balance} ${getCoinEmoji()}`;
const userTag = user?.tag;
const displayTag = userTag && !isUserPrivate ? userTag : '<unknown>';
const userCoinEntryText = `${leaderboardArray.length + 1}. ${displayTag} - ${
userCoinEntry.balance
} ${getCoinEmoji()}`;
leaderboardArray.push(userCoinEntryText);
}
const currentLeaderboardText = leaderboardArray.join('\n');
Expand Down
64 changes: 64 additions & 0 deletions src/commandDetails/coin/privacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { container } from '@sapphire/framework';
import { Permissions, User } from 'discord.js';
import {
CodeyCommandDetails,
CodeyCommandOptionType,
CodeyCommandResponseType,
SapphireMessageExecuteType
} from '../../codeyCommand';
import { getUserPrivacy, changeUserPrivacy } from '../../components/coin';

// Update coin leaderboard privacy of a user
const coinPrivacyExecuteCommand: SapphireMessageExecuteType = async (client, messageFromUser, args) => {
if (!(<Readonly<Permissions>>messageFromUser.member?.permissions).has('ADMINISTRATOR')) {
return `You do not have permission to use this command.`;
}

// First mandatory argument is user
const user = <User>args['user'];
if (!user) {
throw new Error('please enter a valid user mention or ID for leaderboard privacy update.');
}

// Second mandatory argument is privacy
const privacy = args['privacy'];
if (typeof privacy !== 'number' || (privacy !== 0 && privacy !== 1)) {
throw new Error('please enter 0/1 for public/private.');
}

// Adjust privacy
await changeUserPrivacy(user.id, <number>privacy);

// Get new privacy
const newPrivacy = await getUserPrivacy(user.id);
return `${user.username} is now ${newPrivacy ? 'private' : 'not private'}.`;
};

export const coinPrivacyCommandDetails: CodeyCommandDetails = {
name: 'privacy',
aliases: ['p'],
description: 'Update the leaderboard privacy of a user.',
detailedDescription: `**Examples:**
\`${container.botPrefix}coin privacy @Codey 1\``,

isCommandResponseEphemeral: false,
messageWhenExecutingCommand: 'Updating leaderboard privacy...',
executeCommand: coinPrivacyExecuteCommand,
codeyCommandResponseType: CodeyCommandResponseType.STRING,

options: [
{
name: 'user',
description: 'The user to adjust the privacy of,',
type: CodeyCommandOptionType.USER,
required: true
},
{
name: 'privacy',
description: 'The privacy to set the specified user to,',
type: CodeyCommandOptionType.NUMBER,
Copy link
Contributor

Choose a reason for hiding this comment

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

This would make more sense as a CodeyCommandOptionType.BOOLEAN

required: true
}
],
subcommandDetails: {}
};
6 changes: 5 additions & 1 deletion src/commands/coin/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { coinCheckCommandDetails } from '../../commandDetails/coin/check';
import { coinInfoCommandDetails } from '../../commandDetails/coin/info';
import { coinUpdateCommandDetails } from '../../commandDetails/coin/update';
import { coinCurrentLeaderboardCommandDetails } from '../../commandDetails/coin/leaderboard';
import { coinPrivacyCommandDetails } from '../../commandDetails/coin/privacy';

const coinCommandDetails: CodeyCommandDetails = {
name: 'coin',
Expand All @@ -19,13 +20,16 @@ const coinCommandDetails: CodeyCommandDetails = {
\`${container.botPrefix}coin info\`
\`${container.botPrefix}coin i\`
\`${container.botPrefix}coin update @Codey 100\`
\`${container.botPrefix}coin update @Codey 0 Reset Codey's balance.\``,
\`${container.botPrefix}coin u @Codey 0 Reset Codey's balance.\`
\`${container.botPrefix}coin privacy @Codey 1\`
\`${container.botPrefix}coin p @Codey 0\``,
options: [],
subcommandDetails: {
adjust: coinAdjustCommandDetails,
check: coinCheckCommandDetails,
info: coinInfoCommandDetails,
update: coinUpdateCommandDetails,
privacy: coinPrivacyCommandDetails,
leaderboard: coinCurrentLeaderboardCommandDetails
},
defaultSubcommandDetails: coinCheckCommandDetails
Expand Down
24 changes: 24 additions & 0 deletions src/components/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,30 @@ export const getCoinBalanceByUserId = async (userId: string): Promise<number> =>
return _.get(res, 'balance', 0);
};

/*
Returns 1 or 0 for whether user is private
*/
export const getUserPrivacy = async (userId: string): Promise<number> => {
const db = await openDB();
// Query user privacy from DB.
const res = await db.get('SELECT is_private FROM user_coin WHERE user_id = ?', userId);
Copy link
Collaborator

Choose a reason for hiding this comment

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

corresponding update statement for how ppl can set that they wanna be private?
also might need to change DB schema code for new column

Copy link
Member Author

Choose a reason for hiding this comment

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

Hope I got everything...

// If user doesn't have a privacy value, default to false (public).
return _.get(res, 'is_private', 0);
};

export const changeUserPrivacy = async (userId: string, privacy: number): Promise<void> => {
const db = await openDB();
await db.run(
`
INSERT INTO user_coin (user_id, is_private) VALUES (?, ?)
ON CONFLICT(user_id)
DO UPDATE SET is_private = ?`,
userId,
privacy,
privacy
);
};

/*
If user doesn't exist, create row with newBalance as the balance.
Otherwise, update balance to newBalance.
Expand Down
3 changes: 2 additions & 1 deletion src/components/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ const initUserCoinTable = async (db: Database): Promise<void> => {
`
CREATE TABLE IF NOT EXISTS user_coin (
user_id VARCHAR(255) PRIMARY KEY NOT NULL,
balance INTEGER NOT NULL CHECK(balance>=0)
balance INTEGER NOT NULL CHECK(balance>=0),
is_private INTEGER CHECK(is_private IN (0, 1))
)
`
);
Expand Down