【发布时间】:2021-04-28 20:31:42
【问题描述】:
我的 discord 机器人有这个代码,它可以禁止人们,并首先告诉他们禁止的原因。
cooldown = []
@bot.command(pass_context = True)
@commands.has_role('Senior Moderator')
async def ban(ctx, member: discord.Member = None, *, reason = None):
author = str(ctx.author)
if author in cooldown:
await ctx.send('Calm down! You\'ve already banned someone less than two hours ago.')
return
try:
if reason == None:
reason = 'breaking the rules.'
await member.send(f'You have been banned from **{ctx.guild.name}** for **{reason}**')
await member.ban(reason = f"Banned by {ctx.message.author} for "+reason)
await ctx.send(f'{member.mention} has been banned.')
cooldown.append(author)
await asyncio.sleep(2 * 60 * 60) #The argument is in seconds. 2hr = 7200s
cooldown.remove(author)
except:
await ctx.send('Error. Please check you are typing the command correctly. `!ban @username (reason)`.')
但是,如果我试图禁止的用户禁用了 DM,则机器人无法发送禁止原因消息,因此不会继续下一步,即禁止他们,并返回错误消息,这是错误。请检查您是否正确输入了命令。 !ban @username(原因)
请你重写代码,让它在禁止某人之前尝试向某人推理推理,但如果他们禁用了 DM,它仍然会禁止他们。谢谢!
【问题讨论】:
标签: discord discord.py