Help with discord bot slash commands options

Question:
How i can add options to this slash command. I need one text option for placements. It was normal command before but i want to change it to slash
Repl link:
https://replit.com/@finnishdev/PeskySelfishTheory

@client.tree.command(name = "calculate", description = "calculate match points")
@commands.has_role(allowed_role_name)
async def calculate_scores(interaction):
    try:
        our_positions = get_positions(placements)
        opponent_positions = get_missing_numbers(our_positions)
        our_score = calculate_team_score(our_positions)
        opponent_score = calculate_team_score(opponent_positions)
        total_score = our_score + opponent_score

        embed = discord.Embed(title='Results', color=discord.Color.blue())
        embed.add_field(name='Your Team Score', value=f'{our_score} (players: {len(our_positions)})', inline=False)
        embed.add_field(name='Opponent Team Score', value=f'{opponent_score} (players: {len(opponent_positions)})', inline=False)

        if total_score == 4364:
            embed.add_field(name='Total Score', value=f'{total_score}', inline=False)

        await interaction.response.send_message(embed=embed)

    except ValueError:
        await interaction.response.send_message('Invalid placements. Please use a valid format, e.g., 3,5-7,10,12-14,20,26.')

def get_positions(input_str):
    parts = input_str.split(',')
    positions = []
    for part in parts:
        if '-' in part:
            start, end = map(int, part.split('-'))
            positions.extend(range(start, end + 1))
        else:
            positions.append(int(part))
    return positions

def calculate_team_score(positions):
    scores_list = [
        300, 280, 262, 244, 228, 213, 198, 185, 173, 161,
        150, 140, 131, 122, 114, 107, 99, 93, 87, 81,
        75, 70, 66, 61, 57, 54, 50, 47, 44, 41,
        38, 35, 33, 31, 29, 27, 25, 24, 22, 21,
        19, 18, 17, 16, 15, 14, 13, 12, 11, 11
    ]

    total_score = sum(scores_list[position - 1] for position in positions)
    return total_score

def get_missing_numbers(positions):
    all_positions = set(range(1, 51))
    missing_numbers = list(all_positions - set(positions))
    return missing_numbers

Can anyone help me with this? I really need help

Please. I need help. Even someone

I still need heellpp!! Anyone who can help??

Use the app_commands module for slash and the Option parameter for text option for placements.

@client.tree.command(name="calculate", description="calculate match points")
@app_commands.describe(placements="Enter the placements as a comma-separated list")
@commands.has_role(allowed_role_name)
async def calculate_scores(interaction: discord.Interaction, placements: str):
    try:
2 Likes

Thank you, it worked! How i can make option not required? Like that you dont need to put it. Also how can i change it that its channel only or role only?

Set the option’s default value to None

You can check the channel ID inside the command function and respond accordingly, and also use the commands.has_role() decorator

from discord import app_commands
from discord.ext import commands

@client.tree.command(name="calculate", description="calculate match points")
@app_commands.describe(placements="Enter the placements as a comma-separated list, or leave blank.")
@app_commands.guild_only() # This will make the command only available in guilds (servers)
@commands.has_role(allowed_role_name) # Role check using decorator
async def calculate_scores(interaction: discord.Interaction, placements: str = None):
    # Giving you a brief example of channel restriction
    allowed_channel_id = 123456789 # Replace with your channel ID
    if interaction.channel_id != allowed_channel_id:
        await interaction.response.send_message("This command can only be used in a specific channel.")
        return

    # If placements are not provided
    if not placements:
        await interaction.response.send_message("No placements provided. Please enter placements.")
        return

    try:
        # Rest of your code...
        # ...

    except ValueError:
        await interaction.response.send_message('Invalid placements. Please use a valid format, e.g., 3,5-7,10,12-14,20,26.')
1 Like

Thank you:smiley:!! Idk what to do without you :laughing:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.