Python ‘guild’ cannot be converted to a MySQL type

  discord.py, mysql, mysql-python, python

I’m trying to make a welcome system for my discord.py bot, which has to be server specific. But when I try to input the guild id it throws this error. I’m quite new to MySQL so any help would be appreciated.

Error:

Traceback (most recent call last):
  File "C:Usersuser-pcAppDataLocalProgramsPythonPython39libsite-packagesdiscordextcommandsbot.py", line 994, in invoke
    await ctx.command.invoke(ctx)
  File "C:Usersuser-pcAppDataLocalProgramsPythonPython39libsite-packagesdiscordextcommandscore.py", line 1448, in invoke
    await ctx.invoked_subcommand.invoke(ctx)
  File "C:Usersuser-pcAppDataLocalProgramsPythonPython39libsite-packagesdiscordextcommandscore.py", line 894, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "C:Usersuser-pcAppDataLocalProgramsPythonPython39libsite-packagesdiscordextcommandscore.py", line 176, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: ProgrammingError: Failed processing format-parameters; Python 'guild' cannot be converted to a MySQL type

Here’s my code:

    @welcome.command()
    @has_permissions(administrator=True)
      async def channel(self, ctx, channel:discord.TextChannel):
      cursor.execute(f'SELECT channel_id FROM welcome WHERE guild_id = {ctx.guild.id}')
      result = cursor.fetchone()
      if result is None:
        cursor.execute('INSERT INTO welcome (guild_id, channel_i) VALUES (%s,%s)', (ctx.guild, channel.id))
        #values = (ctx.guild, channel.id)
        db.commit()
        #values = (ctx.guild.id, channel.id)
        await ctx.send(f'Welcome channel has been set to {channel.mention}')
      elif result is not None:
        cursor.execute('UPDATE welcome SET channel_id = %s WHERE guild_id = %s', (channel.id, ctx.guild))
        db.commit()
        #values = (channel.id, ctx.guild.id)
        await ctx.send(f'Welcome channel has been updated to {channel.mention}')
      cursor.close()
      db.close()

Source: Python Questions

LEAVE A COMMENT