我正在尝试使用discord.py创建数据库,但不起作用

时间:2020-06-15 08:09:39

标签: sqlite discord.py

我一直在观看一个视频,该视频教我如何为一个简单的欢迎频道创建数据库!这基本上为每个加入的成员设置了一个频道,将向该频道发送一个欢迎频道!不幸的是,我遇到一个错误(请看下面),有人可以帮忙吗?

这是数据库:

@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()

这是错误:

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.

1 个答案:

答案 0 :(得分:0)

错误是说,在执行命令时,您没有放置通道参数,例如!channel #general

要克服此问题,可以设置默认参数,然后检查该参数的值是否为默认值。如果是,则将参数设置为执行命令的通道。

像这样:

# 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

参考: