python - 如何在 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 的地方,它会宣布(嵌入)输入的任何内容。
解决方案
您需要pass_context=True
添加@bot.command()
将您的第一行更改为:@bot.command(brief='announce [message]', pass_context=True)
这会将消息的上下文(消息数据类)作为第一个参数 ctx 传递给函数。
推荐阅读
- jackson - 在 SnakeYAML 中禁用本机类型 ID
- ansible - 执行剧本时假设创建资源组在执行剧本时出错
- java - 如何在 Spring Boot 中运行 CommandLineRunner
- swiftui - SwiftUI 菜单/上下文菜单中的标题
- azure - Powershell 过滤
- stored-procedures - 发生冲突时如何使用新值进行更新
- javascript - React-Native:无法在模式内滚动平面列表
- django - 登录测试期间身份验证失败
- php - 包含两个 '.' 的无括号表达式的行为 并且 '+'/'-' 将在 PHP 8 中更改:'+'/'-' 将具有更高的优先级
- javascript - 失败:此驱动程序实例没有有效的会话 ID(您是否调用了 WebDriver.quit()?)并且可能不再使用