伙计们,所以我正在研究一种过滤器,该过滤器可以删除脏话。我的问题是我收到“ NoneType”对象不是下标错误。谁能解释为什么以及如何解决?我知道错误是由getMutedValue = curs.fetchone()[0]
承担的,但是如果我删除[0],它将无法正常工作。
@client.event
async def on_message(message):
curs.execute('SELECT muted FROM user WHERE userID = (%s)', (message.author.id,))
getMutedValue = curs.fetchone()[0]
curs.execute('SELECT word FROM filter')
getWord = curs.fetchone()
curs.execute('SELECT immunity FROM user WHERE userID = (%s)', (message.author.id,))
getImmunityValue = curs.fetchone()[0]
if getMutedValue == 1:
muted = message.author.mention + ' You are currently muted!'
await message.channel.send(muted)
await message.delete()
if getImmunityValue == 0:
for word in getWord:
if getMutedValue == 0:
if message.content.count(word) > 0:
channel = client.get_channel(729267888539828255)
id = message.author.id
curs.execute('SELECT username FROM user WHERE userID = (%s)', (id,))
getUsername = curs.fetchone()[0]
curs.execute(f'UPDATE user SET warning = +1 WHERE userID = {id}')
db.commit()
curs.execute('SELECT warning FROM user WHERE userID = (%s)', (id,))
getWarning = curs.fetchone()[0]
await message.delete()
await message.channel.send(f'{message.author.mention} Please do not use an swear words. If you continue you will get punished!')
Embed = discord.Embed(
title=f'{getWarning}x Warning from {getUsername}',
color=discord.Colour.red(),
timestamp=datetime.utcnow()
)
Embed.add_field(name='Blacklist Wort:', value=word, inline=False)
Embed.add_field(name='Channel-ID:', value=message.channel.id, inline=False)
Embed.add_field(name='Channel:', value=message.channel, inline=False)
await channel.send(embed=Embed)
await client.process_commands(message)
答案 0 :(得分:2)
如果没有要提取的行,则fetchone()
方法可以返回None。 (Documentation。)程序接下来应该做什么?在检查用户是否被静音的情况下,最明智的做法可能是假设他们没有被静音。
例如:
row = curs.fetchone()
if row is not None:
getMutedValue = row[0]
else:
getMutedValue = 0
如果您退回None
,则后备值为0。如果您退回了值,则使用该值。