首页 > 解决方案 > PyCharm 在“datetime”中找不到“utcnow”(Discord.py,Python 3.8)

问题描述

我找到了这个提醒命令(在 Stackoverflow 上),并根据我的 cog 更正了一些位。所以代码中有这一点utcnow,PyCharm 无法在其中找到它datetime

代码

@commands.command(case_insensitive=True, aliases=["remind", "remindme", "remind_me"])
    @commands.bot_has_permissions(attach_files=True, embed_links=True)
    async def reminder(self, ctx, time, *, reminder, counter):
        print(time)
        print(reminder)
        user = ctx.message.author
        embed = discord.Embed(color=0x55a7f7, timestamp=datetime.utcnow())
        embed.set_footer(
            text="If you have any questions, suggestions or bug reports, please join our support Discord Server: link hidden",
            icon_url=f"{self.client.user.avatar_url}")
        seconds = 0
        if reminder is None:
            embed.add_field(name='Warning',
                            value='Please specify what do you want me to remind you about.')  # Error message
        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='Warning',
                            value='Please specify a proper duration, send `reminder_help` for more information.')
        elif seconds < 300:
            embed.add_field(name='Warning',
                            value='You have specified a too short duration!\nMinimum duration is 5 minutes.')
        elif seconds > 7776000:
            embed.add_field(name='Warning',
                            value='You have specified a too long duration!\nMaximum duration is 90 days.')
        else:
            await ctx.send(f"Alright, I will remind you about {reminder} in {counter}.")
            await asyncio.sleep(seconds)
            await ctx.send(f"Hi, you asked me to remind you about {reminder} {counter} ago.")
            return
        await ctx.send(embed=embed)

标签: pythondatetimediscord.py

解决方案


首先,确保您正在导入日期时间库:

import datetime

根据您的导入,对 datetime 函数的正确调用将如下所示:

datetime.datetime.utcnow()

发生这种情况是因为您实际上正在导入一个类似的文件datetime.py,并且其中存在您要使用的类,称为datetime.

对于一些参考,整个电话解释:

  • datetime - 告诉 python 在 datetime 模块中进行选择
  • datetime - 告诉 python 在该模块中选择类数据时间
  • utcnow - 告诉 python 在该类中选择方法 utcnow
  • () - 执行该模块。

推荐阅读