Skip to content

Commit

Permalink
Merge pull request #29 from N3aar/feat/reply-greetings
Browse files Browse the repository at this point in the history
feat: reply greetings
  • Loading branch information
N3aar authored Jun 12, 2024
2 parents c2a015a + 797e2f9 commit 9ba0f83
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/commands/anime/anilist.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Media, MediaType } from "@/shared/types/anilistTypes.js";
import { embedColors } from "@/utils/contants.js";
import { formatDate } from "@/utils/dateFormat.js";
import { pascal, replaceUnderlines } from "@/utils/stringFormat.js";
import { capitalize, replaceUnderlines } from "@/utils/stringFormat.js";
import { Command } from "@sapphire/framework";
import { type APIEmbedField, EmbedBuilder, type TextChannel } from "discord.js";

Expand Down Expand Up @@ -106,7 +106,7 @@ export class AnilistCommand extends Command {
if (status) {
fields.push({
name: "Status",
value: pascal(replaceUnderlines(status)),
value: capitalize(replaceUnderlines(status)),
inline: true,
});
}
Expand Down Expand Up @@ -195,15 +195,15 @@ export class AnilistCommand extends Command {
if (format) {
fields.push({
name: "Format",
value: pascal(replaceUnderlines(format)),
value: capitalize(replaceUnderlines(format)),
inline: true,
});
}

if (status) {
fields.push({
name: "Status",
value: pascal(replaceUnderlines(status)),
value: capitalize(replaceUnderlines(status)),
inline: true,
});
}
Expand All @@ -219,15 +219,15 @@ export class AnilistCommand extends Command {
if (source) {
fields.push({
name: "Source",
value: pascal(source),
value: capitalize(source),
inline: true,
});
}

if (season) {
fields.splice(6, 0, {
name: "Season",
value: pascal(season),
value: capitalize(season),
inline: true,
});
}
Expand Down
82 changes: 82 additions & 0 deletions src/events/replyGreeting.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import { greetings, specialDates, timeRanges } from "@/utils/contants.js";
import { capitalize } from "@/utils/stringFormat.js";
import { container } from "@sapphire/pieces";
import type { Message, User } from "discord.js";

type currentDate = {
hours: number;
day: number;
month: number;
};

type hourRange = {
start: number;
end: number;
};

type monthRange = {
day: number;
month: number;
};

export default async function replyGreeting(message: Message): Promise<void> {
const content = message.content.toLowerCase();
const member = message.member;
const mentioned = message.mentions.has(container.client.user as User);

if (
!member ||
!mentioned ||
greetings.every((greeting) => !content.includes(greeting))
)
return;

const { hours, day, month } = getCurrentDateTime();
const greeting = getGreeting(content, hours, day, month);

if (greeting) {
await message.channel.send(`${member.toString()} ${greeting}`);
}
}

function getGreeting(
content: string,
hours: number,
day: number,
month: number,
): string | null {
const greetings: { [key: string]: () => boolean } = {
"bom dia": () => isWithinTimeRange(hours, timeRanges.morning),
"boa tarde": () => isWithinTimeRange(hours, timeRanges.afternoon),
"boa noite": () => isWithinTimeRange(hours, timeRanges.night),
"feliz natal": () => isSpecialDate(day, month, specialDates.christmas),
"feliz ano novo": () => isSpecialDate(day, month, specialDates.newYear),
};

for (const [greeting, condition] of Object.entries(greetings)) {
if (new RegExp(`\\b${greeting}\\b`).test(content) && condition()) {
return `${capitalize(greeting)}!`;
}
}

return null;
}

function getCurrentDateTime(): currentDate {
const date = new Date();
return {
hours: date.getHours(),
day: date.getDate(),
month: date.getMonth() + 1,
};
}

function isWithinTimeRange(hours: number, range: hourRange): boolean {
return range.start <= range.end
? hours >= range.start && hours <= range.end
: hours >= range.start || hours <= range.end;
}

function isSpecialDate(day: number, month: number, date: monthRange): boolean {
return day === date.day && month === date.month;
}
2 changes: 2 additions & 0 deletions src/listeners/messageCreate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import emojiHelper from "@/events/emojiHelper.js";
import giveXp from "@/events/giveXp.js";
import replyGreeting from "@/events/replyGreeting.js";
import urlHelper from "@/events/urlHelper.js";
import { Listener } from "@sapphire/framework";
import type { Message } from "discord.js";
Expand All @@ -19,6 +20,7 @@ export class MessageCreate extends Listener {
if (message.author.bot) return;

emojiHelper(message);
replyGreeting(message);
giveXp(message);
urlHelper(message);
}
Expand Down
19 changes: 19 additions & 0 deletions src/utils/contants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ export const birthdayVideos = [
"https://utfs.io/f/5f2da89f-cffd-42f3-bee6-1320e907f68a-z5703.mp4",
];

export const greetings = [
"bom dia",
"boa tarde",
"boa noite",
"feliz natal",
"feliz ano novo",
];

export const timeRanges = {
morning: { start: 6, end: 11 },
afternoon: { start: 12, end: 18 },
night: { start: 19, end: 5 },
};

export const specialDates = {
christmas: { day: 25, month: 12 },
newYear: { day: 1, month: 1 },
};

export const expValues = {
min: 10,
max: 25,
Expand Down
2 changes: 1 addition & 1 deletion src/utils/stringFormat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function pascal(text: string) {
export function capitalize(text: string) {
return text
?.split(" ")
.map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
Expand Down

0 comments on commit 9ba0f83

Please sign in to comment.