Python Discord.py计划命令
于是,我发出了一个无声的命令。它工作得很好,但只有当bot联机时,因为我在给定的时间内使用asyncio.sleep 我该怎么做,这样它就可以按计划工作,而不是异步IO?比如: 命令名为: 添加一个在给定时间取消用户静音的新计划。 如果机器人已经离线一段时间,那么用户应该被解除静音,解除他的静音 我的命令是:Python Discord.py计划命令,python,discord,command,discord.py,Python,Discord,Command,Discord.py,于是,我发出了一个无声的命令。它工作得很好,但只有当bot联机时,因为我在给定的时间内使用asyncio.sleep 我该怎么做,这样它就可以按计划工作,而不是异步IO?比如: 命令名为: 添加一个在给定时间取消用户静音的新计划。 如果机器人已经离线一段时间,那么用户应该被解除静音,解除他的静音 我的命令是: @commands.command() @commands.guild_only() @commands.has_permissions(manage_roles=True) async
@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_权限(manage_roles=True)
异步def静音(self,ctx,成员:discord.member,time,*,reason=None):
role=discord.utils.get(member.guild.roles,name=“MUTED”)
等待成员。添加_角色(角色)#?由于某种原因,这不起作用
timeSet=f“{time}”
如果时间结束(“m”):
持续时间=整数(时间)
arg=持续时间*60
time=“月”
elif time.endswith(“h”):
持续时间=整数(时间)
arg=int(持续时间*60*60)
time=“小时”
elif time.endswith(“d”):
持续时间=整数(时间)
arg=int(持续时间*60*60*24)
time=“天”
elif time.endswith(“y”):
持续时间=整数(时间)
arg=int(持续时间*60*60*24*31*365)
time=“年”
其他:
arg=arg
等待ctx.message.delete()
#为静音设置嵌入
embedMuteServer=discord.Embed(title=“MUTED”,description=“--------------------------------------------------”,color=0xff0000)
embeddemuteserver.add_字段(name=“Muted user”,value=f“{member}”,inline=True)
embedMuteServer.add_字段(name=“For:”,value=f'{reason}',inline=True)
embeddemuteserver.add_字段(name=“For:”,value=f'{timeSet}{time}',inline=True)
embedMuteServer.set_页脚(text=“-------------------------------------------------------------”)
#设置发送嵌入
embedMute=discord.Embed(title=“您在中被静音”,description=f“{member.guild.name}”,color=0xff7800)
add_字段(name=“静音原因:”,value=f“{Reason}”,inline=False)
embeddemute.add_字段(name=“使您静音的人是:”,value=f“{ctx.message.author.title}”,inline=False)
embeddemute.add_字段(name=“您被静音了:”,value=f“{timeSet}{time}”)
#发送嵌入
尝试:
等待成员发送(嵌入=嵌入静音)
等待ctx.send(嵌入=嵌入服务器)
除:
通过
打印(“已发送服务器和DM嵌入”)
#执行命令
等待异步IO.sleep(arg)
等待成员。删除_角色(角色)
#发送非静音DM
尝试:
embed=discord.embed(title=“您被取消静音!”,description=“--”,color=0x33e639)
embed.add_字段(name=“您的静音计时器已过期!”,value=“尝试不再静音”,inline=False)
等待成员发送(嵌入=嵌入)
打印(“取消静音DM发送”)
不和谐除外。禁止:
通过
打印(“无法向该用户发送消息”)
Heya,谢谢你对代码的改进,但是,你知道我怎样才能实现标题中所说的吗?非常感谢。当bot脱机时,您无法解除某人的静音,因为当bot脱机时,您无法解除用户的静音。如果希望bot能够取消用户静音,则必须使用APS(高级Python调度器),它可以帮助您执行计划,即使bot已重新启动,如果时间超过APS时间,则它将在联机后立即执行计划。不幸的是,我不知道APS,祝你好运。