繁体   English   中英

Discord.py 预定命令

[英]Discord.py scheduled commands

所以,我做了一个静音命令。 它工作正常,但只有当机器人在线时,因为我在给定的时间内使用 asyncio.sleep。

我将如何做到这一点,而不是 asyncio,它会按计划工作? 喜欢:

命令被称为:

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

到目前为止,我的命令是这样的:

@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")

您应该以不同的方式指定公会,下面有一个示例。

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM