-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from N3aar/feat/scheduled-events
Feat/scheduled events
- Loading branch information
Showing
16 changed files
with
189 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
prisma/migrations/20240610005557_update_date_and_fields/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
Warnings: | ||
- You are about to drop the column `date` on the `Events` table. All the data in the column will be lost. | ||
- Added the required column `day` to the `Events` table without a default value. This is not possible if the table is not empty. | ||
- Added the required column `month` to the `Events` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Events" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"description" TEXT NOT NULL, | ||
"month" INTEGER NOT NULL, | ||
"day" INTEGER NOT NULL, | ||
"repeat" BOOLEAN NOT NULL DEFAULT false, | ||
"guild_id" TEXT, | ||
"created_by" TEXT, | ||
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" DATETIME NOT NULL, | ||
CONSTRAINT "Events_guild_id_fkey" FOREIGN KEY ("guild_id") REFERENCES "Guild" ("id") ON DELETE SET NULL ON UPDATE CASCADE, | ||
CONSTRAINT "Events_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "User" ("discord_id") ON DELETE SET NULL ON UPDATE CASCADE | ||
); | ||
INSERT INTO "new_Events" ("created_at", "created_by", "description", "guild_id", "id", "repeat", "updated_at") SELECT "created_at", "created_by", "description", "guild_id", "id", "repeat", "updated_at" FROM "Events"; | ||
DROP TABLE "Events"; | ||
ALTER TABLE "new_Events" RENAME TO "Events"; | ||
PRAGMA foreign_key_check("Events"); | ||
PRAGMA foreign_keys=ON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
Warnings: | ||
- Added the required column `type` to the `Events` table without a default value. This is not possible if the table is not empty. | ||
*/ | ||
-- RedefineTables | ||
PRAGMA foreign_keys=OFF; | ||
CREATE TABLE "new_Events" ( | ||
"id" TEXT NOT NULL PRIMARY KEY, | ||
"description" TEXT NOT NULL, | ||
"month" INTEGER NOT NULL, | ||
"day" INTEGER NOT NULL, | ||
"type" TEXT NOT NULL, | ||
"repeat" BOOLEAN NOT NULL DEFAULT false, | ||
"guild_id" TEXT, | ||
"created_by" TEXT, | ||
"created_at" DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
"updated_at" DATETIME NOT NULL, | ||
CONSTRAINT "Events_guild_id_fkey" FOREIGN KEY ("guild_id") REFERENCES "Guild" ("id") ON DELETE SET NULL ON UPDATE CASCADE, | ||
CONSTRAINT "Events_created_by_fkey" FOREIGN KEY ("created_by") REFERENCES "User" ("discord_id") ON DELETE SET NULL ON UPDATE CASCADE | ||
); | ||
INSERT INTO "new_Events" ("created_at", "created_by", "day", "description", "guild_id", "id", "month", "repeat", "updated_at") SELECT "created_at", "created_by", "day", "description", "guild_id", "id", "month", "repeat", "updated_at" FROM "Events"; | ||
DROP TABLE "Events"; | ||
ALTER TABLE "new_Events" RENAME TO "Events"; | ||
PRAGMA foreign_key_check("Events"); | ||
PRAGMA foreign_keys=ON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { container } from "@sapphire/pieces"; | ||
import type { TextChannel } from "discord.js"; | ||
|
||
type ChannelMessage = { | ||
channel: TextChannel; | ||
message: string[]; | ||
}; | ||
|
||
export default async function announceScheduledEvent() { | ||
const today = new Date(); | ||
const events = await container.db.events.findMany({ | ||
where: { | ||
month: today.getMonth() + 1, | ||
day: today.getDate(), | ||
type: "DEFAULT", | ||
}, | ||
include: { | ||
guild: true, | ||
}, | ||
}); | ||
|
||
if (!events || events.length <= 0) return; | ||
|
||
const guilds = container.client.guilds.cache; | ||
const channelMessages = new Map<string, ChannelMessage>(); | ||
|
||
for (const event of events) { | ||
if (!event.guildId || !event.guild?.mainChannel) continue; | ||
|
||
const guild = guilds.get(event.guild.discordId); | ||
const channel = guild?.channels.cache.get(event.guild.mainChannel) as | ||
| TextChannel | ||
| undefined; | ||
|
||
if (!channel || !guild) continue; | ||
|
||
if (channelMessages.has(channel.id)) { | ||
const messages = channelMessages.get(channel.id)?.message; | ||
messages?.push(event.description); | ||
} else { | ||
channelMessages.set(channel.id, { | ||
channel, | ||
message: [event.description], | ||
}); | ||
} | ||
|
||
if (!event.repeat) { | ||
await container.db.events.delete({ where: { id: event.id } }); | ||
} | ||
} | ||
|
||
for (const channelMessage of channelMessages.values()) { | ||
const channel = channelMessage.channel; | ||
channel.send( | ||
`# Eventos\n${channelMessage.message.map((m) => `- ${m}`).join("\n")}`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { container } from "@sapphire/pieces"; | ||
import type { Message } from "discord.js"; | ||
import type { Message, TextChannel } from "discord.js"; | ||
|
||
export default function giveXp(message: Message) { | ||
const member = message.member; | ||
const guild = message.guild; | ||
const channel = message.channel as TextChannel; | ||
|
||
if (!member || !guild) return; | ||
if (!member || !guild || !channel) return; | ||
|
||
container.expHandler.addExp(member, guild); | ||
container.expHandler.addExp(member, guild, channel); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { defaultTimeZone } from "@/utils/contants.js"; | ||
import { CronJob } from "cron"; | ||
import announceScheduledEvent from "./announceScheduledEvent.js"; | ||
|
||
export default function startTimers() { | ||
new CronJob( | ||
"* * * * *", | ||
() => { | ||
announceScheduledEvent(); | ||
}, | ||
null, | ||
true, | ||
defaultTimeZone, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters