如何修复 PyCharm 中的“未解析的引用‘时间’”? [已关闭]

问题描述 投票:0回答:2

我正在 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) 将休眠该成员静音的给定时间,然后唤醒并取消该成员的静音。嗯,这个错误来了。

Unresolved reference 'time'

我真的不想删除另一个我费尽心血的命令。

python discord discord.py bots
2个回答
3
投票

尝试将

import time
添加到顶部。它是默认安装的,但这并不意味着它是导入的。您可以在此处阅读有关 Python 标准库的信息。


1
投票

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

正如您在常见问题解答中所看到的,请使用

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

祝你有美好的一天

© www.soinside.com 2019 - 2024. All rights reserved.