我如何使用discord.py读取bot DM并将其转发到频道?

时间:2020-05-30 14:48:36

标签: python discord discord.py

我正在尝试创建一个可以接收DM并将其与发送DM的人的用户名和用户ID一起转发到服务器中特定通道的消息的机器人。这是我尝试过的。尽管我认为我的其余代码可能会影响到它,但这似乎没有用。

@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)

我的其余代码可以在here中找到。

1 个答案:

答案 0 :(得分:1)

尝试使用message.channel == message.author.dm_channel和与.format()相对的f字符串。

@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)