Skip to content

Commit

Permalink
feat: add avatar update based on holiday
Browse files Browse the repository at this point in the history
  • Loading branch information
Guilhermeasper committed Sep 17, 2023
1 parent 42df8ce commit 47bcbb0
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
46 changes: 44 additions & 2 deletions src/bot/events/ready.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { Client } from 'discord.js';
import { join } from 'path';

import { BotEvent } from 'src/types';
import { logger } from 'src/utils/logger';
import { getBicho } from 'src/utils/bichoGame';
import GuildModel from 'src/database/schemas/guild';
import { isDateInRange } from 'src/utils/dateRange';

export const ready: BotEvent = {
name: 'ready',
once: true,
execute: (client: Client) => {
execute: async (client: Client) => {
logger.info(`Logged in as ${client.user?.tag}`);
logger.info(`Marquinhos™ is online!`);
// Start heartbeat for bicho game
startBichoGame(client);
await updateAvatarBasedOnHoliday(client);
// Marquinhos for Multiple guilds
const guilds = client.guilds.cache.map((guild) => guild.id);
guilds.forEach(async (guild) => {
Expand All @@ -39,9 +42,48 @@ export const ready: BotEvent = {
},
};

async function startBichoGame(client: Client) {
function startBichoGame(client: Client) {
client.user.setActivity(getBicho());
setInterval(function () {
client.user.setActivity(getBicho());
}, 100 * 1000);
}

async function updateAvatarBasedOnHoliday(client: Client) {
const avatar = getAvatar(client?.user.avatar);
if (!avatar) return;
await client.user.setAvatar(
join(__dirname, `../../resources/images/${avatar}`)
);
}

function getAvatar(avatarHash: string | null) {
const regularAvatar = 'marquinhoshead.jpg';
const regularAvatarHash = '3b124d750ce2a473031e559993179653';

const christmasAvatar = 'marquinhosnatal.png';
const christmasAvatarHash = 'b4b215d7e3176ce99b73097c7f3d5985';
const christmasStartDate = '27/11';
const christmasEndDate = '06/01';

const halloweenAvatar = 'marquinhos_halloween.png';
const halloweenAvatarHash = '40aab1d039453c791474e772c18bb6a0';
const halloweenStartDate = '24/10';
const halloweenEndDate = '06/11';

if (isDateInRange(halloweenStartDate, halloweenEndDate)) {
if (avatarHash === halloweenAvatarHash) {
return null;
}
return halloweenAvatar;
} else if (isDateInRange(christmasStartDate, christmasEndDate)) {
if (avatarHash === christmasAvatarHash) {
return null;
}
return christmasAvatar;
}
if (avatarHash === regularAvatarHash) {
return null;
}
return regularAvatar;
}
28 changes: 28 additions & 0 deletions src/utils/dateRange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export function isDateInRange(startDate: string, endDate: string): boolean {
const currentDate = new Date();
const startParts = startDate.split('/');
const endParts = endDate.split('/');

if (startParts.length !== 2 || endParts.length !== 2) {
throw new Error('Invalid date format. Please use "dd/mm" format.');
}

const startDay = parseInt(startParts[0], 10);
const startMonth = parseInt(startParts[1], 10) - 1;
const endDay = parseInt(endParts[0], 10);
const endMonth = parseInt(endParts[1], 10) - 1;

const endDateIsNextYear = endMonth < startMonth;

const startDateObj = new Date(
currentDate.getFullYear(),
startMonth,
startDay
);
const endDateObj = new Date(
currentDate.getFullYear() + (endDateIsNextYear ? 1 : 0),
endMonth,
endDay
);
return currentDate >= startDateObj && currentDate <= endDateObj;
}

0 comments on commit 47bcbb0

Please sign in to comment.