-1

I have a logging system in my discord bot, and I want to make a system which allows you to ignore channels, but I'm stuck with it. Currently I have this code:

cursor=db.cursor(dictionary=True)
cursor.execute(f"SELECT * FROM `logexclude` WHERE server={int(before.guild.id)}")
excluded = cursor.fetchall()
for i in excluded:
    if before.channel.id in i[1]: return

excluded is a list and it looks like this: [{'server': 890248334542008321, 'channel_ids': 890248758875533322, 'user_ids': 0, 'role_ids': 0}]

However, when I try to access it's first element (excluded[0]), it just gives me an error:

    if before.channel.id in i[1]: return
TypeError: 1
Pearoo
  • 45
  • 1
  • 7
  • If your elements in `excluded` are a `dictionary` each then how could you expect to parse them like a `list` (`i[1]`). You should parse them using there keys like `i[channel_ids]`. By default they should be `list` but `dictionary` flag is set `True` when `db.cursor()` function is called i.e., `cursor=db.cursor(dictionary=True)`. – kite Oct 17 '21 at 16:03
  • What exactly do you mean by "dictionary's element"? – user207421 Oct 18 '21 at 08:52

1 Answers1

0

Dictionaries store their data in key:value pairs, so you can only access them by key (or value). You can't access them by index, there is the problem.

cursor=db.cursor(dictionary=True)
cursor.execute(f"SELECT * FROM `logexclude` WHERE server=?", (before.guild.id))
excluded = cursor.fetchall()
for i in excluded:
    if i['channel_ids'] == before.channel.id: 
         return

This should work.

P. S. It's better to separate query and parameters using question mark (?), here's described why.

P. S. S. If you want to store multiple channels in channel_id, you should use one-to-many relationship for your database.

mrzen
  • 133
  • 9
  • Do you think this would work? the `if` block will fail because `i['channel_ids']` is `int`, not an `iterable object`. You need to check it with a `==`. – kite Oct 17 '21 at 16:28
  • Actually, we can ditch this `if` check if we adjust the query a little bit, like `cursor.execute(f"SELECT * FROM \`logexclude\` WHERE channel_ids=?", (before.channel.id))` i.e., by using `channel id` instead of fetching the data based on `guild id`. – kite Oct 17 '21 at 16:36
  • @kite Thank you for noticing my mistake in the code, I have edited my answer. – mrzen Oct 18 '21 at 07:41