首页 > 解决方案 > 如何在 Discord.py 中发布公告命令?

问题描述

无论我看起来多么努力,我都找不到有关如何在 Discord.py 中执行此操作的教程,这全是 Discord.js。这是我当前的代码;

@bot.command(brief='announce [message]')
    async def announce(ctx, message : str):
            print(str(message))
            if(str(ctx.message.author) == user):
                    await ctx.send('User Authentication Successful')
                    try:
                            for chan in channels:
                                    try:
                                            channel = bot.get_channel(chan)
                                            info = discord.Embed(title='New Announcement!', description=str(message), color=0xFFFFFF)
                                            await channel.send(embed=info)
                                    except Exception as e:
                                            await ctx.send(e)
                                            await ctx.send("Error: " + str(chan))
                    except Exception as e:
                            await ctx.send(e)

这是我得到的错误。

Ignoring exception in command announce
Traceback (most recent call last):
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\bot.py", line 846, in process_commands
    yield from command.invoke(ctx)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 367, in invoke
    yield from self.prepare(ctx)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 345, in prepare
    yield from self._parse_arguments(ctx)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 304, in _parse_arguments
    transformed = yield from self.transform(ctx, param)
  File "C:\Users\mashh\AppData\Local\Programs\Python\Python36-32\lib\site-packages\discord\ext\commands\core.py", line 212, in transform
    raise MissingRequiredArgument('{0.name} is a required argument that is missing.'.format(param))
discord.ext.commands.errors.MissingRequiredArgument: ctx is a required argument that is missing.

请帮我 :(

编辑:如果有什么令人困惑的地方,我很抱歉。我正在尝试制作 -announce 命令。在我输入 -announce 的地方,它会宣布(嵌入)输入的任何内容。

标签: pythondiscorddiscord.py

解决方案


您需要pass_context=True添加@bot.command()

将您的第一行更改为:@bot.command(brief='announce [message]', pass_context=True)

这会将消息的上下文(消息数据类)作为第一个参数 ctx 传递给函数。


推荐阅读