0
votes

So I have a bot that is essentially a rental database, the user uses the command !rental and then inputs various data about the rental, such as name, person renting to, price, duration (in days), and then it calculates the start and end date. So the rental parameters I have are name, renter, price, duration, start date, and end date. These parameters are then displayed in a Discord Embed so the user can see that they've successfully created a rental (see picture): https://i.imgur.com/zMpIn1K.png

The data that is represented in the embed is then stored in an SQLite3 database, in this case it's called rentals.db. I then have another command !view which is where the problem occurs. I currently just have the !view command to print all the rows in the SQLite3 table, which is good for testing so one of the rows looks like this: [('Test Name', 'Test Renter Name', 'Test Price', 'Test Duration', 'Start Date', 'End Date')]

What I really want to happen is when the user inputs the !view command, I want all rows in the table to be displayed in separate Discord Embeds, so each embed is one of the stored rentals so the user can see all of their rentals they have. I have a rough idea that I need to use a loop for all of the rows in the SQLite3 database, but I'm not sure to add the database info into the embed. Usually I create embeds based on user input so for example I would have name = input('What is your name?') and then in the embed it would look something like this embed.add_field(name='Name', value='{}'.format(name)), but I don't think I can use {}.format(database info) for this scenario.

Here is my rough starter code for the !view command:

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)

I'm not sure if I'm even starting the loop right, so that's where I'd like your guidance. Whether you're pointing me to documentation, another duplicate post, or helping me here I'd be grateful for your help!

1
Solved by myself after a few hours of tinkering. Using for row in rows: and then each embed value was an increment of row. So since name is the first object in the row it is row[0], then renter is row[1], etc.Aqyl

1 Answers

0
votes
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')

This works as intended, for each row in the database it makes a separate embed with the proper values from each row. It also adds a trashcan emoji for each embed to then have a delete function implemented.