Skip to content

Commit 570950c

Browse files
authored
Make /coin check default to the caller (#282)
* Make `/coin check` default to the caller `/coin check` originally had a mandatory argument of a target user to check the balance of. Now the argument defaults to the user who called the command, making it more ergonomic to use. * Remove .coin balance subcommand * Add check for if caller is checking own balance - Also generalizes getUserIDFromMessage to just get the User straight up - Updated previou references where relevant (leaderboard) - Replaces "`user` has" with `You have` if the user is calling `/coin check` on their own * Add additional aliases for `/coin check` - `'b', 'bal', 'balance'` due to the removal of the balance subcommand * Fix linter error
1 parent ea86019 commit 570950c

File tree

5 files changed

+30
-56
lines changed

5 files changed

+30
-56
lines changed

src/codeyCommand.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,17 @@ const setCommandSubcommand = (
178178
});
179179
};
180180

181-
/** Helper method to get user id from message */
182-
export const getUserIdFromMessage = (message: Message | SapphireCommand.ChatInputInteraction): string => {
181+
/**
182+
* Gets the User object from a message response.
183+
*
184+
* Retrieving the `User` depends on whether slash commands were used or not.
185+
* This method helps generalize this process.
186+
* */
187+
export const getUserFromMessage = (message: Message | SapphireCommand.ChatInputInteraction): User => {
183188
if (message instanceof Message) {
184-
return message.author.id;
189+
return message.author;
185190
} else {
186-
return message.user.id;
191+
return message.user;
187192
}
188193
};
189194

src/commandDetails/coin/balance.ts

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/commandDetails/coin/check.ts

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,44 @@ import {
55
CodeyCommandOptionType,
66
CodeyCommandResponseType,
77
SapphireMessageExecuteType,
8-
SapphireMessageResponse
8+
SapphireMessageResponse,
9+
getUserFromMessage
910
} from '../../codeyCommand';
1011
import { getCoinBalanceByUserId } from '../../components/coin';
1112
import { getCoinEmoji } from '../../components/emojis';
1213

1314
// Check a user's balance
1415
const coinCheckExecuteCommand: SapphireMessageExecuteType = async (
1516
_client,
16-
_messageFromUser,
17+
messageFromUser,
1718
args
1819
): Promise<SapphireMessageResponse> => {
19-
// Mandatory argument is user
20-
const user = <User>args['user'];
20+
let user: User;
21+
let displayMessage: string;
22+
23+
// use the caller as a default user if no argument is provided
24+
if (args['user']) {
25+
user = <User>args['user'];
26+
displayMessage = `${user.username} has`;
27+
} else {
28+
user = getUserFromMessage(messageFromUser);
29+
displayMessage = `You have`;
30+
}
2131
// Get coin balance
2232
let balance: number;
2333
try {
2434
balance = await getCoinBalanceByUserId(user.id);
2535
} catch (e) {
2636
return `Could not fetch the user's balance, contact a mod for help`;
2737
}
38+
2839
// Show coin balance
29-
return `${user.username} has ${balance} Codey coins ${getCoinEmoji()}.`;
40+
return `${displayMessage} ${balance} Codey coins ${getCoinEmoji()}.`;
3041
};
3142

3243
export const coinCheckCommandDetails: CodeyCommandDetails = {
3344
name: 'check',
34-
aliases: ['c'],
45+
aliases: ['c', 'b', 'balance', 'bal'],
3546
description: "Check a user's coin balance.",
3647
detailedDescription: `**Examples:**
3748
\`${container.botPrefix}coin check @Codey\`
@@ -47,7 +58,7 @@ export const coinCheckCommandDetails: CodeyCommandDetails = {
4758
name: 'user',
4859
description: 'The user to check the balance of,',
4960
type: CodeyCommandOptionType.USER,
50-
required: true
61+
required: false
5162
}
5263
],
5364
subcommandDetails: {}

src/commandDetails/coin/leaderboard.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { MessageEmbed, User } from 'discord.js';
33
import {
44
CodeyCommandDetails,
55
CodeyCommandResponseType,
6-
getUserIdFromMessage,
6+
getUserFromMessage,
77
SapphireMessageExecuteType,
88
SapphireMessageResponse
99
} from '../../codeyCommand';
@@ -61,7 +61,7 @@ const coinCurrentLeaderboardExecuteCommand: SapphireMessageExecuteType = async (
6161
messageFromUser,
6262
_args
6363
): Promise<SapphireMessageResponse> => {
64-
const userId = getUserIdFromMessage(messageFromUser);
64+
const userId = getUserFromMessage(messageFromUser).id;
6565
// Get extra users to filter bots later
6666
const leaderboard = await getCurrentCoinLeaderboard(limit * 2);
6767
return { embeds: [await getCurrentCoinLeaderboardEmbed(client, leaderboard, userId)] };

src/commands/coin/coin.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Command, container } from '@sapphire/framework';
22
import { CodeyCommand, CodeyCommandDetails } from '../../codeyCommand';
33
import { coinAdjustCommandDetails } from '../../commandDetails/coin/adjust';
4-
import { coinBalanceCommandDetails } from '../../commandDetails/coin/balance';
54
import { coinCheckCommandDetails } from '../../commandDetails/coin/check';
65
import { coinInfoCommandDetails } from '../../commandDetails/coin/info';
76
import { coinUpdateCommandDetails } from '../../commandDetails/coin/update';
@@ -15,8 +14,6 @@ const coinCommandDetails: CodeyCommandDetails = {
1514
\`${container.botPrefix}coin adjust @Codey 100\`
1615
\`${container.botPrefix}coin adjust @Codey -100 Codey broke.\`
1716
\`${container.botPrefix}coin\`
18-
\`${container.botPrefix}bal\`
19-
\`${container.botPrefix}balance\`
2017
\`${container.botPrefix}coin check @Codey\`
2118
\`${container.botPrefix}coin c @Codey\`
2219
\`${container.botPrefix}coin info\`
@@ -26,13 +23,12 @@ const coinCommandDetails: CodeyCommandDetails = {
2623
options: [],
2724
subcommandDetails: {
2825
adjust: coinAdjustCommandDetails,
29-
balance: coinBalanceCommandDetails,
3026
check: coinCheckCommandDetails,
3127
info: coinInfoCommandDetails,
3228
update: coinUpdateCommandDetails,
3329
leaderboard: coinCurrentLeaderboardCommandDetails
3430
},
35-
defaultSubcommandDetails: coinBalanceCommandDetails
31+
defaultSubcommandDetails: coinCheckCommandDetails
3632
};
3733

3834
export class CoinCommand extends CodeyCommand {

0 commit comments

Comments
 (0)