如何在discord.py中为这个斜杠命令添加选项?

huangapple go评论112阅读模式
英文:

How do I give this slash command options in discord.py

问题

  1. @bot.tree.command(name="alerttype", description="选项:The Gathering Legions、World Boss、The Helltide Rises、All Alerts")
  2. async def alerttype(interaction: discord.Interaction, alert_type: str):
  3. if interaction.guild is None:
  4. await interaction.response.send_message("此命令不能在私信中使用。")
  5. return
  6. if not interaction.user.guild_permissions.manage_channels:
  7. await interaction.response.send_message("您没有执行此命令所需的权限。")
  8. return
  9. if alert_type not in ["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]:
  10. await interaction.response.send_message("无效的警报类型。请重试。")
  11. return
  12. c.execute("UPDATE Channels SET alert_types = ? WHERE guild_id = ?", (alert_type, interaction.guild.id))
  13. conn.commit()
  14. await interaction.response.send_message(f"警报类型设置为 {alert_type}。")
英文:

How can I have this set up to take options that I predefine for the user rather than them needing to type it in themselves?

  1. @bot.tree.command(name="alerttype", description="Options: The Gathering Legions, World Boss, The Helltide Rises, All Alerts")
  2. async def alerttype(interaction: discord.Interaction, alert_type: str):
  3. if interaction.guild is None:
  4. await interaction.response.send_message("This command cannot be used in a DM.")
  5. return
  6. if not interaction.user.guild_permissions.manage_channels:
  7. await interaction.response.send_message("You do not have the required permissions to execute this command.")
  8. return
  9. if alert_type not in ["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]:
  10. await interaction.response.send_message("Invalid alert type. Please try again.")
  11. return
  12. c.execute("UPDATE Channels SET alert_types = ? WHERE guild_id = ?", (alert_type, interaction.guild.id))
  13. conn.commit()
  14. await interaction.response.send_message(f"Alert type set to {alert_type}.")
  15. ```
  16. </details>
  17. # 答案1
  18. **得分**: 1
  19. ```python
  20. bot.tree.command(name="alerttype", description="Options: The Gathering Legions, World Boss, The Helltide Rises, All Alerts")
  21. async def alerttype(interaction: discord.Interaction, alert_type: Literal["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]):
  22. if interaction.guild is None:
  23. await interaction.response.send_message("This command cannot be used in a DM.")
  24. return
  25. if not interaction.user.guild_permissions.manage_channels:
  26. await interaction.response.send_message("You do not have the required permissions to execute this command.")
  27. return
  28. if alert_type not in ["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]:
  29. await interaction.response.send_message("Invalid alert type. Please try again.")
  30. return
  31. c.execute("UPDATE Channels SET alert_types = ? WHERE guild_id = ?", (alert_type, interaction.guild.id))
  32. conn.commit()
  33. await interaction.response.send_message(f"Alert type set to {alert_type}.")
英文:

For this question you can use a literal to show a list of options:

  1. bot.tree.command(name=&quot;alerttype&quot;, description=&quot;Options: The Gathering Legions, World Boss, The Helltide Rises, All Alerts&quot;)
  2. async def alerttype(interaction: discord.Interaction, alert_type: Literal[&quot;The Gathering Legions&quot;, &quot;World Boss&quot;, &quot;The Helltide Rises&quot;, &quot;All Alerts&quot;]):
  3. if interaction.guild is None:
  4. await interaction.response.send_message(&quot;This command cannot be used in a DM.&quot;)
  5. return
  6. if not interaction.user.guild_permissions.manage_channels:
  7. await interaction.response.send_message(&quot;You do not have the required permissions to execute this command.&quot;)
  8. return
  9. if alert_type not in [&quot;The Gathering Legions&quot;, &quot;World Boss&quot;, &quot;The Helltide Rises&quot;, &quot;All Alerts&quot;]:
  10. await interaction.response.send_message(&quot;Invalid alert type. Please try again.&quot;)
  11. return
  12. c.execute(&quot;UPDATE Channels SET alert_types = ? WHERE guild_id = ?&quot;, (alert_type, interaction.guild.id))
  13. conn.commit()
  14. await interaction.response.send_message(f&quot;Alert type set to {alert_type}.&quot;)

huangapple
  • 本文由 发表于 2023年6月26日 11:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553389.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定