From 73c64a63d20e4c9b143c9ca1c257fc2cda23f058 Mon Sep 17 00:00:00 2001 From: Fan-Yang Date: Fri, 26 Jan 2024 18:00:47 -0500 Subject: [PATCH 1/3] send receiver user a dm on transfer to notify them --- src/commandDetails/coin/transfer.ts | 32 +++++++++++++++++++++++++---- src/components/coin.ts | 10 +++------ 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/commandDetails/coin/transfer.ts b/src/commandDetails/coin/transfer.ts index fe850e2e..b955e625 100644 --- a/src/commandDetails/coin/transfer.ts +++ b/src/commandDetails/coin/transfer.ts @@ -1,5 +1,5 @@ -import { container } from '@sapphire/framework'; -import { User } from 'discord.js'; +import { SapphireClient, container } from '@sapphire/framework'; +import { CommandInteraction, Embed, EmbedBuilder, Message, User, userMention } from 'discord.js'; import { CodeyCommandOptionType, CodeyCommandDetails, @@ -10,6 +10,7 @@ import { } from '../../codeyCommand'; import { getCoinBalanceByUserId, transferTracker } from '../../components/coin'; import { getCoinEmoji } from '../../components/emojis'; +import { receiveMessageOnPort } from 'worker_threads'; const coinTransferExecuteCommand: SapphireMessageExecuteType = async ( client, @@ -63,12 +64,35 @@ 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 = result.metadata['receiver']; + const client = result.metadata['_client']; + + const mentionUser = userMention(receivingUser.id); + + // do not attempt sending dms to discord bots + if (!receivingUser.bot) { + const message = sentMessage; + const messageLink = message.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(result.metadata.transferId, (transfer) => { transfer.transferMessage = sentMessage; }); @@ -83,7 +107,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: [ diff --git a/src/components/coin.ts b/src/components/coin.ts index c66ce141..65ef1679 100644 --- a/src/components/coin.ts +++ b/src/components/coin.ts @@ -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: From 7b7ce6003b7efed81e18084c587b5639e4ea9cc6 Mon Sep 17 00:00:00 2001 From: Fan-Yang Date: Fri, 26 Jan 2024 18:22:07 -0500 Subject: [PATCH 2/3] fix linter errors/remove unneeded imports --- src/commandDetails/coin/transfer.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/commandDetails/coin/transfer.ts b/src/commandDetails/coin/transfer.ts index b955e625..af8ae752 100644 --- a/src/commandDetails/coin/transfer.ts +++ b/src/commandDetails/coin/transfer.ts @@ -1,5 +1,5 @@ import { SapphireClient, container } from '@sapphire/framework'; -import { CommandInteraction, Embed, EmbedBuilder, Message, User, userMention } from 'discord.js'; +import { EmbedBuilder, Message, User, userMention } from 'discord.js'; import { CodeyCommandOptionType, CodeyCommandDetails, @@ -10,7 +10,6 @@ import { } from '../../codeyCommand'; import { getCoinBalanceByUserId, transferTracker } from '../../components/coin'; import { getCoinEmoji } from '../../components/emojis'; -import { receiveMessageOnPort } from 'worker_threads'; const coinTransferExecuteCommand: SapphireMessageExecuteType = async ( client, From fcb951ff858f99b0c47ca6e6f3aed7014cebc893 Mon Sep 17 00:00:00 2001 From: Fan-Yang Date: Sat, 27 Jan 2024 11:13:34 -0500 Subject: [PATCH 3/3] fix dm error on slash command, prevent transfers to bots --- src/commandDetails/coin/transfer.ts | 32 ++++++++++++++++++----------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/commandDetails/coin/transfer.ts b/src/commandDetails/coin/transfer.ts index af8ae752..ed5c4b92 100644 --- a/src/commandDetails/coin/transfer.ts +++ b/src/commandDetails/coin/transfer.ts @@ -1,5 +1,5 @@ import { SapphireClient, container } from '@sapphire/framework'; -import { EmbedBuilder, Message, User, userMention } from 'discord.js'; +import { CommandInteraction, EmbedBuilder, Message, User, userMention } from 'discord.js'; import { CodeyCommandOptionType, CodeyCommandDetails, @@ -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) { @@ -77,20 +82,23 @@ const transferAfterMessageReply: SapphireAfterReplyType = async (result, sentMes const mentionUser = userMention(receivingUser.id); - // do not attempt sending dms to discord bots - if (!receivingUser.bot) { - const message = sentMessage; - const messageLink = message.url; - const transferPingMessage = `Hey ${mentionUser}, you've received a CodeyCoin transfer! + const message = sentMessage; + let messageLink = message.url; + if (!messageLink) { + // if command was run as a slash command, the above will fail + const 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] }); - } + const transferPingEmbed = new EmbedBuilder() + .setColor('Green') + .setTitle('New CodeyCoin Transfer!') + .setDescription(transferPingMessage); + client.users.send(receivingUser, { embeds: [transferPingEmbed] }); transferTracker.runFuncOnTransfer(result.metadata.transferId, (transfer) => { transfer.transferMessage = sentMessage;