Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto db push for guilds #819

Open
Natemo2625 opened this issue Apr 11, 2024 · 8 comments
Open

Auto db push for guilds #819

Natemo2625 opened this issue Apr 11, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@Natemo2625
Copy link
Contributor

Natemo2625 commented Apr 11, 2024

Explain your suggestion
I wish that it would automatically check for guilds the bot is in when started and update the database accordingly so I don't have to kick and re-invite the bot to get slash commands to work for every server it's in

@Natemo2625 Natemo2625 added the enhancement New feature or request label Apr 11, 2024
@Natemo2625
Copy link
Contributor Author

Hello, has anyone seen this? Would really help with slash commands not working after system changed and/or DB loss

@Natemo2625
Copy link
Contributor Author

Will someone please have a look at this? Would solve all of the problems that I'm facing if it were added.

@Natemo2625 Natemo2625 changed the title Auto db push for guild IDs Auto db push for guilds Aug 28, 2024
@Natemo2625
Copy link
Contributor Author

Could someone at least give me a starting point to fix it myself? It's really annoying

@Darkwater409
Copy link

Darkwater409 commented Dec 24, 2024

That would be done via the bot start event emitter.

/*
* client is your discord.js Client
* commands obj is your command data you want to add to the guild
*/
client.on('ready', async (guild) => {
guild.commands.set(commands).then(() => 
console.log(`Commands deployed in guild ${guild.name}!`));
})

Naturally, it would need to be adjusted for this bot's code. But, that's the general idea of how it would be done the easiest.

Might also be a good idea to have some sort of check in there to prevent from updating anything that hasn't changed.

@Natemo2625
Copy link
Contributor Author

Ah alright, thanks for the starting point, will comment here again if I've got it working

@Darkwater409
Copy link

Darkwater409 commented Dec 28, 2024

Ah alright, thanks for the starting point, will comment here again if I've got it working

It looks like for this bot you might just need to add this to the client.on("ready", async () => { ... }); event emitter:

ApplicationCommandRegistries.setDefaultBehaviorWhenNotIdentical(
        RegisterBehavior.BulkOverwrite
);

So, basically the ready event in the index.ts file would go from this:

client.on('ready', async () => {
        client.music.connect(client.user!.id);
        client.user?.setActivity('/', {
                type: ActivityType.Watching
        });

        client.user?.setStatus('online');
        const token = client.twitch.auth.access_token;
        if (!token) return;

        // happens to be the first DB call at start up
        try {
                const notifyDB = await trpcNode.twitch.getAll.query();

                const query: string[] = [];
                for (const user of notifyDB.notifications) {
                        query.push(user.twitchId);
                        client.twitch.notifyList[user.twitchId] = {
                                sendTo: user.channelIds,
                                logo: user.logo,
                                live: user.live,
                                messageSent: user.sent,
                                messageHandler: {}
                        };
                }
                await notify(query).then(() =>
                        setInterval(async () => {
                                const newQuery: string[] = [];
                                // pickup newly added entries
                                for (const key in client.twitch.notifyList) {
                                        newQuery.push(key);
                                }
                                await notify(newQuery);
                        }, 60 * 1000)
                );
        } catch (err) {
                Logger.error('Prisma ' + err);
        }
});

to this:

client.on('ready', async () => {
        ApplicationCommandRegistries.setDefaultBehaviorWhenNotIdentical(
                RegisterBehavior.Overwrite
        );

        client.music.connect(client.user!.id);
        client.user?.setActivity('/', {
                type: ActivityType.Watching
        });

        client.user?.setStatus('online');
        const token = client.twitch.auth.access_token;
        if (!token) return;

        // happens to be the first DB call at start up
        try {
                const notifyDB = await trpcNode.twitch.getAll.query();

                const query: string[] = [];
                for (const user of notifyDB.notifications) {
                        query.push(user.twitchId);
                        client.twitch.notifyList[user.twitchId] = {
                                sendTo: user.channelIds,
                                logo: user.logo,
                                live: user.live,
                                messageSent: user.sent,
                                messageHandler: {}
                        };
                }
                await notify(query).then(() =>
                        setInterval(async () => {
                                const newQuery: string[] = [];
                                // pickup newly added entries
                                for (const key in client.twitch.notifyList) {
                                        newQuery.push(key);
                                }
                                await notify(newQuery);
                        }, 60 * 1000)
                );
        } catch (err) {
                Logger.error('Prisma ' + err);
        }
});

That should fix it if I'm not mistaken. There might be some indentation errors, however, since I made that edit entirely from GitHub mobile.

@Darkwater409
Copy link

Thinking about this a bit more. It might be better to have a command that updates all commands and then restarts the bot to load the changes in every guild.

@Natemo2625
Copy link
Contributor Author

Hmm alright, I can try to do that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants