首页 > 解决方案 > 建议命令在 discord.py 中不起作用

问题描述

我目前正在discord.py中编写一个机器人,它执行诸如kick之类的mod命令。我正在尝试制作一个命令,您可以向服务器建议一些东西,就像他们制作一个称为suggest-channel的频道一样,当他们执行命令它将其发送到该通道。我有一个命令可以向特定频道发送消息以获取机器人建议,但这是我得到的最接近的。下面是我在 cogs 中的建议命令的代码,是的,我使用命令处理。

@commands.command()
async def suggest(self ,ctx , *, suggestion):
    embed = discord.Embed(title=f"Suggestion from {ctx.author.name}", description=suggestion, color=discord.Color.blue())
    embed.set_footer(text=f"Suggestion created by {ctx.author}")
    channel = discord.utils.get(message.server.channels, name="suggestion-channel")
    message = await channel.send(embed=embed)
    await message.add_reaction("✅")
    await message.add_reaction("❌")
    await ctx.message.delete()
    await ctx.send("Thank you for your suggestion!")

标签: pythondiscorddiscord.py

解决方案


首先,您没有在on_message事件中获取频道,或者使用用户的消息来获取服务器的频道。您必须替换messagectx. 其次,ctx.server自 discord.py 迁移后不再有效。它现在被称为ctx.guild,您可以在他们的文档中阅读。

    channel = discord.utils.get(message.server.channels, name="suggestion-channel")
    # change this to:
    channel = discord.utils.get(ctx.guild.channels, name="suggestion-channel")

更改代码工作

否则,如果您发现任何其他命令都不起作用,则您可能尚未处理您的on_message事件。您可以查看有关如何解决此问题的问题:


推荐阅读