Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
197 views
in Technique[技术] by (71.8m points)

python - Discord.py scheduled commands

So, I made a mute command. It works fine, but only when the bot is online, since I use asyncio.sleep for the time that was given.

How would I do it, so instead of asyncio, it would work on a schedule? Like:

Command is called:

Add a new schedule unmuting the user at the given time. If the bot has been offline for the time, that the user was supposed to be unmuted, unmute him.

My command is this so far:

@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")
question from:https://stackoverflow.com/questions/65883222/discord-py-scheduled-commands

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should specify the guild in a different way, there is an example below.

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

and your second problem is you shouldn't make the "arg" into an int, if you do that you won't get the hour, day, week, or the month argument.

instead, you should use something like arg.endswith("h")

You don't need arg since you're already passing the duration in time.

Here is your fixed and slightly improved Code.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...