Recurring Task

Is there anyway i can get my bot to run a task every 10 seconds or so to check if a giveaway is over by querying my mongodb
15 Replies
MrBeast
MrBeastOP2y ago
i know how to check if a giveaway has ended or not and how to handle that but i have no idea how to implement a recurring task in my bot cleanly
Favna
Favna2y ago
Install, register, use @sapphire/plugin-scheduled-tasks We don't have guide pages yet but various bots use if such as @Dragonite
Spinel
Spinel2y ago
Discord bots that use @sapphire/framework v4 - Official Bot Examples ᴱ ᴰ ᴶˢ - Gemboard ᴱ ᴰ - Dragonite ᴱ ᴰ - Radon ᴱ ᴬ - Sapphire Application Commands Examples ᴱ - Archangel ᴱ ᴰ - Zeyr ᴰ ᴬ Discord bots that use @sapphire/framework v3 - Arima ᴱ - Nino ᴱ ᴰ - Operator ᴱ ᴬ ᴰ - Spectera ᴬ Discord bots that use @sapphire/framework v2 - Materia ᴱ - RTByte ᴱ ᴬ - Skyra ᴬ ᴰ - YliasDiscordBot ᴬ : Uses ESM (if not specified then uses CJS) : Advanced bot (if not specified it is a simple bot, or not graded) : Uses Docker in production ᴶˢ: Written in JavaScript. If not specified then the bot is written in TypeScript.
ararou!
ararou!2y ago
i wouldve used it myself but i never got them to fire
Favna
Favna2y ago
Works just fine for dragonite and I know others use it as well
ararou!
ararou!2y ago
it could’ve been my db or smth else but I just use listeners
MrBeast
MrBeastOP2y ago
is there anyway i can use it without using a redis db and maybe a mongo db
Favna
Favna2y ago
Not with this plugin but you can come up with your own system. Mongo however unlike Redis doesn't have key expiry systems which Bullmq relies on
Favna
Favna2y ago
Also regarding having no idea, we live in 2023 yaknow:
Favna
Favna2y ago
To write a task handler for recurring tasks in TypeScript, you can follow these steps: 1. Install the necessary dependencies: - node-schedule: to schedule recurring tasks. - @prisma/client: to connect to your MongoDB database using the Prisma ORM.
You can install these dependencies using npm or yarn:
npm install node-schedule @prisma/client

# OR

yarn add node-schedule @prisma/client

npm install node-schedule @prisma/client

# OR

yarn add node-schedule @prisma/client

2. Create a Task model using the Prisma schema language. This model will represent your scheduled giveaway events. You can define this model in a file called schema.prisma:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model Task {
id String @id @default(uuid())
date DateTime
message String
}

datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}

model Task {
id String @id @default(uuid())
date DateTime
message String
}

3. Generate the Prisma client by running npx prisma generate or yarn prisma generate. This will create the PrismaClient class that you can use to interact with your MongoDB database. 4. Write the task handler function that will handle your scheduled events. This function will use the PrismaClient to retrieve the scheduled events from the database and perform the necessary actions. You can define this function in a file called task-handler.ts:
import { PrismaClient, Task } from '@prisma/client';
import schedule from 'node-schedule';

const prisma = new PrismaClient();

async function handleTasks() {
const tasks = await prisma.task.findMany();

for (const task of tasks) {
const taskDate = new Date(task.date);
const now = new Date();

if (taskDate <= now) {
console.log(`Starting task: ${task.message}`);

// Perform the necessary actions for the task

await prisma.task.delete({ where: { id: task.id } });

console.log(`Finished task: ${task.message}`);
}
}
}

// Schedule the task handler to run every minute
schedule.scheduleJob('*/1 * * * *', handleTasks);

import { PrismaClient, Task } from '@prisma/client';
import schedule from 'node-schedule';

const prisma = new PrismaClient();

async function handleTasks() {
const tasks = await prisma.task.findMany();

for (const task of tasks) {
const taskDate = new Date(task.date);
const now = new Date();

if (taskDate <= now) {
console.log(`Starting task: ${task.message}`);

// Perform the necessary actions for the task

await prisma.task.delete({ where: { id: task.id } });

console.log(`Finished task: ${task.message}`);
}
}
}

// Schedule the task handler to run every minute
schedule.scheduleJob('*/1 * * * *', handleTasks);

In this function, we first retrieve all tasks from the database using prisma.task.findMany(). We then loop through each task, check if its date is before or equal to the current time, and perform the necessary actions for the task. Finally, we delete the task from the database using prisma.task.delete(). 5. Run the task handler using node task-handler.js. This will start the task handler and schedule it to run every minute. That's it! You now have a task handler for recurring tasks that uses MongoDB as a database and the Prisma ORM to interact with that database. That's the response it generated At a glance it covers all the major things
MrBeast
MrBeastOP2y ago
Alright thanks ill try and make my own system based on this information
ararou!
ararou!2y ago
ew mongo do you need to use specifically mongo
Favna
Favna2y ago
Not my choice
MrBeast
MrBeastOP2y ago
whats wrong with mongo, what database do you recommend
ararou!
ararou!2y ago
psql is better atleast imo
Want results from more Discord servers?
Add your server