Skip to content

Commit 1813bd7

Browse files
authoredJan 27, 2024
Merge pull request #507 from uwcsc/issue-#506
Send receiver a dm on CodeyCoin transfer to notify them
2 parents 056ee25 + fcb951f commit 1813bd7

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed
 

‎src/commandDetails/coin/transfer.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { container } from '@sapphire/framework';
2-
import { User } from 'discord.js';
1+
import { SapphireClient, container } from '@sapphire/framework';
2+
import { CommandInteraction, EmbedBuilder, Message, User, userMention } from 'discord.js';
33
import {
44
CodeyCommandOptionType,
55
CodeyCommandDetails,
@@ -41,6 +41,11 @@ const coinTransferExecuteCommand: SapphireMessageExecuteType = async (
4141
return new SapphireMessageResponseWithMetadata(`You can't transfer to yourself.`, {});
4242
}
4343

44+
// Ensure the transfer involves a non-bot recipient
45+
if (receivingUser.bot) {
46+
return new SapphireMessageResponseWithMetadata(`You can't transfer to bots.`, {});
47+
}
48+
4449
// Retrieve sending user balance and ensure transferred amount is valid
4550
const senderBalance = await getCoinBalanceByUserId(sendingUser.id);
4651
if (amount > senderBalance) {
@@ -63,12 +68,38 @@ const coinTransferExecuteCommand: SapphireMessageExecuteType = async (
6368

6469
return new SapphireMessageResponseWithMetadata(await transfer.getTransferResponse(), {
6570
transferId: transfer.transferId,
71+
_client: client,
72+
receiver: receivingUser,
6673
});
6774
};
6875

6976
const transferAfterMessageReply: SapphireAfterReplyType = async (result, sentMessage) => {
7077
if (typeof result.metadata.transferId === 'undefined') return;
71-
// Store the message which the game takes place in the game object
78+
79+
// Send a dm to the transfer receiver
80+
const receivingUser = <User>result.metadata['receiver'];
81+
const client = <SapphireClient>result.metadata['_client'];
82+
83+
const mentionUser = userMention(receivingUser.id);
84+
85+
const message = <Message>sentMessage;
86+
let messageLink = message.url;
87+
if (!messageLink) {
88+
// if command was run as a slash command, the above will fail
89+
const commandInteraction = <CommandInteraction>sentMessage;
90+
const messageReply = await commandInteraction.fetchReply();
91+
messageLink = messageReply.url;
92+
}
93+
const transferPingMessage = `Hey ${mentionUser}, you've received a CodeyCoin transfer!
94+
95+
Check it out here: ${messageLink}`;
96+
97+
const transferPingEmbed = new EmbedBuilder()
98+
.setColor('Green')
99+
.setTitle('New CodeyCoin Transfer!')
100+
.setDescription(transferPingMessage);
101+
client.users.send(receivingUser, { embeds: [transferPingEmbed] });
102+
72103
transferTracker.runFuncOnTransfer(<string>result.metadata.transferId, (transfer) => {
73104
transfer.transferMessage = sentMessage;
74105
});
@@ -83,7 +114,7 @@ export const coinTransferCommandDetails: CodeyCommandDetails = {
83114
\`${container.botPrefix}coin transfer @Codey 10 Lost a bet to @Codey\``,
84115

85116
isCommandResponseEphemeral: false,
86-
messageWhenExecutingCommand: 'Transferring coins...',
117+
messageWhenExecutingCommand: 'Setting up the transaction...',
87118
afterMessageReply: transferAfterMessageReply,
88119
executeCommand: coinTransferExecuteCommand,
89120
options: [

‎src/components/coin.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -443,19 +443,15 @@ export class Transfer {
443443
case TransferResult.Confirmed:
444444
const newReceiverBalance = await getCoinBalanceByUserId(this.state.receiver.id);
445445
const newSenderBalance = await getCoinBalanceByUserId(this.state.sender.id);
446-
return `${this.state.receiver.username} accepted the transfer. ${
447-
this.state.receiver.username
448-
} now has ${newReceiverBalance} Codey ${pluralize(
446+
return `${this.state.receiver.toString()} accepted the transfer. ${this.state.receiver.toString()} now has ${newReceiverBalance} Codey ${pluralize(
449447
'coin',
450448
newReceiverBalance,
451-
)} ${getCoinEmoji()}. ${
452-
this.state.sender.username
453-
} now has ${newSenderBalance} Codey ${pluralize(
449+
)} ${getCoinEmoji()}. ${this.state.sender.toString()} now has ${newSenderBalance} Codey ${pluralize(
454450
'coin',
455451
newSenderBalance,
456452
)} ${getCoinEmoji()}.`;
457453
case TransferResult.Rejected:
458-
return `This transfer was rejected by ${this.state.receiver.username}.`;
454+
return `This transfer was rejected by ${this.state.receiver.toString()}.`;
459455
case TransferResult.Pending:
460456
return 'Please choose whether you would like to accept this transfer.';
461457
case TransferResult.Invalid:

0 commit comments

Comments
 (0)