I made a command to DM a specific user and then output a message that it has been successful in the same channel, but for no reason at all it sends 2 DM’s and 2 success messages.
<code>@bot.tree.command(name="dm", description="Send a DM to a user")
async def dm(interaction: discord.Interaction, target_user: str, message_to_send: str):
if (interaction.channel.id != [channel id]):
return
target_user = int(target_user.removeprefix('<@').removesuffix('>'))
if (sent_messages.get(target_user) is not True):
await bot.get_user(target_user).send(message_to_send)
await interaction.response.send_message(f"Message sent successfully to <@{target_user}>", ephemeral=False)
sent_messages[target_user] = True
else:
return
</code>
<code>@bot.tree.command(name="dm", description="Send a DM to a user")
async def dm(interaction: discord.Interaction, target_user: str, message_to_send: str):
if (interaction.channel.id != [channel id]):
return
target_user = int(target_user.removeprefix('<@').removesuffix('>'))
if (sent_messages.get(target_user) is not True):
await bot.get_user(target_user).send(message_to_send)
await interaction.response.send_message(f"Message sent successfully to <@{target_user}>", ephemeral=False)
sent_messages[target_user] = True
else:
return
</code>
@bot.tree.command(name="dm", description="Send a DM to a user")
async def dm(interaction: discord.Interaction, target_user: str, message_to_send: str):
if (interaction.channel.id != [channel id]):
return
target_user = int(target_user.removeprefix('<@').removesuffix('>'))
if (sent_messages.get(target_user) is not True):
await bot.get_user(target_user).send(message_to_send)
await interaction.response.send_message(f"Message sent successfully to <@{target_user}>", ephemeral=False)
sent_messages[target_user] = True
else:
return
I have even added a sent
variable to indicate if the message is sent or not, even though it shouldn’t be needed. I added print statements and they only activated once.
New contributor
Robbie is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.