繁体   English   中英

Discord.py 使用 SQLite3 嵌入循环

[英]Discord.py Embed Loop With SQLite3

所以我有一个机器人,它本质上是一个租赁数据库,用户使用命令!rental然后输入关于租赁的各种数据,例如姓名、租给的人、价格、持续时间(以天为单位),然后它计算开始和结束日期。 所以我的租赁参数是姓名、租客、价格、持续时间、开始日期和结束日期。 然后这些参数显示在 Discord 嵌入中,因此用户可以看到他们已经成功创建了一个租赁(见图): https://i.imgur.com/zMpIn1K.png

然后将嵌入中表示的数据存储在 SQLite3 数据库中,在这种情况下,它称为rentals.db 然后我有另一个命令!view ,这是问题发生的地方。 我目前只有!view命令来打印 SQLite3 表中的所有行,这对于测试很有用,因此其中一行如下所示: [('Test Name', 'Test Renter Name', 'Test Price', 'Test Duration', 'Start Date', 'End Date')]

我真正想要发生的是当用户输入!view命令时,我希望表中的所有行都显示在单独的 Discord 嵌入中,因此每个嵌入都是存储的租金之一,因此用户可以看到他们所有的租金有。 我有一个粗略的想法,我需要对 SQLite3 数据库中的所有行使用循环,但我不确定将数据库信息添加到嵌入中。 通常我会根据用户输入创建嵌入,例如我会有name = input('What is your name?')然后在嵌入中它看起来像这样embed.add_field(name='Name', value='{}'.format(name)) ,但我认为我不能在这种情况下使用{}.format(database info)

这是!view命令的粗略入门代码:

if message.content.startswith('!view'):
        rows = cursor.execute("SELECT name, renter, duration, price, start, end FROM rental").fetchall()
        print(rows)
        # By the way I know in this loop that the {} don't do anything, they're just filler spots because I don't know what to put there!
        for x in rows:
            embed = discord.Embed(title="Rental", color=0x0000FF)
            embed.set_thumbnail(url="https://icon-library.com/images/database-png-icon/database-png-icon-22.jpg")
            embed.add_field(name='Bot Name:', value='{}', inline=False)
            embed.add_field(name='Renter Name:', value='{}', inline=False)
            embed.add_field(name='Rental Duration:', value='{} day(s)', inline=False)
            embed.add_field(name='Start Date:', value='{}', inline=False)
            embed.add_field(name='End Date:', value='{}', inline=False)
            embed.set_footer(icon_url='https://pbs.twimg.com/profile_images/1325672283881484289/oaGtVIOD_400x400.png', text='Created by @Expected')
            await message.channel.send(embed=embed)

我不确定我是否正确地开始了循环,所以这就是我想要你指导的地方。 无论您是指向我的文档、另一个重复的帖子,还是在这里帮助我,我都会感谢您的帮助!

if message.content.startswith('!view'):
        rows = cursor.execute("SELECT name, renter, duration, price, start, end FROM rental").fetchall()
        for row in rows:
            embed = discord.Embed(title='Rental', color=0x0000FF)
            embed.set_thumbnail(url='https://www.pngkit.com/png/detail/231-2316751_database-database-icon-png.png')
            embed.add_field(name='Bot Name:', value=row[0], inline=False)
            embed.add_field(name='Renter Name:', value=row[1], inline=False)
            embed.add_field(name='Rental Duration:', value=row[2], inline=False)
            embed.add_field(name='Rental Price:', value=row[3], inline=False)
            embed.add_field(name='Start Date:', value=row[4], inline=False)
            embed.add_field(name='End Date:', value=row[5], inline=False)
            embed.set_footer(icon_url='https://pbs.twimg.com/profile_images/1325672283881484289/oaGtVIOD_400x400.png', text='Created by @Expected')
            msg = await message.channel.send(embed=embed)
            await msg.add_reaction('\U0001F5D1')

这按预期工作,对于数据库中的每一行,它都会单独嵌入每行的正确值。 它还为每个嵌入添加了一个垃圾桶表情符号,然后实现删除 function。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM