【发布时间】: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 this。time.sleep将阻止任何其他命令运行。
标签: python pycharm discord discord.py bots