Discord.py-无法尝试/除非403:用户不接受DM



我一直在到处寻找这个,但try/except对我来说从来都不起作用,并给我带来了多个错误。代码是禁止服务器上的每个成员,并发送服务器已删除的DM。它是有效的,但机器人会停在一个人身上,并显示错误"Cannot send messages to this user",我想处理它,但总是会出现以下错误:indent expectedexpression expected。我没有try/catch的代码是:

@client.command(pass_context=True)
async def abandon(ctx):
for member in ctx.guild.members:
if len(member.roles) < 2:
await member.send(f"Hello {member.display_name},nn" + banreason + banreason2 + banreason3)
await member.ban(reason="Executed due to deletion of the server. Invite links were sent in DMs.")
await ctx.send(f"**{member.display_name}** was banned and invite links were sent. :white_check_mark:")
print(f"Banned {member.display_name} and invite links were sent.")
print("Banning complete!")

如何在await member.send周围导入正确的try/catch括号?

编辑:添加了异常处理代码:

@client.command(pass_context=True)
async def abandon(ctx):
for member in ctx.guild.members:
if len(member.roles) < 2:
try:
print(f"Debug: Targeted user is {member.display_name}")
except CommandInvokeError:
ctx.send(f"{member.display_name}" + resolvedmerror)
print(f"{member.display_name} was banned with 403 Forbidden exception (No DMs accepted by user)")
pass
await member.send(f"Hello {member.display_name},nn" + banreason + banreason2 + banreason3)
await member.ban(reason="Executed due to deletion of the server. Invite links were sent in DMs.")
await ctx.send(f"**{member.display_name}** was banned and invite links were sent. :white_check_mark:")
print(f"Banned {member.display_name} and invite links were sent.")
print("Banning complete!")

我刚刚遇到了使用空格而不是制表符的简单格式问题。哈哈,python太敏感了,我认为需要一些东西,但只按了一下。

最新更新