【问题标题】:How do I fix "Unresolved reference 'time'" in PyCharm? [closed]如何在 PyCharm 中修复“未解决的参考‘时间’”? [关闭]
【发布时间】:2021-06-13 18:45:36
【问题描述】:

我正在 PyCharm 中编写一个 Discord 机器人,并且正在处理静音命令。我听说很多时间模块已经默认安装了,你不需要导入它。我开始有相反的想法了。

import discord 
from discord.ext import commands 
from discord.ext.commands import MissingPermissions

client = commands.Bot(command_prefix = "$")

@client.event
async def on_ready():
    print('Bot is ready')
    await client.change_presence(status=discord.Status.online, activity=discord.Game("$help"), afk = False)

@client.command()
@commands.has_permissions(administrator = True)
async def mute(ctx, member : discord.Member, *, amount = 60, reason = "No reason provided."):

    guild = client.guilds[0]

    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, read_messages = False)

    await member.add_roles(mutedRole, reason=reason)
    await member.send(f"You have been muted for {amount} seconds from {ctx.guild.name}. Reason = "+reason)
    await ctx.send(f"Muted {discord.Member} for {amount} seconds from {ctx.guild.name}. Reason = "+reason)
    time.sleep(amount)
    await member.remove_roles(mutedRole)
    await member.send(f"You have been unmuted from {ctx.guild.name}")
    await ctx.send(f"{discord.Member} has been unmuted from {ctx.guild.name}")

所以这个想法是机器人将静音该成员,然后 time.sleep(amount) 将在被静音的成员的给定时间内休眠,然后唤醒并取消静音该成员。嗯,这个错误来了。

我真的不想删除另一个我努力工作的命令。

【问题讨论】:

  • 为什么没有导入时间模块?
  • 你还没有导入时间
  • 并且不要将 time.sleep 用于 discord.py 机器人。 see thistime.sleep 将阻止任何其他命令运行。

标签: python pycharm discord discord.py bots


【解决方案1】:

discord.py 的重要一点是命令是异步的。因此,通过使用时间模块,您将在静音期间阻止所有机器人。

正如您在FAQ 中看到的,请改用asyncio.sleep()。您还必须为此导入 asyncio (import asyncio)。您将在 discord.py 的常见问题解答(之前链接)中直接看到更多信息

祝你有美好的一天

【讨论】:

  • 好的,我使用了它,但我收到一条错误消息“D:/Users/404la_g/PycharmProjects/Bot/main.py:287: RuntimeWarning: coroutine 'sleep' is never awaited asyncio.sleep(量)运行时警告:启用 tracemalloc 以获取对象分配回溯”对不起,如果我不能正确格式化它
  • nvm 我把它修好了
  • 只需将await 放在asyncio.sleep 前面,因为它是一个协程,它会很好。
【解决方案2】:

尝试在顶部添加import time。它是默认安装的,但这并不意味着它是导入的。你可以阅读 Python 标准库here

【讨论】:

  • 哦。现在我觉得很愚蠢哈哈。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-21
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 2014-02-09
  • 2015-11-12
相关资源
最近更新 更多