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

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

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

问题

@bot.tree.command(name="alerttype", description="选项:The Gathering Legions、World Boss、The Helltide Rises、All Alerts")
async def alerttype(interaction: discord.Interaction, alert_type: str):

    if interaction.guild is None:
        await interaction.response.send_message("此命令不能在私信中使用。")
        return

    if not interaction.user.guild_permissions.manage_channels:
        await interaction.response.send_message("您没有执行此命令所需的权限。")
        return

    if alert_type not in ["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]:
        await interaction.response.send_message("无效的警报类型。请重试。")
        return

    c.execute("UPDATE Channels SET alert_types = ? WHERE guild_id = ?", (alert_type, interaction.guild.id))
    conn.commit()

    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?

@bot.tree.command(name="alerttype", description="Options: The Gathering Legions, World Boss, The Helltide Rises, All Alerts")
    async def alerttype(interaction: discord.Interaction, alert_type: str):
    
        if interaction.guild is None:
            await interaction.response.send_message("This command cannot be used in a DM.")
            return
    
        if not interaction.user.guild_permissions.manage_channels:
            await interaction.response.send_message("You do not have the required permissions to execute this command.")
            return
    
        if alert_type not in ["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]:
            await interaction.response.send_message("Invalid alert type. Please try again.")
            return
    
        c.execute("UPDATE Channels SET alert_types = ? WHERE guild_id = ?", (alert_type, interaction.guild.id))
        conn.commit()
    
        await interaction.response.send_message(f"Alert type set to {alert_type}.")
    ```

</details>


# 答案1
**得分**: 1

```python
bot.tree.command(name="alerttype", description="Options: The Gathering Legions, World Boss, The Helltide Rises, All Alerts")
async def alerttype(interaction: discord.Interaction, alert_type: Literal["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]):
    if interaction.guild is None:
        await interaction.response.send_message("This command cannot be used in a DM.")
        return

    if not interaction.user.guild_permissions.manage_channels:
        await interaction.response.send_message("You do not have the required permissions to execute this command.")
        return

    if alert_type not in ["The Gathering Legions", "World Boss", "The Helltide Rises", "All Alerts"]:
        await interaction.response.send_message("Invalid alert type. Please try again.")
        return

    c.execute("UPDATE Channels SET alert_types = ? WHERE guild_id = ?", (alert_type, interaction.guild.id))
    conn.commit()

    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:

bot.tree.command(name=&quot;alerttype&quot;, description=&quot;Options: The Gathering Legions, World Boss, The Helltide Rises, All Alerts&quot;)
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;]):
        if interaction.guild is None:
            await interaction.response.send_message(&quot;This command cannot be used in a DM.&quot;)
            return
    
        if not interaction.user.guild_permissions.manage_channels:
            await interaction.response.send_message(&quot;You do not have the required permissions to execute this command.&quot;)
            return
    
        if alert_type not in [&quot;The Gathering Legions&quot;, &quot;World Boss&quot;, &quot;The Helltide Rises&quot;, &quot;All Alerts&quot;]:
            await interaction.response.send_message(&quot;Invalid alert type. Please try again.&quot;)
            return
    
        c.execute(&quot;UPDATE Channels SET alert_types = ? WHERE guild_id = ?&quot;, (alert_type, interaction.guild.id))
        conn.commit()
    
        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:

确定