Skip to content
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

Coin prune #536

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion docs/COMMAND-WIKI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- **Description:** Handle coin functions.
- **Examples:**<br>`.coin adjust @Codey 100`<br>`.coin adjust @Codey -100 Codey broke.`<br>`.coin`<br>`.coin check @Codey`<br>`.coin c @Codey`<br>`.coin info`<br>`.coin i`<br>`.coin update @Codey 100`<br>`.coin update @Codey 0 Reset Codey's balance.`<br>`.coin transfer @Codey 10`<br>`.coin transfer @Codey 15 Lost a bet to Codey `
- **Options:** None
- **Subcommands:** `adjust`, `check`, `info`, `update`, `leaderboard`, `transfer`
- **Subcommands:** `adjust`, `check`, `info`, `update`, `leaderboard`, `transfer`, `prune`

## coin adjust
- **Aliases:** `a`
Expand Down Expand Up @@ -48,6 +48,14 @@
- **Options:** None
- **Subcommands:** None

## coin prune
- **Aliases:** `p`
- **Description:** The reason why we are pruning the balances.
- **Examples:**<br>`.coin prune @Codey 100`<br>`.coin prune @Codey -100 Codey broke.`
- **Options:**
- ``reason``: The reason why we are pruning the balances.
- **Subcommands:** None

## coin transfer
- **Aliases:** `t`
- **Description:** Transfer coins from your balance to another user.
Expand Down
94 changes: 94 additions & 0 deletions src/commandDetails/coin/prune.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import { CodeyUserError } from './../../codeyUserError';
import { container } from '@sapphire/framework';
import { ChatInputCommandInteraction, PermissionsBitField } from 'discord.js';
import {
CodeyCommandDetails,
CodeyCommandOptionType,
SapphireMessageExecuteType,
SapphireMessageResponse,
} from '../../codeyCommand';
import {
updateCoinBalanceByUserId,
getCoinBalanceByUserId,
UserCoinEvent,
} from '../../components/coin';

// Divide everyone's coin counts by given divisor to ensure the leaderboard is populated with more active members
// Designed to be used at the end of a term
const coinPruneExecuteCommand: SapphireMessageExecuteType = async (
client,
messageFromUser,
args,
): Promise<SapphireMessageResponse> => {
if (
!(<Readonly<PermissionsBitField>>messageFromUser.member?.permissions).has(
PermissionsBitField.Flags.Administrator,
)
) {
throw new CodeyUserError(messageFromUser, `You do not have permission to use this command.`);
}

// First and only mandatory argument is divisor
const divisor = <number>args['divisor']; // Cast divisor to a number to allow arithmetic operations
if (!divisor || divisor <= 1) {
throw new CodeyUserError(messageFromUser, 'Please enter a valid number to divide by.');
}

// Optional argument is reason
const reason = args['reason'];

if (messageFromUser instanceof ChatInputCommandInteraction) {
await (<ChatInputCommandInteraction>messageFromUser).deferReply();
}
// Prune coin balance of all users
const allMembers = await messageFromUser.guild?.members.fetch();
if (allMembers) {
for (const member of allMembers.values()) {
const currentBalance: number = await getCoinBalanceByUserId(member.user.id);
await updateCoinBalanceByUserId(
member.user.id,
<number>Math.round(currentBalance / divisor),
UserCoinEvent.AdminCoinPrune,
<string>(reason ? reason : ''),
client.user?.id,
);
}
}
// The message to be displayed after the command has been completed
const returnMessage = `Divided all users' coin balances by ${divisor}.`;

if (messageFromUser instanceof ChatInputCommandInteraction) {
await (<ChatInputCommandInteraction>messageFromUser).editReply(returnMessage);
} else {
await messageFromUser.channel?.send(returnMessage);
}
return;
};

export const coinPruneCommandDetails: CodeyCommandDetails = {
name: 'prune',
aliases: ['p'],
description: "Divide every users' coin balance by the passed argument.",
detailedDescription: `**Examples:**
\`${container.botPrefix}coin prune @Codey 100\`
\`${container.botPrefix}coin prune @Codey -100 Codey broke.\``,

isCommandResponseEphemeral: false,
messageWhenExecutingCommand: 'Pruning...',
executeCommand: coinPruneExecuteCommand,
options: [
{
name: 'divisor',
description: "The number to divide all users' coin balances by.",
type: CodeyCommandOptionType.NUMBER,
required: true,
},
{
name: 'reason',
description: 'The reason why we are pruning the balances.',
type: CodeyCommandOptionType.STRING,
required: false,
},
],
subcommandDetails: {},
};
2 changes: 2 additions & 0 deletions src/commands/coin/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { coinInfoCommandDetails } from '../../commandDetails/coin/info';
import { coinLeaderboardCommandDetails } from '../../commandDetails/coin/leaderboard';
import { coinTransferCommandDetails } from '../../commandDetails/coin/transfer';
import { coinUpdateCommandDetails } from '../../commandDetails/coin/update';
import { coinPruneCommandDetails } from '../../commandDetails/coin/prune';

const coinCommandDetails: CodeyCommandDetails = {
name: 'coin',
Expand All @@ -31,6 +32,7 @@ const coinCommandDetails: CodeyCommandDetails = {
update: coinUpdateCommandDetails,
leaderboard: coinLeaderboardCommandDetails,
transfer: coinTransferCommandDetails,
prune: coinPruneCommandDetails,
},
defaultSubcommandDetails: coinCheckCommandDetails,
};
Expand Down
1 change: 1 addition & 0 deletions src/components/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum BonusType {

export enum UserCoinEvent {
AdminCoinAdjust,
AdminCoinPrune,
AdminCoinUpdate,
BonusDaily,
BonusActivity,
Expand Down
Loading