1

I'm coding a Discord bot in PyCharm, and I'm working on a mute command. I've heard a lot that the time module is already installed by default and you don't need to import it. I'm starting to think the opposite.

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}")

So the idea was the bot would mute that member, and then the time.sleep(amount) would sleep for the given amount of time of the member being muted, then would wake up and unmute the member. Well, this error came.

Unresolved reference 'time'

I really don't want to delete another command that I worked so hard on.

bad_coder
  • 8,684
  • 19
  • 37
  • 59
404la_g
  • 25
  • 3

2 Answers2

3

Try adding import time to the top. It is installed by default, but that doesn't mean it's imported. You can read about the Python Standard Library here.

albert
  • 487
  • 3
  • 9
1

An important thing about discord.py is that the commands are asynchrone. So by using the time module you will just block all your bot for the duration of the mute.

As you can see in the FAQ, please use asyncio.sleep() instead. You also have to import asyncio for that (import asyncio). You will see more information directly in the discord.py's FAQ (linked before)

Have a nice day

Baptiste
  • 306
  • 1
  • 7
  • Ok, I used that but I got an error saying "D:/Users/404la_g/PycharmProjects/Bot/main.py:287: RuntimeWarning: coroutine 'sleep' was never awaited asyncio.sleep(amount) RuntimeWarning: Enable tracemalloc to get the object allocation traceback" sorry if I cant format it correctly – 404la_g Mar 16 '21 at 15:55
  • nvm i fixed it gawd – 404la_g Mar 16 '21 at 15:57
  • Just put `await` in front of the `asyncio.sleep` because it's a coroutine and it will be good. – Baptiste Mar 16 '21 at 15:59