【问题标题】:Databases for Prefix and Reminder Command discord.py前缀和提醒命令数据库 discord.py
【发布时间】:2021-03-06 00:11:03
【问题描述】:

我对数据库不太了解,我想为我的提醒命令和可变前缀做一个。对于前缀,我目前使用的是 json 文件,但有人说最好使用数据库。此外,我的提醒命令不存储提醒,因此当机器人关闭时,提醒就消失了。如果有人可以帮助我,我会很高兴。

这是我的提醒命令代码:

@commands.command(aliases=['remind','remindme'])
    async def reminder(self, ctx, time, *, reminder):
        user = ctx.message.author
        embed = discord.Embed(colour=discord.Colour.gold())
        seconds = 0
        if reminder is None:
            embed.add_field(name='❌ Wrong Usage', value='`reminder <duration> <reminder>` is the correct way to use this command.')
        if time.lower().endswith("d"):
            seconds += int(time[:-1]) * 60 * 60 * 24
            counter = f"{seconds // 60 // 60 // 24} days"
        if time.lower().endswith("h"):
            seconds += int(time[:-1]) * 60 * 60
            counter = f"{seconds // 60 // 60} hours"
        elif time.lower().endswith("m"):
            seconds += int(time[:-1]) * 60
            counter = f"{seconds // 60} minutes"
        elif time.lower().endswith("s"):
            seconds += int(time[:-1])
            counter = f"{seconds} seconds"
        if seconds == 0:
            embed.add_field(name='❌ Wrong Usage',
                            value='Please specify a proper duration')
        elif seconds > 31536000:
            embed.add_field(name='❌ Wrong Usage', value='You have specified a too long duration!\nMaximum duration is 365 days.')
        else:
            await ctx.send(f"Alright, I will remind you about `{reminder}` in `{counter}`.")
            await asyncio.sleep(seconds)
            await ctx.send(f"**Reminder** {user.mention}: {reminder}")
            return
        await ctx.send(embed=embed)```

【问题讨论】:

    标签: python database sqlite discord.py discord.py-rewrite


    【解决方案1】:

    对于数据库 SQL 或 NoSQL,您有 2 个选项,例如 MySQL/SQLite/Postgres 或 MongoDB。您可以从他们的网站获得一个免费的 500MB MongoDB 数据库,但如果您将其托管在 VPS/KVM/专用机器/计算机上,您可以在那里进行设置。 JSON 对于涉及频繁数据更改的任何事情都是不利的。

    每当您创建新提醒时,您必须将其插入到数据库中。然后你需要使用 SELECT/find 来获取它。我在下面附加了一个使用多个前缀的示例,默认前缀为?,并且在 DMS 中,如果它在服务器/公会中,它将检查数据库。

    import pymongo
    password = os.environ.get("password")
    client = pymongo.MongoClient(f"mongodb://127.0.0.1")
    
    def get_prefix(bot, message):
      global client
      if not message.guild:
        return commands.when_mentioned_or("?")(bot, message)
      db = client.bot
      posts = db.serversettings
      prefix = "?"
      for x in posts.find({"guildid":message.guild.id}):
        prefix=x["prefix"]
      return commands.when_mentioned_or(prefix)(bot, message)
    bot = commands.Bot(command_prefix=get_prefix,case_insensitive=True)
    

    【讨论】:

    • 哦,完美!我的机器人的默认前缀也是?。谢谢
    • 嘿 FluxedScript,我可以看看这个的 SQL 版本吗?我可能会选择 SQL。
    • @KeroQuero 抱歉,我花了很长时间才回复。如果你进入 SQL,你需要提前创建你的表,这对我来说意味着更多的工作,但你需要有一个数据库,然后一个表接受 guild_id 作为 varchar 255 或类似 prefix 作为 TEXT .然后只需在创建公会时插入,或者它不像 on_message 事件那样不存在。然后只需使用 SELECT 来获取前缀。要创建前缀命令,您需要使用 UPDATE
    猜你喜欢
    • 1970-01-01
    • 2021-02-07
    • 2020-12-18
    • 2021-07-11
    • 2021-09-06
    • 2019-11-09
    • 2021-04-04
    • 2021-10-02
    • 2022-01-05
    相关资源
    最近更新 更多