【问题标题】:discord.py help command using discord componentsdiscord.py 使用不和谐组件的帮助命令
【发布时间】:2021-08-07 00:07:25
【问题描述】:

我正在使用 discord-components 库的“SelectOption”组件改进我的帮助命令。这是代码:-

@bot.command()
async def help(ctx):
  embed_main = discord.Embed(title = "Help!", color = discord.Color.random())
  await ctx.send(
    embed=embed_main,
    components = [
      Select(placeholder="Make A Choice!", options=[SelectOption(emoji="????", label="Moderation", value="A", description="Get Help On Moderation")])
      ]
  )
  interaction = await bot.wait_for("select_option", check=lambda i: i.component[0].value == "A")
  await interaction.respond(
    embed = discord.Embed(title = "???? Moderation Commands", color = discord.Color.random())
    embed.add_field(name = "Type `tnj.help <command name> for more info on it", value = "`kick`\n `ban`\n `unban`\n `mute`\n `unmute`\n `purge`")
    )

一切正常,但它给出了一个语法错误,并在这一行显示“预期的')'”:-

embed.add_field(name = "Type `tnj.help <command name> for more info on it", value = "`kick`\n `ban`\n `unban`\n `mute`\n `unmute`\n `purge`")

我不知道我做错了什么,任何帮助将不胜感激。

【问题讨论】:

  • 在 Python 中,赋值是纯粹的语句,不能用于需要表达式的地方。 embed = discord.Embed(title = "???? Moderation Commands", color = discord.Color.random()) 是一个语句,但在需要表达式的地方使用(作为interaction.respond() 调用的参数)。这会导致SyntaxError。此外,如果您打算将这两行用作调用的两个参数,则需要用逗号分隔它们。

标签: python discord discord.py


【解决方案1】:

很难猜出你的意图,但这段代码可能更接近你想要做的:

embed = discord.Embed(title = "? Moderation Commands", color = discord.Color.random())
await interaction.respond(
    embed,
    embed.add_field(name = "Type `tnj.help <command name> for more info on it", value = "`kick`\n `ban`\n `unban`\n `mute`\n `unmute`\n `purge`")
    )

这假定embed.add_field() 返回的值适合作为调用interaction.respond() 的第二个参数。

【讨论】:

  • 我明白你的意思,但是我在哪里构建嵌入呢?做你的建议会给embed is not defined。如果我做类似await interaction.respond( embed = discord.Embed(title = "? Moderation Commands", color = discord.Color.random()), embed.add_field(name = "Type tnj.help &lt;command name&gt; for more info on it", value = "kick\n ban\n unban\n mute\n unmute\n purge") ) 的事情,它会给positional argument cannot appear after a keyword arguments
  • 没有理由应该给embed is not defined。该分配出现在您对interaction.respond() 的异步调用之前,因此绝对应该定义它。您确定您使用的是我发布的确切代码,并且围绕它的代码是正确的吗?尝试删除代码,直到您拥有最低限度(在我的答案中使用代码 sn-p),然后查看它是否有效。您在评论中发布的其他 sn-p 将不起作用。 embed = ... 当用作参数时是关键字参数,并且必须在位置参数之后(那些没有=)。
猜你喜欢
  • 1970-01-01
  • 2021-05-07
  • 1970-01-01
  • 2020-11-06
  • 2021-09-16
  • 2022-06-11
  • 2019-03-17
  • 2019-07-16
  • 2021-01-01
相关资源
最近更新 更多