我有这个代码让我的不和谐机器人暂时静音:
@bot.command()
async def tempmute(ctx, member: discord.Member, time: int, d, *, reason=None):
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name="Muted")
if not mutedRole:
mutedRole = await guild.create_role(name="Muted")
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True)
for role in guild.roles:
if role.name == "Muted":
await member.add_roles(role)
embed = discord.Embed(title="TempMuted!", description=f"{member.mention} has been tempmuted.", colour=discord.Colour.light_gray())
embed.add_field(name="Reason:", value=reason, inline=False)
embed.add_field(name="Time for the mute:", value=f"{time}{d}", inline=False)
await ctx.send(embed=embed)
if d == "s":
await asyncio.sleep(time)
if d == "m":
await asyncio.sleep(time*60)
if d == "h":
await asyncio.sleep(time*60*60)
if d == "d":
await asyncio.sleep(time*60*60*24)
await member.remove_roles(role)
embed = discord.Embed(title="Unmute (temp mute expired) ", description=f"Unmuted -{member.mention} ", colour=discord.Colour.light_gray())
await ctx.send(embed=embed)
return
但是,我的机器人每 12-24 小时重新启动一次,如果有人在机器人重新启动时当前处于临时静音状态,他们永远不会被取消静音,因为机器人忘记了它曾经将它们静音,所以它们保持静音直到我手动取消静音他们。有没有办法让我可以让机器人仍然记得取消静音,即使机器人在静音期间重新启动?如果是这样,请你告诉我我应该在我的代码中添加什么来这样做。谢谢!
注意:如果有帮助,我会通过 GitHub 和 Heroku 托管我的机器人,以防万一。
答案 0 :(得分:0)
你可以用文本文件来做,但它不会很准确:
@bot.command()
async def tempmute(ctx, member: discord.Member, time: int, d, *, reason=None):
guild = ctx.guild
mutedRole = discord.utils.get(guild.roles, name="Muted")
if not mutedRole:
mutedRole = await guild.create_role(name="Muted")
for channel in guild.channels:
await channel.set_permissions(mutedRole, speak=False, send_messages=False, read_message_history=True)
await member.add_roles(mutedRole)
embed = discord.Embed(title="TempMuted!", description=f"{member.mention} has been tempmuted.", colour=discord.Colour.light_gray())
embed.add_field(name="Reason:", value=reason, inline=False)
embed.add_field(name="Time for the mute:", value=f"{time}{d}", inline=False)
await ctx.send(embed=embed)
if d == "s":
mutetime = time
if d == "m":
mutetime = time*60
if d == "h":
mutetime = time*60*60
if d == "d":
mutetime = time*60*60*24
with open("TXT_FILE_PATH", "w+") as mutetimef:
mutetime.write(mutetime)
while True:
with open("TXT_FILE_PATH", "w+") as mutetimef:
if int(mutetimef.read()) == 0:
await member.remove_roles(mutedRole)
embed = discord.Embed(title="Unmute (temp mute expired) ", description=f"Unmuted -{member.mention} ", colour=discord.Colour.light_gray())
await ctx.send(embed=embed)
return
else:
mutetime -= 1
mutetimef.seek(0)
mutetimef.truncate()
mutetimef.write(mutetime)
await asyncio.sleep(1)
您还需要创建一个 .txt 文件来保存它。正如我所说,这不会太准确,但它可以工作。此外,您还必须定义变量或其他东西,如果它正在运行,则它会在重新启动后运行,否则它会运行它