如何在Discord.py中发出公告命令?

时间:2018-10-22 00:17:18

标签: python discord 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,并宣布(嵌入)任何键入的内容。

1 个答案:

答案 0 :(得分:0)

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

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

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