Skip to content

Commit

Permalink
Merge pull request #507 from uwcsc/issue-#506
Browse files Browse the repository at this point in the history
Send receiver a dm on CodeyCoin transfer to notify them
  • Loading branch information
Fan-Yang-284 authored Jan 27, 2024
2 parents 056ee25 + fcb951f commit 1813bd7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
39 changes: 35 additions & 4 deletions src/commandDetails/coin/transfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { container } from '@sapphire/framework';
import { User } from 'discord.js';
import { SapphireClient, container } from '@sapphire/framework';
import { CommandInteraction, EmbedBuilder, Message, User, userMention } from 'discord.js';
import {
CodeyCommandOptionType,
CodeyCommandDetails,
Expand Down Expand Up @@ -41,6 +41,11 @@ const coinTransferExecuteCommand: SapphireMessageExecuteType = async (
return new SapphireMessageResponseWithMetadata(`You can't transfer to yourself.`, {});
}

// Ensure the transfer involves a non-bot recipient
if (receivingUser.bot) {
return new SapphireMessageResponseWithMetadata(`You can't transfer to bots.`, {});
}

// Retrieve sending user balance and ensure transferred amount is valid
const senderBalance = await getCoinBalanceByUserId(sendingUser.id);
if (amount > senderBalance) {
Expand All @@ -63,12 +68,38 @@ const coinTransferExecuteCommand: SapphireMessageExecuteType = async (

return new SapphireMessageResponseWithMetadata(await transfer.getTransferResponse(), {
transferId: transfer.transferId,
_client: client,
receiver: receivingUser,
});
};

const transferAfterMessageReply: SapphireAfterReplyType = async (result, sentMessage) => {
if (typeof result.metadata.transferId === 'undefined') return;
// Store the message which the game takes place in the game object

// Send a dm to the transfer receiver
const receivingUser = <User>result.metadata['receiver'];
const client = <SapphireClient>result.metadata['_client'];

const mentionUser = userMention(receivingUser.id);

const message = <Message>sentMessage;
let messageLink = message.url;
if (!messageLink) {
// if command was run as a slash command, the above will fail
const commandInteraction = <CommandInteraction>sentMessage;
const messageReply = await commandInteraction.fetchReply();
messageLink = messageReply.url;
}
const transferPingMessage = `Hey ${mentionUser}, you've received a CodeyCoin transfer!
Check it out here: ${messageLink}`;

const transferPingEmbed = new EmbedBuilder()
.setColor('Green')
.setTitle('New CodeyCoin Transfer!')
.setDescription(transferPingMessage);
client.users.send(receivingUser, { embeds: [transferPingEmbed] });

transferTracker.runFuncOnTransfer(<string>result.metadata.transferId, (transfer) => {
transfer.transferMessage = sentMessage;
});
Expand All @@ -83,7 +114,7 @@ export const coinTransferCommandDetails: CodeyCommandDetails = {
\`${container.botPrefix}coin transfer @Codey 10 Lost a bet to @Codey\``,

isCommandResponseEphemeral: false,
messageWhenExecutingCommand: 'Transferring coins...',
messageWhenExecutingCommand: 'Setting up the transaction...',
afterMessageReply: transferAfterMessageReply,
executeCommand: coinTransferExecuteCommand,
options: [
Expand Down
10 changes: 3 additions & 7 deletions src/components/coin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -443,19 +443,15 @@ export class Transfer {
case TransferResult.Confirmed:
const newReceiverBalance = await getCoinBalanceByUserId(this.state.receiver.id);
const newSenderBalance = await getCoinBalanceByUserId(this.state.sender.id);
return `${this.state.receiver.username} accepted the transfer. ${
this.state.receiver.username
} now has ${newReceiverBalance} Codey ${pluralize(
return `${this.state.receiver.toString()} accepted the transfer. ${this.state.receiver.toString()} now has ${newReceiverBalance} Codey ${pluralize(
'coin',
newReceiverBalance,
)} ${getCoinEmoji()}. ${
this.state.sender.username
} now has ${newSenderBalance} Codey ${pluralize(
)} ${getCoinEmoji()}. ${this.state.sender.toString()} now has ${newSenderBalance} Codey ${pluralize(
'coin',
newSenderBalance,
)} ${getCoinEmoji()}.`;
case TransferResult.Rejected:
return `This transfer was rejected by ${this.state.receiver.username}.`;
return `This transfer was rejected by ${this.state.receiver.toString()}.`;
case TransferResult.Pending:
return 'Please choose whether you would like to accept this transfer.';
case TransferResult.Invalid:
Expand Down

0 comments on commit 1813bd7

Please sign in to comment.