TypeError: 'NoneType' object is not subscriptable, Discord Python Bot Mysql

Question:

Raw code:

@ loop(hours=12)
async def ticket_clear():
    # try:
    cur.execute("SELECT auth_id, chann_id from tickets")
    for (auth_id, chann_id) in cur:
        channel = client.get_channel(chann_id)
        if (channel):
            return
        else:
            cur.execute(
                f"DELETE from tickets WHERE chann_id=%s", ((chann_id),))
            conn.commit()
    # except:
       # print("Reconnecting")
        # await reconnect()

Have some code futher up which works perfectly:

cur.execute("SELECT auth_id, chann_id from tickets")
    for (auth_id, chann_id) in cur:
        tickets[chann_id] = auth_id

I have double checked everything, looked online.
the database is populated with atleast 40 entrees, and it works previously. it has to be something with discord.

Here i am checking if the channel still exists (if it does not discord returns a NoneType), which it then conditions to remove the ticket from the database. although here i get an error in the line
for (auth_id, chann_id) in cur:

As requested, full error code

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/discord/client.py", line 409, in _run_event
    await coro(*args, **kwargs)
  File "/home/kobe/RyBet/rybet.py", line 80, in on_ready
    await load()
  File "/home/kobe/RyBet/rybet.py", line 92, in load
    for (auth_id, chann_id) in cur:
  File "/usr/local/lib/python3.9/dist-packages/mysql/connector/cursor_cext.py", line 787, in fetchone
    return self._fetch_row()
  File "/usr/local/lib/python3.9/dist-packages/mysql/connector/cursor_cext.py", line 739, in _fetch_row
    row = self._rows[self._next_row]
TypeError: 'NoneType' object is not subscriptable
Asked By: Zaiga

||

Answers:

@ loop(hours=12)
async def ticket_clear():
    try:
        cur.execute("SELECT auth_id, chann_id from tickets")
        for (auth_id, chann_id) in cur:
            channel = client.get_channel(chann_id)
            if (channel):
                cur.execute(
                    f"DELETE from tickets WHERE chann_id=%s", (chann_id,))
                conn.commit()
    except Exception as e:
        print(e)
        await reconnect()

fixed it by removing that int(chann_id), it was apparently not giving a correct result

Answered By: Zaiga
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.