Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
544 views
in Technique[技术] by (71.8m points)

python - TypeError: 'NoneType' object is not iterable - SQLite and discord.py

The function will check if the channel with the reaction is a private channel between bot and user and then do other things.

The code:

@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 = ...

The output:

 (782664385889959976,)
 Ignoring exception in on_raw_reaction_add
 Traceback (most recent call last):
 File "C:UsersplaysAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordclient.py", 
 line 312, in _run_event
 await coro(*args, **kwargs)
 File "C:UsersplaysOneDriveРабочий столPythonot2.py", line 130, in on_raw_reaction_add
 channel_dm_id_list = list(cur.fetchone())
 TypeError: 'NoneType' object is not iterable

The table columns:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

fetchone() is returning the next row. In the line print(cur.fetchone()) you are already getting the first row. In the next line channel_dm_id_list = list(cur.fetchone()) you are trying to get the second value. But since there is no second value, the method returns None which is causing your error. So either remove the print statement or store the first result like that:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...