简体   繁体   中英

Check if a message was sent by a user in a DM (discord.py) then send their message from a direct message into a certain channel?

msg_dump_channel = 1234
@bot.event
async def on_message(message: discord.Message):
    channel = bot.get_channel(msg_dump_channel)
    if str(message.author) == "user":
        await channel.send(message.content)
    await bot.process_commands(message)

This is my code, I know that DMs don't have a guild, so how would you write it for a DM?

discord.Message objects have a channel attribute that you can use for your check:

@bot.event
async def on_messsage(message)
    if isinstance(message.channel, discord.DMChannel) and str(message.author) ==  'user':
        channel = bot.get_channel(channel_id)
        await channel.send(message.content)
    else:
        pass

    bot.process_commands(message)
        

this should work for an example

@bot.event
async def on_message(message)
guild = message.guild
if not guild:
    print(" DM: {0.author.name} : {0.content}".format(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