处理私有消息discord.py

2024-05-26 11:55:24 发布

您现在位置:Python中文网/ 问答频道 /正文

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

另外,如果只有help命令可以私下使用也会更好


Tags: 方法函数py命令消息错误help私人
2条回答

如果需要,请将客户端重设为bot

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

如果您使用的是discord.ext.commands,那么可以从以下内容开始

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

关于帮助命令,您可以让他在服务器中键入命令,然后私下将其返回给他。这会增加反应的速度✅ 私下把所有的帮助都交给他

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("✅")

相关问题 更多 >