英文:
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="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}.")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论