【问题标题】:TypeError: 'NoneType' object is not iterable - SQLite and discord.pyTypeError:“NoneType”对象不可迭代 - SQLite 和 discord.py
【发布时间】:2021-04-14 06:51:50
【问题描述】:

该函数会检查有反应的频道是否是机器人和用户之间的私人频道,然后做其他事情。

代码:

@bot.event
async def on_raw_reaction_add(payload):
    channel = bot.get_channel(payload.channel_id)
    msg = await channel.fetch_message(payload.message_id)
    emoji = payload.emoji
    author = payload.member
    if emoji.is_custom_emoji():
        emoji_count = discord.utils.get(msg.reactions, emoji=emoji).count
    else:
        emoji_count = discord.utils.get(msg.reactions, emoji = emoji.name).count
    cur.execute(f"SELECT discord_user_dmchannel_id FROM users WHERE discord_user_id = \
                   {int(payload.user_id)};")
    print(cur.fetchone())
    channel_dm_id_list = list(cur.fetchone())
    channel_dm_id = channel_dm_id_list[0]
    if payload.channel_id == channel_dm_id:
        if int(emoji_count) > 1:
            if emoji = ...

输出:

 (782664385889959976,)
 Ignoring exception in on_raw_reaction_add
 Traceback (most recent call last):
 File "C:\Users\plays\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", 
 line 312, in _run_event
 await coro(*args, **kwargs)
 File "C:\Users\plays\OneDrive\Рабочий стол\Python\bot2.py", line 130, in on_raw_reaction_add
 channel_dm_id_list = list(cur.fetchone())
 TypeError: 'NoneType' object is not iterable

表格列:

users(
discord_user_id INT PRIMARY KEY,
discord_user_dmchannel_id INT,
discord_user_name TEXT,
... 
...);

【问题讨论】:

  • 如果没有结果,fetchone 是否返回 None

标签: python sqlite discord


【解决方案1】:

fetchone() 正在返回下一行。 在print(cur.fetchone()) 行中,您已经获得了第一行。在下一行channel_dm_id_list = list(cur.fetchone()) 中,您正在尝试获取第二个值。但由于没有第二个值,该方法返回 None ,这会导致您的错误。所以要么删除打印语句,要么像这样存储第一个结果:

channel_dm_id_list = list(cur.fetchone())
print(channel_dm_id_list)

【讨论】:

    猜你喜欢
    • 2012-08-25
    • 1970-01-01
    • 2017-08-29
    • 2016-08-30
    • 2014-11-21
    • 1970-01-01
    相关资源
    最近更新 更多