#Sqlite3 Warning System

1 messages · Page 1 of 1 (latest)

young sandBOT
#
Too Many Tags

Please use either the disnake or python tag, but not both. If your question pertains to disnake, please use the disnake tag. If your question is a general python question that does not depend on disnake, please use the python tag.
I've taken a guess based on the contents of your message which your question is actually about.

If you believe this to be in error, please let us know.

ancient grotto
#

Send your file system tree

#

I'd prefer screenshot, but I already know the problem

#

As I can say, code you send is inside of warn.py file, which is inside of commands folder

#

To access warning.sqlite you need to write relative path

#

"../warning.sqlite"

#

No

#

Just write relative path

#

Rlly?

#

I.g you're new in files and paths too

lofty walrus
#

Are you looking to make this a public bot or just a small private bot for a couple servers?

#

If you aren’t trying to scale it to be large, I highly recommend trying out the dataset library

#

it’s super easy and supports sqlite

#

2 sec lemme get on my pc

#

in your main file you wanna do

import dataset
database = dataset.connect('sqlite:///database.db')
#

and then after your bot = Bot() do bot.database = database

#

so that in your cogs you can do

#
self.bot.database

to access your database

#

yeah 2 sec i have more

#
class WarnCommand(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.listener()
    async def on_ready(self):
        print('>warn command active!')

    @commands.command()
    async def warn(self, ctx, user: disnake.Member, reason):
        self.bot.database['warns'].insert(
            dict(
                guild_id=ctx.guild.id,
                user_id=user.id,
                reason=reason,
                timestamp=datetime.now().timestamp()
            )
        )
#

['warns'] will access the 'warns' table

#

and then you just .insert() a dict object containing the keys=values you want for the row

#

if the table doesnt exist it will create one for you

#

if the column doesnt exist it will create it for you

#

legit makes databases SO easy

#

only downside is that it isnt async so it's cant really be used for larger scale bots

#

i highly recommend checking out the documentation because it talks about the .update, .upsert, and the .insert_ingore methods which are really helpful in a lot of cases

#

say you wanna check for a specific user

#
async def get_warning(user: disnake.User):
    return self.bot.database['warns'].find_one(user_id=user.id)
#

and then you can access the values from it's columns by doing more []

#

like

#
async def get_warning_reason(user: disnake.User):
    return self.bot.database['warns'].find_one(user_id=user.id)['reason']

async def get_warning_time(user: disnake.User):
    return self.bot.database['warns'].find_one(user_id=user.id)['timestamp']
#

but you need to be careful because if .find_one returns None (like if there isn't an entry matching your criteria)

#

the bot will raise an error

#

yeah it's super easy to use, i def wouldnt have gotten this far into bot programming if i had to write actual SQL statements lmfao

#

yup

#

and it will return a list

#

so you gotta iterate through it with a for loop

#

like remove them from the database after a certain amount of time?

#

an easier way would be not to remove them but to just not display them by doing something like

all_warnings = self.bot.database['warns'].find(user_id=user.id)

warnings_list = []
for warning in all_warnings:
    if (datetime.now() - datetime.fromtimestamp(warning['timestamp'])) < datetime.timedelta(days=90):
        warnings_list.append(warning)
#

like you basically make a datetime object from the timestamp and check if the difference between now and then was less than 90 days and if so include it in the list otherwise continue

#

yup 👍

#

lemme see your code

lofty walrus
#

you need to do

#
for warning in allwarnings:
   print(warning['reason'])
#

yeah that's basically a list

#

no problem

#

yeah if you have any questions feel free to dm me