【发布时间】:2022-06-16 00:29:19
【问题描述】:
我在 python 中为我的不和谐机器人制作了脚本。我试图为我的机器人发出命令,但在启动我的代码时出错。
我制作的完整代码:
@bot.command()
async def gen(ctx,name=None):
role = discord.utils.find(lambda r: r.name == 'VIP ACCESS', ctx.guild.roles)
if role in ctx.author.roles:
if name == None:
await ctx.send("Specify The Brand You Want! `{prefix}stock`")
else:
name = name.lower()+".txt"
if name not in os.listdir("Accounts"):
await ctx.send(f"Account Does Not Exist! `{prefix}stock`")
else:
with open("Accounts\\"+name) as file:
lines = file.read().splitlines()
if len(lines) == 0:
await ctx.send("These Accounts Are Out Of Stock! `{prefix}stock`")
else:
with open("Accounts\\"+name) as file:
account = random.choice(lines)
try:
await ctx.author.send(f"`{str(account)}`\n\nThis message will delete in {str(delete)} seconds!",delete_after=delete)
except:
await ctx.send("Failed to send! Turn On Your Direct Messages!(In The Server Privacy Settings)")
else:
await ctx.send("Sent The Account To Your DMS!")
with open("Accounts\\"+name,"w") as file:
file.write("")
with open("Accounts\\"+name,"a") as file:
for line in lines:
if line != account:
file.write(line+"\n")
else:
await ctx.send("You haven't got access to use that command.")
启动代码时我会得到什么:
if name not in os.listdir("Accounts"):
IndentationError: unexpected indent
【问题讨论】:
-
你有一堆短缩进。 Python 缩进是 4 个空格。您有几个看起来像 1-2 个空格。
-
缩进在Python中有意义,你的无处不在。
-
缩进必须是相同数字的倍数。
-
@RandomDavis 实际上他们没有必须成为,但他们肯定应该成为。
-
您需要取消缩进此行以与上一行相同。在再次发布此问题之前,请尝试自己检查缩进。
标签: python discord discord.py