Sapphire Scheduled Tasks | When do they launch after bot restarts?

I have a task that disables all message components. But this fails to run after bot restarts and trying to run it Logs:
2024-05-15 02:54:54 - INFO - @yume/sapphire-utilities registered successfully! // preInit
2024-05-15 02:54:55 - ERROR - [ScheduledTaskPlugin] There was no task found for "DisableMessageComponents"
2024-05-15 02:54:56 - INFO - @yume/sapphire-utilities/tasks registered successfully! // postLogin
2024-05-15 02:54:54 - INFO - @yume/sapphire-utilities registered successfully! // preInit
2024-05-15 02:54:55 - ERROR - [ScheduledTaskPlugin] There was no task found for "DisableMessageComponents"
2024-05-15 02:54:56 - INFO - @yume/sapphire-utilities/tasks registered successfully! // postLogin
I setup it to load only on postLogin because in task I need to fetch channel & message, and obv I can't do that when bot is not ready. Seems like ScheduledTaskPlugin trying to run tasks that was on redis before postLogin step if you restart your bot Is there a way to change that? :Hmm: My custom plugin that loads this task to my bots:
export class CustomSapphireScheduledTasks extends Plugin {
public static [postLogin](this: SapphireClient) {
loadScheduledTasks()

container.logger.info(`@yume/sapphire-utilities/tasks registered successfully!`)
}
}

SapphireClient.plugins.registerPostLoginHook(
CustomSapphireScheduledTasks[postLogin],
"CustomSapphireScheduledTasks-postLogin"
)
export class CustomSapphireScheduledTasks extends Plugin {
public static [postLogin](this: SapphireClient) {
loadScheduledTasks()

container.logger.info(`@yume/sapphire-utilities/tasks registered successfully!`)
}
}

SapphireClient.plugins.registerPostLoginHook(
CustomSapphireScheduledTasks[postLogin],
"CustomSapphireScheduledTasks-postLogin"
)
Solution:
No. I mean don't call /register of the plugin and make a file similar to this and call it instead. You're already halfway there with your postLogin code. https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/src/register.ts
GitHub
plugins/packages/scheduled-tasks/src/register.ts at main · sapphire...
Plugins for the Sapphire Framework. Contribute to sapphiredev/plugins development by creating an account on GitHub.
Jump to solution
7 Replies
Favna
Favna2mo ago
are you still calling the /register entrypoint of the plugin despite your own code?
secre
secre2mo ago
yep. because I need to register tasks that I need only for 1 bot
Favna
Favna2mo ago
yeah you should remove that and replicate the code in your own register you're currently registering it twice
secre
secre2mo ago
export function loadScheduledTasks() {
const store = "scheduled-tasks" as const

void container.stores.loadPiece({
name: DisableMessageComponents.name,
piece: DisableMessageComponents,
store,
})
}
export function loadScheduledTasks() {
const store = "scheduled-tasks" as const

void container.stores.loadPiece({
name: DisableMessageComponents.name,
piece: DisableMessageComponents,
store,
})
}
this is example load function, by replicating you mean to add here logic to load tasks from their folder e.g. scheduled-tasks?
Solution
Favna
Favna2mo ago
No. I mean don't call /register of the plugin and make a file similar to this and call it instead. You're already halfway there with your postLogin code. https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/src/register.ts
GitHub
plugins/packages/scheduled-tasks/src/register.ts at main · sapphire...
Plugins for the Sapphire Framework. Contribute to sapphiredev/plugins development by creating an account on GitHub.
secre
secre2mo ago
What hook get called first preGenericsInitialization or postInitialization? :Hmm: Oh I managed to get it to work. Initially I just copied all code from your's register.ts and put it to postLogin hook. And nothing happens. I just saw the description to stores.register method, that you need to register store as early as possible. I leave to the postInit stage and now everything works! Final look of the custom plugin:
export class CustomSapphireScheduledTasks extends Plugin {
public static [postInitialization](this: SapphireClient): void {
this.stores.register(new ScheduledTaskStore())
}

public static [postLogin](this: SapphireClient, options: ClientOptions) {
container.tasks = new ScheduledTaskHandler(options.tasks)

loadScheduledTasks()

container.tasks.createRepeated()

container.logger.info(`@yume/sapphire-utilities/tasks registered successfully!`)
}
}

SapphireClient.plugins.registerPostLoginHook(
CustomSapphireScheduledTasks[postLogin],
"CustomSapphireScheduledTasks-postLogin"
)

SapphireClient.plugins.registerPostInitializationHook(
CustomSapphireScheduledTasks[postInitialization],
"CustomSapphireScheduledTasks-postInitialization"
)
export class CustomSapphireScheduledTasks extends Plugin {
public static [postInitialization](this: SapphireClient): void {
this.stores.register(new ScheduledTaskStore())
}

public static [postLogin](this: SapphireClient, options: ClientOptions) {
container.tasks = new ScheduledTaskHandler(options.tasks)

loadScheduledTasks()

container.tasks.createRepeated()

container.logger.info(`@yume/sapphire-utilities/tasks registered successfully!`)
}
}

SapphireClient.plugins.registerPostLoginHook(
CustomSapphireScheduledTasks[postLogin],
"CustomSapphireScheduledTasks-postLogin"
)

SapphireClient.plugins.registerPostInitializationHook(
CustomSapphireScheduledTasks[postInitialization],
"CustomSapphireScheduledTasks-postInitialization"
)