我正在尝试向设置了该频道的所有人发送消息,但是发送消息时遇到问题

时间:2020-09-16 10:49:40

标签: discord.py

从理论上讲,代码应该已经向所有设置了该通道的服务器发送了一条消息!虽然似乎不起作用。我认为这是因为即使在sqlite中也无法获取channel_id。这是代码:

@client.event
async def on_ready():
    print("The client is online!")
    db = sqlite3.connect('entry.sqlite')
    cursor = db.cursor()
    cursor.execute('''
    CREATE TABLE IF NOT EXISTS main(
    guild_id TEXT,
    channel_id,
    user_id
    )
    ''')

@client.command()
async def ping(ctx):
    embed = discord.Embed(
    title = "Pong!",
    description = f"Your ping is {client.latency}ms !", color=0xf9c900)
    await ctx.send(embed=embed)

@client.command()
async def public(ctx, channel:discord.TextChannel, winners: int, time: int, link:str, *, prize:str):
    db = sqlite3.connect('entry.sqlite')
    cursor = db.cursor()
    channel_id = cursor.execute(f"SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}")
    result = cursor.fetchone()
    if result is None:
        return
    else:
        for guild in client.guilds:
            for channel in channel_id:
                try:
                    embed = discord.Embed(
                    title = f"**Giving Away {prize}**",
                    description = f"React with ? to enter! \n **{winners}** winner \n \n ? Must be in **{link}** to enter!", color=0xf9c900)
                    msg = await ctx.send(embed=embed)
                    asyncio.sleep(1)
                    await msg.add_reaction('?')
                    asyncio.sleep(time)
                except Exception:
                    continue
                else:
                    break

@client.command()
async def setchannel(ctx, channel:discord.TextChannel):
    if ctx.message.author.guild_permissions.manage_messages:
        db = sqlite3.connect('entry.sqlite')
        cursor = db.cursor()
        cursor.execute(f"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)
            embed = discord.Embed(description=f":white_check_mark: succesfully added the giveaway channel {channel.mention}!",color=0x00ced1)
            await ctx.send(embed=embed)
        elif result is not None:
            sql = ("UPDATE main SET channel_id = ? WHERE guild_id = ?")
            val = (channel.id, ctx.guild.id)
            embed = discord.Embed(description=f":white_check_mark: succesfully updated the giveaway channel {channel.mention}!",color=0x00ced1)
            await ctx.send(embed=embed)
        cursor.execute(sql, val)
        db.commit()
        cursor.close()
        db.close()

我要弄清楚的部分是这个部分:

@client.command()
async def public(ctx, channel:discord.TextChannel, winners: int, time: int, link:str, *, prize:str):
    db = sqlite3.connect('entry.sqlite')
    cursor = db.cursor()
    channel_id = cursor.execute(f"SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}")
    result = cursor.fetchone()
    if result is None:
        return
    else:
        for guild in client.guilds:
            for channel in channel_id:
                try:
                    embed = discord.Embed(
                    title = f"**Giving Away {prize}**",
                    description = f"React with ? to enter! \n **{winners}** winner \n \n ? Must be in **{link}** to enter!", color=0xf9c900)
                    msg = await ctx.send(embed=embed)
                    asyncio.sleep(1)
                    await msg.add_reaction('?')
                    asyncio.sleep(time)
                except Exception:
                    continue
                else:
                    break

这部分假设是要向设置在那里的通道发送消息!但是它不起作用,并且没有向我发送任何错误消息!

1 个答案:

答案 0 :(得分:0)

首先,您的SQL "SELECT channel_id FROM main WHERE guild_id = {ctx.guild.id}"将为运行channel_id命令的行会检索public

然后,您使用cursor.fetchone()获得一行数据,但是在循环中,您尝试循环遍历channel_id,这不是您想要的数据,因此我怀疑您的for循环是否均匀运行中。