Skip to content
Advertisement

on_guild_join data doesn’t save on my database

I test-ran my code using on_guild_join to fetch data from a server it joined and save it in a specific db file, but when I checked that, it was empty, no data was saved in it. I checked that on_guild_join part was working, and gave all intents. Here’s the code:

class Bot(Botbase):
    def __init__(self):
        self.ready = False
        # self.cogs_ready = Ready()
        self.guild = None
        self.scheduler = AsyncIOScheduler()
        db.autosave(self.scheduler)
        super().__init__(
            command_prefix=get_prefix,
            owner_ids=OWNER_IDS,
            intents=Intents.all()
        )

    async def on_guild_join(self, guild):
        print('detected new serverninitiating setup sequence...')
        db.execute('INSERT OR REPLACE INTO war_guild (GuildID) VALUES (?)', guild.id)
        db.multiexec('INSERT OR IGNORE INTO channels (ChannelName, ChannelID, ChannelType) VALUES (?, ?, ?)', 
                     ((channel.name, channel.id, 'text') for channel in guild.text_channels))
        db.multiexec('INSERT OR IGNORE INTO channels (ChannelName, ChannelID, ChannelType) VALUES (?, ?, ?)', 
                     ((channel.name, channel.id, 'voice') for channel in guild.voice_channels))
        print(' updating guild and channel info to a database...') 

class for command ‘execute’, ‘multiexec’:

from os.path import isfile
from sqlite3 import connect

DB_PATH = "./data/db/database.db"
BUILD_PATH = "./data/db/build.sql"

cxn = connect(DB_PATH, check_same_thread=False)
cur = cxn.cursor()

def execute(command, *values):
    cur.execute(command, tuple(values))

def multiexec(command, valueset):
    cur.executemany(command, valueset)

Advertisement

Answer

In Sqlite3 you need to call commit() to save your changes to the database or they will not be visible from other database connections, and hence, lost.

In your case you will need to add cxn.commit(). I would suggest to also close the database connection when you are done with it, for that use cxn.close().

References:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement