处理私人消息 discord.py

Handling private messages discord.py

如何处理 discord.py 中的所有私人消息? 我想知道是否有办法对所有私人消息回复相同的消息,例如“您不能在私人消息中使用命令” 我知道当消息是私有的时函数会引发错误,但我不想对每个函数使用 try 和 except

另外如果私下只能使用help命令就更好了

如果您正在使用 discord.ext.commands,您可以从这个开始

@bot.command()
@commands.guild_only()
async def something(ctx):
......

关于帮助命令,你可以让他在服务器中输入命令,然后return私下给他。这将增加对 ✅ 的反应,并私下向他发送嵌入的所有帮助

bot.remove_command("help")

@bot.command()
async def help(ctx):
    embed = discord.Embed(
        title="Help")

    embed.set_footer(
        text="Enjoy, in case of issues contact AZ#0573")
    await ctx.author.send(embed=embed)
    await ctx.message.add_reaction("✅")

如果需要,请将客户端替换为机器人

@client.event
async def on_message(message):
    if message.author.id != client.user.id:
        if message.guild:  # If message in guild
            await client.process_commands(message)  # Process command
        else:
            return await message.author.send("Sorry, but i dont process commands on direct messages...")