mirror of
https://github.com/JonnyBro/JaBa.git
synced 2024-12-28 14:23:02 +05:00
Compare commits
4 commits
4593f2804d
...
2fd25f074d
Author | SHA1 | Date | |
---|---|---|---|
|
2fd25f074d | ||
|
b45f3dcc35 | ||
|
823aea886f | ||
|
c2d1c85696 |
4 changed files with 155 additions and 100 deletions
2
src/adapters/cache/MapCache.js
vendored
2
src/adapters/cache/MapCache.js
vendored
|
@ -1,4 +1,4 @@
|
||||||
import ICacheAdapter from "./ICacheAdapter";
|
import ICacheAdapter from "./ICacheAdapter.js";
|
||||||
|
|
||||||
export default class MapCache extends ICacheAdapter {
|
export default class MapCache extends ICacheAdapter {
|
||||||
constructor() {
|
constructor() {
|
||||||
|
|
|
@ -1,69 +1,71 @@
|
||||||
import { CronJob } from "cron";
|
import { CronJob } from "cron";
|
||||||
|
import useClient from "../utils/use-client.js";
|
||||||
|
import UserModel from "../models/UserModel.js";
|
||||||
|
import { createEmbed } from "../utils/create-embed.js";
|
||||||
|
import logger from "./logger.js";
|
||||||
|
import { getNoun } from "./functions.js";
|
||||||
|
|
||||||
/**
|
const checkBirthdays = async () => {
|
||||||
*
|
const client = useClient();
|
||||||
* @param {import("../base/Client")} client
|
|
||||||
*/
|
const guilds = client.guilds.cache.values();
|
||||||
async function checkBirthdays(client) {
|
const users = await client.adapter.find(UserModel, { birthdate: { $gt: 1 } });
|
||||||
for (const guild of client.guilds.cache.values()) {
|
|
||||||
|
const currentData = new Date();
|
||||||
|
const currentYear = currentData.getFullYear();
|
||||||
|
const currentMonth = currentData.getMonth();
|
||||||
|
const currentDate = currentData.getDate();
|
||||||
|
|
||||||
|
for (const guild of guilds) {
|
||||||
try {
|
try {
|
||||||
const guildData = await client.getGuildData(guild.id);
|
const data = await client.getGuildData(guild.id);
|
||||||
const channel = guildData.plugins.birthdays ? await client.channels.fetch(guildData.plugins.birthdays) : null;
|
const channel = data.plugins.birthdays ? await client.channels.fetch(data.plugins.birthdays) : null;
|
||||||
|
|
||||||
if (channel) {
|
if (!channel) return;
|
||||||
const date = new Date();
|
|
||||||
const currentDay = date.getDate();
|
|
||||||
const currentMonth = date.getMonth() + 1;
|
|
||||||
const currentYear = date.getFullYear();
|
|
||||||
|
|
||||||
const users = await client.usersData.find({ birthdate: { $gt: 1 } });
|
const userIDs = users.filter(u => guild.members.cache.has(u.id)).map(u => u.id);
|
||||||
|
|
||||||
for (const user of users) {
|
await Promise.all(
|
||||||
if (!guild.members.cache.has(user.id)) continue;
|
userIDs.map(async userID => {
|
||||||
|
const user = users.find(u => u.id === userID);
|
||||||
|
const userData = new Date(user.birthdate).getFullYear() <= 1970 ? new Date(user.birthdate * 1000) : new Date(user.birthdate);
|
||||||
|
const userYear = userData.getFullYear();
|
||||||
|
const userMonth = userData.getMonth();
|
||||||
|
const userDate = userData.getDate();
|
||||||
|
|
||||||
const userDate = new Date(user.birthdate).getFullYear() <= 1970 ? new Date(user.birthdate * 1000) : new Date(user.birthdate);
|
const age = currentYear - userYear;
|
||||||
const day = userDate.getDate();
|
|
||||||
const month = userDate.getMonth() + 1;
|
|
||||||
const year = userDate.getFullYear();
|
|
||||||
const age = currentYear - year;
|
|
||||||
|
|
||||||
if (currentMonth === month && currentDay === day) {
|
if (userDate === currentDate && userMonth === currentMonth) {
|
||||||
const embed = client.embed({
|
const embed = createEmbed({
|
||||||
author: client.user.getUsername(),
|
author: client.user.username,
|
||||||
fields: [
|
fields: [
|
||||||
{
|
{
|
||||||
name: client.translate("economy/birthdate:HAPPY_BIRTHDAY", null, guildData.language),
|
name: client.translate("economy/birthdate:HAPPY_BIRTHDAY", {
|
||||||
value: client.translate(
|
lng: data.language,
|
||||||
"economy/birthdate:HAPPY_BIRTHDAY_MESSAGE",
|
}),
|
||||||
{
|
value: client.translate("economy/birthdate:HAPPY_BIRTHDAY_MESSAGE", {
|
||||||
user: user.id,
|
lng: data.language,
|
||||||
age: `**${age}** ${client.functions.getNoun(
|
user: user.id,
|
||||||
age,
|
age: `**${age}** ${getNoun(age, [
|
||||||
client.translate("misc:NOUNS:AGE:1", null, guildData.language),
|
client.translate("misc:NOUNS:AGE:1", null, data.language),
|
||||||
client.translate("misc:NOUNS:AGE:2", null, guildData.language),
|
client.translate("misc:NOUNS:AGE:2", null, data.language),
|
||||||
client.translate("misc:NOUNS:AGE:5", null, guildData.language),
|
client.translate("misc:NOUNS:AGE:5", null, data.language),
|
||||||
)}`,
|
])}`,
|
||||||
},
|
}),
|
||||||
guildData.language,
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
await channel.send({ embeds: [embed] }).then(m => m.react("🎉"));
|
await channel.send({ embeds: [embed] }).then(m => m.react(" "));
|
||||||
}
|
}
|
||||||
}
|
}),
|
||||||
}
|
);
|
||||||
} catch (e) {
|
} catch (error) {
|
||||||
if (e.code === 10003) console.log(`No channel found for ${guild.name}`);
|
logger.error(error);
|
||||||
else console.error(`Error processing birthdays for guild "${guild.name}":`, e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
export async function init(client) {
|
export async function init() {
|
||||||
new CronJob("0 5 * * *", checkBirthdays(client), null, true, "Europe/Moscow");
|
new CronJob("0 5 * * *", checkBirthdays(), null, true, "Europe/Moscow");
|
||||||
}
|
|
||||||
export async function run(client) {
|
|
||||||
await checkBirthdays(client);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,66 +1,59 @@
|
||||||
/**
|
import UserModel from "../models/UserModel";
|
||||||
*
|
import useClient from "../utils/use-client";
|
||||||
* @param {import("../base/Client")} client
|
|
||||||
*/
|
const checkReminds = async () => {
|
||||||
async function checkReminds(client) {
|
const client = useClient();
|
||||||
client.usersData.find({ reminds: { $gt: [] } }).then(users => {
|
|
||||||
|
client.adapter.find(UserModel, { reminds: { $gt: [] } }).then(users => {
|
||||||
for (const user of users) {
|
for (const user of users) {
|
||||||
if (!client.users.cache.has(user.id)) client.users.fetch(user.id);
|
if (!client.users.cache.has(user.id)) client.users.fetch(user.id);
|
||||||
|
|
||||||
client.databaseCache.usersReminds.set(user.id, user);
|
client.cacheReminds.set(user.id, user);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.databaseCache.usersReminds.forEach(async user => {
|
client.cacheReminds.forEach(async user => {
|
||||||
const cachedUser = client.users.cache.get(user.id);
|
const cachedUser = client.users.cache.get(user.id);
|
||||||
|
|
||||||
if (cachedUser) {
|
if (!cachedUser) return;
|
||||||
const dateNow = Math.floor(Date.now() / 1000),
|
|
||||||
reminds = user.reminds,
|
|
||||||
mustSent = reminds.filter(r => r.sendAt < dateNow);
|
|
||||||
|
|
||||||
if (mustSent.length > 0) {
|
const reminds = user.reminds,
|
||||||
mustSent.forEach(r => {
|
mustSent = reminds.filter(r => r.sendAt < Math.floor(Date.now() / 1000));
|
||||||
const embed = client.embed({
|
|
||||||
author: client.translate("general/remindme:EMBED_TITLE"),
|
|
||||||
fields: [
|
|
||||||
{
|
|
||||||
name: client.translate("general/remindme:EMBED_CREATED"),
|
|
||||||
value: `<t:${r.createdAt}:f>`,
|
|
||||||
inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: client.translate("general/remindme:EMBED_TIME"),
|
|
||||||
value: `<t:${r.sendAt}:f>`,
|
|
||||||
inline: true,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
name: client.translate("common:MESSAGE"),
|
|
||||||
value: r.message,
|
|
||||||
},
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
cachedUser.send({
|
if (!mustSent.length) return;
|
||||||
embeds: [embed],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
user.reminds = user.reminds.filter(r => r.sendAt >= dateNow);
|
mustSent.forEach(r => {
|
||||||
|
const embed = client.embed({
|
||||||
|
author: client.translate("general/remindme:EMBED_TITLE"),
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
name: client.translate("general/remindme:EMBED_CREATED"),
|
||||||
|
value: `<t:${r.createdAt}:f>`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: client.translate("general/remindme:EMBED_TIME"),
|
||||||
|
value: `<t:${r.sendAt}:f>`,
|
||||||
|
inline: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: client.translate("common:MESSAGE"),
|
||||||
|
value: r.message,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
await user.save();
|
cachedUser.send({ embeds: [embed] }).then(() => {
|
||||||
|
client.adapter.updateOne(UserModel, { id: user.id }, { $pull: { reminds: { _id: r._id } } });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
if (user.reminds.length === 0) client.databaseCache.usersReminds.delete(user.id);
|
if (!user.reminds.length) client.cacheReminds.delete(user.id);
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
export async function init(client) {
|
export const init = async () => {
|
||||||
setInterval(async () => {
|
setInterval(async () => {
|
||||||
await checkReminds(client);
|
await checkReminds();
|
||||||
}, 1000);
|
}, 1000);
|
||||||
}
|
};
|
||||||
export async function run(client) {
|
|
||||||
await checkReminds(client);
|
|
||||||
}
|
|
|
@ -7,6 +7,9 @@ import logger from "../helpers/logger.js";
|
||||||
import ConfigService from "../services/config/index.js";
|
import ConfigService from "../services/config/index.js";
|
||||||
import InternationalizationService from "../services/languages/index.js";
|
import InternationalizationService from "../services/languages/index.js";
|
||||||
import { SUPER_CONTEXT } from "../constants/index.js";
|
import { SUPER_CONTEXT } from "../constants/index.js";
|
||||||
|
import GuildModel from "../models/GuildModel.js";
|
||||||
|
import UserModel from "../models/UserModel.js";
|
||||||
|
import MemberModel from "../models/MemberModel.js";
|
||||||
|
|
||||||
export class ExtendedClient extends Client {
|
export class ExtendedClient extends Client {
|
||||||
/**
|
/**
|
||||||
|
@ -21,6 +24,7 @@ export class ExtendedClient extends Client {
|
||||||
this.configService = new ConfigService();
|
this.configService = new ConfigService();
|
||||||
this.adapter = new MongooseAdapter(this.configService.get("mongoDB"));
|
this.adapter = new MongooseAdapter(this.configService.get("mongoDB"));
|
||||||
this.i18n = new InternationalizationService(this);
|
this.i18n = new InternationalizationService(this);
|
||||||
|
this.cacheReminds = new Map();
|
||||||
new Player(this);
|
new Player(this);
|
||||||
|
|
||||||
SUPER_CONTEXT.enterWith(this);
|
SUPER_CONTEXT.enterWith(this);
|
||||||
|
@ -37,4 +41,60 @@ export class ExtendedClient extends Client {
|
||||||
logger.error(error);
|
logger.error(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieves a guild data object from the database.
|
||||||
|
* @param {string} guildId - The ID of the guild to find or create.
|
||||||
|
* @returns {Promise<GuildModel>} The guild data object, either retrieved from the database or newly created.
|
||||||
|
*/
|
||||||
|
async getGuildData(guildId) {
|
||||||
|
let guildData = await this.adapter.findOne(GuildModel, { id: guildId });
|
||||||
|
|
||||||
|
if (!guildData) {
|
||||||
|
guildData = new GuildModel({ id: guildId });
|
||||||
|
await guildData.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return guildData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a User data from the database.
|
||||||
|
* @param {string} userID - The ID of the user to find or create.
|
||||||
|
* @returns {Promise<UserModel>} The user data object, either retrieved from the database or newly created.
|
||||||
|
*/
|
||||||
|
async getUserData(userID) {
|
||||||
|
let userData = await this.adapter.findOne(UserModel, { id: userID });
|
||||||
|
|
||||||
|
if (!userData) {
|
||||||
|
userData = new UserModel({ id: userID });
|
||||||
|
await userData.save();
|
||||||
|
}
|
||||||
|
|
||||||
|
return userData;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a Member data from the database.
|
||||||
|
* @param {string} memberId - The ID of the member to find or create.
|
||||||
|
* @param {string} guildId - The ID of the guild the member belongs to.
|
||||||
|
* @returns {Promise<MemberModel>} The member data object, either retrieved from the database or newly created.
|
||||||
|
*/
|
||||||
|
async getMemberData(memberId, guildId) {
|
||||||
|
let memberData = await this.adapter.findOne(MemberModel, { guildID: guildId, id: memberId });
|
||||||
|
|
||||||
|
if (!memberData) {
|
||||||
|
memberData = new MemberModel({ id: memberId, guildID: guildId });
|
||||||
|
await memberData.save();
|
||||||
|
|
||||||
|
const guildData = await this.getGuildData(guildId);
|
||||||
|
|
||||||
|
if (guildData) {
|
||||||
|
guildData.members.push(memberData._id);
|
||||||
|
await guildData.save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return memberData;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue