需要帮助修改我的 discord.py 静音命令才能读/写数据库

时间:2021-02-08 21:52:25

标签: python discord.py

现在我的静音命令运行良好。但是我想增加时间,如果我不继续重新启动我的机器人(更新 main.py 文件中的代码),这本身就没什么大不了的

目前我的代码是:

# Mute Command

    @commands.command(
        name="Mute",
        description='Mutes a user (Must have Manage Messages perms to use!)'
        )
    @commands.has_permissions(manage_messages=True)
    async def mute(self, ctx, member: discord.Member, *, reason=None):
        guild = ctx.guild
        mutedRole = discord.utils.get(guild.roles, name='Muted')
        author = ctx.message.author

        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, read_messages=False)
        
        await member.add_roles(mutedRole, reason=reason)
        embed = discord.Embed(colour=discord.Colour.red())
        embed.set_author(name="MEMBER MUTED")
        embed.add_field(name="Muted by: ", value=f"{author.mention}", inline=False)
        embed.add_field(name="User: ", value=f"{member.mention}", inline=False)
        embed.add_field(name="Reason: ", value=f"***{reason}***", inline=False)
        embed.set_thumbnail(url="https://media.giphy.com/media/IgAfIrRH96pFMT3sOm/giphy.gif")
        await ctx.send(embed=embed)
        await member.send(f'You were muted in {guild.name} for {reason}')

# Unmute Command

    @commands.command(
        name="Unmute",
        description='Unmutes a user (Must have Manage Messages perms to use!)'
        )
    @commands.has_permissions(manage_messages=True)
    async def unmute(self, ctx, member: discord.Member):
        guild = ctx.guild
        mutedRole = discord.utils.get(guild.roles, name='Muted')
        author = ctx.message.author
        
        await member.remove_roles(mutedRole)
        embed = discord.Embed(colour=discord.Colour.green())
        embed.set_author(name="MEMBER UNMUTED")
        embed.add_field(name="Unmuted by: ", value=f"{author.mention}", inline=False)
        embed.add_field(name="User: ", value=f"{member.mention}", inline=False)
        embed.set_thumbnail(url="https://media.giphy.com/media/IgAfIrRH96pFMT3sOm/giphy.gif")
        await ctx.send(embed=embed)
        await member.send(f'You were unmuted in {guild.name}')

给它增加时间不会太难,但我如何让它处理 postgresql 或 sqlite3 数据库,以便它存储公会、成员、静音和静音直到时间,然后不断检查如果静音时间大于静音时间

1 个答案:

答案 0 :(得分:0)

存储时间就像将日期时间对象转换为字符串然后检索值并将字符串转换为日期时间对象一样简单。 dateutil 库可以做到这一点。

pip install python-dateutil

然后在您的代码上:

from dateutil.parser import parse

date_string = "2021-02-08 16:52:21.143652"
date_object = parse(date_string)

现在有很多不同的方法可以连接到你的数据库,所以我建议你看看你想要使用的库,无论是 GINO 还是 psycopg 或其他什么。看看他们可以为您提供有关日期时间等特定字段类型的信息。