【问题标题】:Discord.py TypeError: 'NoneType' object is not subscriptableDiscord.py TypeError:“NoneType”对象不可下标
【发布时间】:2020-11-06 17:16:55
【问题描述】:

伙计们,所以我正在开发一个过滤器来删除脏话。我的问题是我收到“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)

【问题讨论】:

    标签: python mysql discord.py


    【解决方案1】:

    如果没有要获取的行,fetchone() 方法可以返回 None。 (Documentation.) 程序接下来应该做什么?如果您要检查用户是否被静音,最明智的做法可能是假设他们没有被静音。

    例如:

    row = curs.fetchone()
    if row is not None:
        getMutedValue = row[0]
    else:
        getMutedValue = 0
    

    如果您返回 None,则您有一个备用值 0。如果您返回一个值,则使用该值。

    【讨论】:

      猜你喜欢
      • 2022-01-12
      • 1970-01-01
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-14
      • 2019-08-12
      • 2015-02-27
      相关资源
      最近更新 更多