如何确保机器人重启时不重置计时器?

时间:2020-08-26 12:26:22

标签: python bots discord.py discord.py-rewrite

我有以下代码可在一段时间内发布Mut角色:

import discord
from discord.ext import commands
import psycopg2
import asyncio, asyncpg
import os
 
database = os.environ.get('DATABASE')
user = os.environ.get('USER')
password = os.environ.get('PASSWORD')
host = os.environ.get('HOST')
port = os.environ.get('PORT')
 
conn = psycopg2.connect(
    database = f"{database}", 
    user = f"{user}", 
    password = f"{password}", 
    host = f"{host}", 
    port = "5432"
)
 
cursor = conn.cursor()
 
class MuteCommand(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
 
    @commands.command()
    @commands.has_permissions(administrator = True)
    async def mute(self, ctx, who: discord.Member, time: int, what: str, reason = None):
        
        guild = ctx.message.guild
        cursor.execute(f'SELECT role_id FROM public.mute_role WHERE guild_id = \'{guild.id}\';')
        role_mute = cursor.fetchone()
        conn.commit()
        role = ctx.message.guild.get_role(role_mute[0])
        
        await ctx.channel.purge(limit = 1)
        
        if what == str('m'):
            if time >=1 and time <= 59:
                
                await ctx.send(f'--> {who} got a mut on {time}m. because of: {reason}')
 
                await who.add_roles(role)
                await who.move_to(None)
                await asyncio.sleep(time * 60)
                await who.remove_roles(role)
                await ctx.send('Mut is taken away')
        
        if what == str('h'):
            if time >= 1 and time <= 24:
                await ctx.send(f'--> {who} got a mut on  {time}h. because of: {reason}')
 
                await who.add_roles(role)
                await who.move_to(None)
                await asyncio.sleep(time * 3600)
                await who.remove_roles(role)
                await ctx.send('Mut is taken away')
        
        if what == str('d'):
            if time >= 1 and time <= 365:
                await ctx.send(f'--> {who} got a mut on  {time}d. because of: {reason}')
 
                await who.add_roles(role)
                await who.move_to(None)
                await asyncio.sleep(time * 86400)
                await who.remove_roles(role)
                await ctx.send('Mut is taken away')
        
        if what == str('y'):
            if time >= 1:
                await ctx.send(f'--> {who} got a mut on  {time}л. because of: {reason}')
 
                await who.add_roles(role)
                await who.move_to(None)
                await asyncio.sleep(time * 31557600)
                await who.remove_roles(role)
                await ctx.send('Mut is taken away') 
 
 
    @commands.command()
    @commands.has_permissions(administrator = True)
    async def muterole(self, ctx, role_id: discord.Role):
        guild = ctx.message.guild
        role = ctx.message.guild.get_role(role_id)
 
        cursor.execute(f'UPDATE public.mute_role SET role_id = \'{role_id}\' WHERE guild_id = \'{guild.id}\';')
        conn.commit()
 
        emb = discord.Embed(
            title= 'Successfully!',
            description = f'The role {role.mention} has been set for the `mute` command',
            timestamp = ctx.message.created_at
            )
        emb.set_footer(
            text = 'Requested: ' + f'{ctx.author}',
            icon_url = ctx.author.avatar_url
            )
        
        if ctx.guild.system_channel is not None:
            await ctx.guild.system_channel.send(embed = emb)
        elif ctx.guild.system_channel is None:
            await ctx.send(embed = emb)
 
def setup(bot):
    bot.add_cog(MuteCommand(bot))

当我将漫游器上载到主机时,计时器会重置,并且在此时间之后漫游器应该扮演这个角色,但它并不会消失(角色只是简单地丢失给用户)

如何解决?

也许您可以通过数据库执行某些操作,如果可以的话该如何做? 我不会放弃任何方法

0 个答案:

没有答案