How can i make my discord.py bot kick command optional reason section to a required reason?

Question:

@bot.tree.command(name="kick", description="Kicks specified user from the server.")
async def kick(interaction: discord.Interaction, member: discord.Member, reason: Optional[str] = None):
    if interaction.user.guild_permissions.kick_members:
        await member.kick(reason=reason)
        embed = discord.Embed(title="Successfully Kicked.",description=f"{member.mention} has been successfully kicked off the server.nFor {reason} reason.", color = discord.Color.green(), timestamp = datetime.datetime.utcnow())
        embed.set_footer(text = interaction.user.name,icon_url = interaction.user.avatar) 
        message = await interaction.response.send_message(embed=embed)
        await asyncio.sleep(10)
        await interaction.delete_original_response()

Here is the code.

I want to change "reason: Optional[str] = None):" to a required string. I tried a lot, I made it "reason=None" but it gave me error. How can i fix that problem? I wanted to make when somebody wants kick someone from the bot they writes /kick, selects person and give it a reason to it.

Asked By: Hylex

||

Answers:

Just put reason: str and leave it at that.

async def kick(interaction: discord.Interaction, member: discord.Member, reason: str):
Answered By: ESloman

You can do something like this:

async def kick(interaction: discord.Interaction, member: discord.Member, reason: str):
    if interaction.user.guild_permissions.kick_members:
        await member.kick(reason=reason)
        embed = discord.Embed(title="Successfully Kicked.",description=f"{member.mention} has been successfully kicked off the server.nFor {reason} reason.", color = discord.Color.green(), timestamp = datetime.datetime.utcnow())
        embed.set_footer(text = interaction.user.name,icon_url = interaction.user.avatar) 
        message = await interaction.response.send_message(embed=embed)
        await asyncio.sleep(10)
        await interaction.delete_original_response()

This will give you error if you don’t specify any reason, you can even set if user didn’t gave any reason then the reason=None by default you can set some reason like "User broke the rules…" smth like this

Answered By: Myth
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.