user13415893
user13415893

Reputation:

I am trying to make a database with discord.py but does not work

I have been watching a video teaching me how to do a database for a simple welcome channel! This basically sets a channel for every member that joins will send a welcome channel to there own channel! Unfortunately I got an error (Look Below) Can someone help?

Here is the database:

@client.command()
async def channel(self, ctx, channel:discord.TextChannel):
    if ctx.message.author.guild_permissions.manage_messages:
        db = sqlite3.connect("welcome.sqlite")
        cursor = db.cursor()
        cursor.execute("SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}")
        result = cursor.fetchone()
        if result is None:
            sql = ("INSERT INTO main(guild_id, channel_id) VALUES(?,?)")
            val = (ctx.guild.id, channel.id)
            await ctx.send(f"Channel has been set to {channel.mention}")
        elif result is not None:
            sql = ("UPDATE main SET channel_id = ? WHERE guild_id = ?")
            val = (ctx.guild.id, channel.id)
            await ctx.send(f"Channel has been updated to {channel.mention}")
        cursor.execute(sql, val)
        db.commit()
        cursor.close()
        db.close()

Here is the error:

Ignoring exception in command channel:
Traceback (most recent call last):
  File "C:\Users\cedri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\bot.py", line 892, in invoke
    await ctx.command.invoke(ctx)
  File "C:\Users\cedri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 790, in invoke
    await self.prepare(ctx)
  File "C:\Users\cedri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 751, in prepare
    await self._parse_arguments(ctx)
  File "C:\Users\cedri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 670, in _parse_arguments
    transformed = await self.transform(ctx, param)
  File "C:\Users\cedri\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\ext\commands\core.py", line 516, in transform
    raise MissingRequiredArgument(param)
discord.ext.commands.errors.MissingRequiredArgument: channel is a required argument that is missing.

Upvotes: 1

Views: 512

Answers (1)

Diggy.
Diggy.

Reputation: 6944

The error is saying that when you're executing the command, you're not putting the channel argument, for example !channel #general.

To get past this, you can set a default argument, and then check if the value of said argument is the default value. If it is, then set the argument to the channel that the command was executed in.

Like so:

# Renamed the coroutine to avoid namespace pollution
@client.command(name="channel")
async def _channel(ctx, channel: discord.TextChannel=None):
    if not channel:
        channel = ctx.channel
    # rest of your code

Reference:

Upvotes: 1

Related Questions