-
Notifications
You must be signed in to change notification settings - Fork 18
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
required: true | ||
} | ||
], | ||
subcommandDetails: {} | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. | ||
|
There was a problem hiding this comment.
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