Discord.py计划命令

2024-10-02 18:22:22 发布

您现在位置:Python中文网/ 问答频道 /正文

于是,我发出了一个无声的命令。它工作得很好,但只有当bot联机时,因为我在给定的时间内使用asyncio.sleep

我该怎么做,这样它就可以按计划工作,而不是异步IO?比如:

命令名为:

添加一个在给定时间取消用户静音的新计划。 如果机器人已经离线一段时间,那么用户应该被解除静音,解除他的静音

我的命令是:

@commands.command()
@commands.guild_only()
@commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member : discord.Member, arg: int, time, *, reason=None):

    role = discord.utils.get(member.guild.roles, name="MUTED")

    await member.add_roles(role) # ? This for some reason doesnt work


    timeSet = arg

    if time == "m":
        time = "minutes"
        arg = arg * 60
    elif time == "h":
        arg = (arg * 60) * 60
        time = "hours"
    elif time == "d":
        arg = ((arg * 60) * 60) * 24
        time = "days"
    elif time == "y":
        arg = ((((arg * 60) * 60) * 24) * 31) * 365
        time = "years"
    else:
        arg = arg

    await ctx.message.delete()

    #Setting the embed for mutes

    embedMuteServer=discord.Embed(title="MUTED", description="--------------------------------------------", color=0xff0000)
    embedMuteServer.add_field(name="Muted user", value=f"{member}", inline=True)
    embedMuteServer.add_field(name="For: ", value=f'{reason}', inline=True)
    embedMuteServer.add_field(name="For: ", value=f'{timeSet} {time}', inline=True)
    embedMuteServer.set_footer(text="-----------------------------------------------------")

    #Setting up the sending embed

    embedMute=discord.Embed(title="You were muted in", description=f"{member.guild.name}", color=0xff7800)
    embedMute.add_field(name="Reason of the mute: ", value=f"{reason}", inline=False)
    embedMute.add_field(name="The one who muted you was:", value=f"{ctx.message.author.mention}", inline=False)
    embedMute.add_field(name="You were muted for:",value=f"{timeSet} {time}")

    #Sending the embed

    try:
        await member.send(embed=embedMute)
        await ctx.send(embed=embedMuteServer)
    except: 
        pass
    print("Server and DM embeds sent")

    #Doing the command

    
    await asyncio.sleep(arg)
    await member.remove_roles(role)

    #Sending unmuted DM

    try:

        embed=discord.Embed(title="You were unmuted!", description="---", color=0x33e639)
        
        embed.add_field(name="Your muted timer has expired!", value="Try not to get muted again ", inline=False)
        await member.send(embed=embed)
        print("Unmute DM sent")

    except: 
        pass
        print("Cannot send messages to this user")

Tags: thenameaddfieldtimevaluearginline
1条回答
网友
1楼 · 发布于 2024-10-02 18:22:22

你应该以不同的方式指定公会,下面是一个例子

guild = discord.utils.get(self.bot.guilds, id=ctx.guild.id)
role = discord.utils.get(guild.roles, name="MUTED")

你的第二个问题是,你不应该把“arg”变成int,如果你这样做,你就不会得到小时、天、周或月的参数

相反,您应该使用类似arg.endswith("h")

您不需要arg,因为您已经过了持续时间

这是您的固定和稍微改进的代码

@commands.command()
@commands.guild_only()
@commands.has_permissions(manage_roles=True)
async def mute(self, ctx, member : discord.Member, time, *, reason=None):

    role = discord.utils.get(member.guild.roles, name="MUTED")

    await member.add_roles(role) # ? This for some reason doesnt work

    timeSet = f"{time}"

    if time.endswith("m"):
        duration = int(time)
        arg = duration * 60
        time = "Months"
    elif time.endswith("h"):
        duration = int(time)
        arg = int(duration * 60 * 60)
        time = "Hours"
    elif time.endswith("d"):
        duration = int(time)
        arg = int(duration * 60 * 60 * 24)
        time = "Days"
    elif time.endswith("y"):
        duration = int(time)
        arg = int(duration * 60 * 60 * 24 * 31 * 365)
        time = "Years"
    else:
        arg = arg

    await ctx.message.delete()

    #Setting the embed for mutes

    embedMuteServer=discord.Embed(title="MUTED", description="                      ", color=0xff0000)
    embedMuteServer.add_field(name="Muted user", value=f"{member}", inline=True)
    embedMuteServer.add_field(name="For: ", value=f'{reason}', inline=True)
    embedMuteServer.add_field(name="For: ", value=f'{timeSet} {time}', inline=True)
    embedMuteServer.set_footer(text="                          -")

    #Setting up the sending embed

    embedMute=discord.Embed(title="You were muted in", description=f"{member.guild.name}", color=0xff7800)
    embedMute.add_field(name="Reason of the mute: ", value=f"{reason}", inline=False)
    embedMute.add_field(name="The one who muted you was:", value=f"{ctx.message.author.mention}", inline=False)
    embedMute.add_field(name="You were muted for:",value=f"{timeSet} {time}")

    #Sending the embed

    try:
        await member.send(embed=embedMute)
        await ctx.send(embed=embedMuteServer)
    except: 
        pass
    print("Server and DM embeds sent")

    #Doing the command

    
    await asyncio.sleep(arg)
    await member.remove_roles(role)

    #Sending unmuted DM

    try:

        embed=discord.Embed(title="You were unmuted!", description=" -", color=0x33e639)
        
        embed.add_field(name="Your muted timer has expired!", value="Try not to get muted again ", inline=False)
        await member.send(embed=embed)
        print("Unmute DM sent")

    except discord.Forbidden: 
        pass
        print("Cannot send messages to this user")

相关问题 更多 >