Skip to content

Commit c33bba8

Browse files
committed
Add privacy update command and update DB schema
1 parent 60cca2f commit c33bba8

File tree

4 files changed

+88
-3
lines changed

4 files changed

+88
-3
lines changed

src/commandDetails/coin/privacy.ts

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { container } from '@sapphire/framework';
2+
import { Permissions, User } from 'discord.js';
3+
import {
4+
CodeyCommandDetails,
5+
CodeyCommandOptionType,
6+
CodeyCommandResponseType,
7+
SapphireMessageExecuteType
8+
} from '../../codeyCommand';
9+
import { getUserPrivacy, changeUserPrivacy } from '../../components/coin';
10+
11+
// Update coin leaderboard privacy of a user
12+
const coinUpdateExecuteCommand: SapphireMessageExecuteType = async (client, messageFromUser, args) => {
13+
if (!(<Readonly<Permissions>>messageFromUser.member?.permissions).has('ADMINISTRATOR')) {
14+
return `You do not have permission to use this command.`;
15+
}
16+
17+
// First mandatory argument is user
18+
const user = <User>args['user'];
19+
if (!user) {
20+
throw new Error('please enter a valid user mention or ID for balance adjustment.');
21+
}
22+
23+
// Second mandatory argument is privacy
24+
const privacy = args['privacy'];
25+
if (typeof privacy !== 'number' || (privacy !== 0 && privacy !== 1)) {
26+
throw new Error('please enter 0/1 for public/private.');
27+
}
28+
29+
// Adjust privacy
30+
await changeUserPrivacy(user.id, <number>privacy);
31+
32+
// Get new privacy
33+
const newPrivacy = await getUserPrivacy(user.id);
34+
return `${user.username} is now ${newPrivacy ? 'private' : 'not private'}.`;
35+
};
36+
37+
export const coinPrivacyCommandDetails: CodeyCommandDetails = {
38+
name: 'privacy',
39+
aliases: ['p'],
40+
description: 'Update the leaderboard privacy of a user.',
41+
detailedDescription: `**Examples:**
42+
\`${container.botPrefix}coin privacy @Codey 1\``,
43+
44+
isCommandResponseEphemeral: false,
45+
messageWhenExecutingCommand: 'Updating leaderboard privacy...',
46+
executeCommand: coinUpdateExecuteCommand,
47+
codeyCommandResponseType: CodeyCommandResponseType.STRING,
48+
49+
options: [
50+
{
51+
name: 'user',
52+
description: 'The user to adjust the privacy of,',
53+
type: CodeyCommandOptionType.USER,
54+
required: true
55+
},
56+
{
57+
name: 'privacy',
58+
description: 'The privacy to set the specified user to,',
59+
type: CodeyCommandOptionType.NUMBER,
60+
required: true
61+
}
62+
],
63+
subcommandDetails: {}
64+
};

src/commands/coin/coin.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { coinCheckCommandDetails } from '../../commandDetails/coin/check';
55
import { coinInfoCommandDetails } from '../../commandDetails/coin/info';
66
import { coinUpdateCommandDetails } from '../../commandDetails/coin/update';
77
import { coinCurrentLeaderboardCommandDetails } from '../../commandDetails/coin/leaderboard';
8+
import { coinPrivacyCommandDetails } from '../../commandDetails/coin/privacy';
89

910
const coinCommandDetails: CodeyCommandDetails = {
1011
name: 'coin',
@@ -19,13 +20,16 @@ const coinCommandDetails: CodeyCommandDetails = {
1920
\`${container.botPrefix}coin info\`
2021
\`${container.botPrefix}coin i\`
2122
\`${container.botPrefix}coin update @Codey 100\`
22-
\`${container.botPrefix}coin update @Codey 0 Reset Codey's balance.\``,
23+
\`${container.botPrefix}coin u @Codey 0 Reset Codey's balance.\`
24+
\`${container.botPrefix}coin privacy @Codey 1\`
25+
\`${container.botPrefix}coin p @Codey 0\``,
2326
options: [],
2427
subcommandDetails: {
2528
adjust: coinAdjustCommandDetails,
2629
check: coinCheckCommandDetails,
2730
info: coinInfoCommandDetails,
2831
update: coinUpdateCommandDetails,
32+
privacy: coinPrivacyCommandDetails,
2933
leaderboard: coinCurrentLeaderboardCommandDetails
3034
},
3135
defaultSubcommandDetails: coinCheckCommandDetails

src/components/coin.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,30 @@ 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> => {
84+
/*
85+
Returns 1 or 0 for whether user is private
86+
*/
87+
export const getUserPrivacy = async (userId: string): Promise<number> => {
8588
const db = await openDB();
8689
// Query user privacy from DB.
8790
const res = await db.get('SELECT is_private FROM user_coin WHERE user_id = ?', userId);
8891
// If user doesn't have a privacy value, default to false (public).
8992
return _.get(res, 'is_private', 0);
9093
};
9194

95+
export const changeUserPrivacy = async (userId: string, privacy: number): Promise<void> => {
96+
const db = await openDB();
97+
await db.run(
98+
`
99+
INSERT INTO user_coin (user_id, is_private) VALUES (?, ?)
100+
ON CONFLICT(user_id)
101+
DO UPDATE SET is_private = ?`,
102+
userId,
103+
privacy,
104+
privacy
105+
);
106+
};
107+
92108
/*
93109
If user doesn't exist, create row with newBalance as the balance.
94110
Otherwise, update balance to newBalance.

src/components/db.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ const initUserCoinTable = async (db: Database): Promise<void> => {
9292
`
9393
CREATE TABLE IF NOT EXISTS user_coin (
9494
user_id VARCHAR(255) PRIMARY KEY NOT NULL,
95-
balance INTEGER NOT NULL CHECK(balance>=0)
95+
balance INTEGER NOT NULL CHECK(balance>=0),
96+
is_private INTEGER NOT NULL CHECK(is_private IN (0, 1))
9697
)
9798
`
9899
);

0 commit comments

Comments
 (0)