简体   繁体   中英

How do I read bot DMs and forward it to a channel using discord.py?

I'm trying to make a bot that recieves DMs and forwards the message to a specific channel in a server along with the username and user id of whoever sent the DM. Here's what I've tried. It doesn't seem to work, although I think there's a possibility that the rest of my code may be affecting it.

@client.event
async def on_message(message):
    channel = client.get_channel(714242239215304745)
    if message.guild is None and message.author != client.user:
        embed = discord.Embed(
            title = 'Support requested!',
            description = '{}' .format(message.content),
            color = discord.Color.from_rgb(r=159, g=255, b=255)
            )
        embed.set_footer(text='Requested by {} | ID-{}' .format(message.author, message.author.id))
        await channel.send(embed=embed)
        print("Support requested by {} | ID-{}!" .format(message.author, message.author.id))
        print("Content: '{}'." .format(message.content))
    await client.process_commands(message)

The rest of my code can be found here .

Try using message.channel == message.author.dm_channel , and f-strings opposed to .format() .

@client.event
async def on_message(message):
    channel = client.get_channel(714242239215304745) # are you sure this channel exists?
    if message.channel == message.author.dm_channel: # do not use guild == None, as group dms might satisfy this, and you can't message yourself, no need to check client user
        embed = discord.Embed(
            title = 'Support requested!',
            description = f'{message.content}',
            color = 0x9fffff
            )
        embed.set_footer(text=f'Requested by {message.author.display_name} | ID-{message.author.id}')
        await channel.send(embed=embed)
        print("Support requested by {} | ID-{}!" .format(message.author, message.author.id))
        print("Content: '{}'." .format(message.content))
    await client.process_commands(message)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM