Python 实现在for循环中查找非类型的方法?
尝试将非类型从该for循环中拉出时遇到问题,这样我就不会因为此错误而使我的bot崩溃:Python 实现在for循环中查找非类型的方法?,python,sqlite,discord.py,Python,Sqlite,Discord.py,尝试将非类型从该for循环中拉出时遇到问题,这样我就不会因为此错误而使我的bot崩溃: Ignoring exception in command rank: Traceback (most recent call last): File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped ret = await coro(*a
Ignoring exception in command rank:
Traceback (most recent call last):
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "D:\Life\discordbot\main.py", line 232, in rank
await ranking_menu.start(ctx)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 967, in start
await super().start(ctx, channel=channel, wait=wait)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 707, in start
self.message = msg = await self.send_initial_message(ctx, channel)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 962, in send_initial_message
kwargs = await self._get_kwargs_from_page(page)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\menus\__init__.py", line 939, in _get_kwargs_from_page
value = await discord.utils.maybe_coroutine(self._source.format_page, self, page)
File "E:\Programing\discordbot\lib\site-packages\discord\utils.py", line 343, in maybe_coroutine
return await value
File "D:\Life\discordbot\main.py", line 207, in format_page
table = ("\n".join(f'{idx + 1}. {self.ctx.bot.get_user(entry[0]).name} (XP: {entry[1]} | Level: {entry[2]} \n'
File "D:\Life\discordbot\main.py", line 207, in <genexpr>
table = ("\n".join(f'{idx + 1}. {self.ctx.bot.get_user(entry[0]).name} (XP: {entry[1]} | Level: {entry[2]} \n'
AttributeError: 'NoneType' object has no attribute 'name'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "E:\Programing\discordbot\lib\site-packages\discord\ext\commands\core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'name'
再次感谢各位
如果需要,这里是它的一部分的类!条目来自数据库中返回的行数
class LocalXpMenu(ListPageSource):
def __init__(self, ctx, data):
self.ctx = ctx
super().__init__(data, per_page = 10)
async def write_page(self, menu, fields=[]):
offset = (menu.current_page * self.per_page) + 1
len_data = len(self.entries)
embed = Embed(title="Server XP Leaderboard",
colour=self.ctx.author.colour)
embed.set_thumbnail(url = self.ctx.guild.icon_url)
embed.set_footer(text = f"{offset:,} - {min(len_data, offset+self.per_page-1):,} of {len_data:,} members.")
for name, value in fields:
embed.add_field(name=name, value=value, inline=False)
return embed
async def format_page(self, menu, entries):
fields = []
table = ("\n".join(f'{idx + 1}. {self.ctx.bot.get_user(entry[0]).name} (XP: {entry[1]} | Level: {entry[2]} \n'
for idx, entry in enumerate(entries)))
fields.append(("Ranks", table))
return await self.write_page(menu, fields)
为类循环添加条件
-
table=(“\n”.join(f'{idx+1}.{self.ctx.bot.get_user(entry[0]).name}(XP:{entry[1]}Level:{entry[2]}\n'对于idx,如果(entry和(len(entry)>=3)),则枚举(entries)中的条目)
您可以先筛选以仅选择非无条目
mapped_entries=map(lambda条目:条目+self.ctx.bot.get_用户(条目[0]),条目)
过滤的\u条目=过滤器(lambda条目:条目[3]!=无,映射的\u条目)
table=(“\n”.join(f'{idx+1}.{entry[3].name}(XP:{entry[1]}| Level:{entry[2]}\n'对于idx,枚举中的条目(筛选的条目)))
请注意,本例中的idx
将排除None
get\u user
结果的条目。如果要排除这些条目,则必须在mapped\u条目中包含索引,并使用它们。如果get\u user(条目),程序应该做什么
是否没有?跳过?是的,很抱歉花了这么长时间才回复您,我忙了一天我尝试了这个,但似乎还是遇到了同样的错误,所以我不确定到底是怎么回事
class LocalXpMenu(ListPageSource):
def __init__(self, ctx, data):
self.ctx = ctx
super().__init__(data, per_page = 10)
async def write_page(self, menu, fields=[]):
offset = (menu.current_page * self.per_page) + 1
len_data = len(self.entries)
embed = Embed(title="Server XP Leaderboard",
colour=self.ctx.author.colour)
embed.set_thumbnail(url = self.ctx.guild.icon_url)
embed.set_footer(text = f"{offset:,} - {min(len_data, offset+self.per_page-1):,} of {len_data:,} members.")
for name, value in fields:
embed.add_field(name=name, value=value, inline=False)
return embed
async def format_page(self, menu, entries):
fields = []
table = ("\n".join(f'{idx + 1}. {self.ctx.bot.get_user(entry[0]).name} (XP: {entry[1]} | Level: {entry[2]} \n'
for idx, entry in enumerate(entries)))
fields.append(("Ranks", table))
return await self.write_page(menu, fields)