I'm getting a certain error on my TEMPMUTE command

末鹿安然 提交于 2021-01-27 13:02:52

问题


I have made a tempmute code or we can say I found one on stackoverflow. I copied the code but it doesn't seem to work. If anyone of you now and can help me thanks! The code is;

@commands.has_permissions(kick_members=True)
async def tempmute(ctx, member: discord.Member, time=0, reason=None):
    if not member or time == 0:
        return
    elif reason == None:
        reason = 'No reason'
    try:
        if time_list[2] == "s":
            time_in_s = int(time_list[1])
        if time_list[2] == "min":
            time_in_s = int(time_list[1]) * 60
        if time_list[2] == "h":
            time_in_s = int(time_list[1]) * 60 * 60
        if time_list[2] == "d":
            time_in_s = int(time_list[1]) * 60 * 60 * 60
    except:
        time_in_s = 0
 
    tempmuteembed = discord.Embed(colour=discord.Colour.from_rgb(0, 255, 0))
    tempmuteembed.set_author(icon_url=member.avatar_url, name=f'{member} has been tempmuted!')
    tempmuteembed.set_footer(text=f"{ctx.guild.name}  •  {datetime.strftime(datetime.now(), '%d.%m.%Y at %I:%M %p')}")
    tempmuteembed.add_field(name=f'ID:', value=f'{member.id}', inline=False)
    tempmuteembed.add_field(name='Reason:', value=f"{reason}")
    tempmuteembed.add_field(name='Duration:', value=f"{time}")
    tempmuteembed.add_field(name=f'By:', value=f'{ctx.author.name}#{ctx.author.discriminator}', inline=False)
    await ctx.send(embed=tempmuteembed)

 
    guild = ctx.guild
    for role in guild.roles:
        if role.name == 'Muted':
            await member.add_roles(role)
            await ctx.send(embed=tempmuteembed)
            await asyncio.sleep(time_in_s)
            await member.remove_roles(role)
            return

The error I'm getting is the following;

discord.ext.commands.errors.BadArgument: Converting to "int" failed for parameter "time".

回答1:


This is because Commands have Converters that are run on their arguments. Since time defaults to 0, which is of type int, the library tries to convert time to an int. However, this conversion will fail if you give a unit suffix like 10m, since int('10m') fails with a ValueError, which in turn raises BadArgument.

To fix this, simply add a proper type annotation to your time parameter:

from typing import Union
async def tempmute(ctx, member: discord.Member, time: Union[int, str] = 0, reason=None):


来源:https://stackoverflow.com/questions/65703504/im-getting-a-certain-error-on-my-tempmute-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!