1
votes

Issue sending a private message in Discord. Following are two samples of code (simplified).

@client.event
async def on_message(message):

this code block sends back the message (cookie emoji) to the user regardless if its a private chat (with the bot) or channel

    if message.content == ".cookie":
        await client.delete_message(message)
        await client.send_message(message.channel, ":cookie:")

this block of code sends a message to the user privately given that the user sent a command in the channel, it works, im happy with this.

    if message.content == ".cookie":
        await client.delete_message(message)
        await client.send_message(message.author, ":cookie:")

the issue is, I would like the user to be able to send a message ".cookie" in the channel or in the private message to the bot and get the a private message (cookie emoji) back.

with the last block of code. if I send a command .cookie to the channel, I get a private message from the bot. If I send a command .cookie to the Bot privately, I get an error.

not sure how to handle this, thank you

1

1 Answers

1
votes

I don't think it's possible to delete private messages that people send you. You can add a check on the channel to see if it's private before deleting the message

if message.content == ".cookie":
    if not message.channel.is_private:
        await client.delete_message(message)
    await client.send_message(message.author, ":cookie:")