Discord.py-使用特定消息回复DM

时间:2020-09-08 23:06:50

标签: python python-3.x discord discord.py discord.py-rewrite

试图让我的机器人用“这是一个dm”回复DM,但是没有运气,这是我的代码:

@client.event
async def on_message(message):
    if message.guild == null:
        await message.channel.send('this is a dm')
    else:
        pass

我也尝试过使用它:

@client.event
async def on_message(message):
    if isinstance(message.channel, discord.channel.DMChannel):
        await ctx.send('This is a DM')

关于最后一个,由于上下文(ctx),我得到了一个错误

2 个答案:

答案 0 :(得分:0)

尝试一下:

import discord

client = discord.Client()

@client.event
async def on_message(message):
    if isinstance(message.channel, discord.channel.DMChannel) and message.author != client.user:
        await message.channel.send('This is a DM')

client.run("KEY")```

答案 1 :(得分:0)

您发送的第一个代码使用null,这在python中并不是问题。第二个有点不必要,请改用它:

@client.event
async def on_message(message):
    if not message.guild:
        await message.channel.send('this is a dm')