Skip to content
Snippets Groups Projects
Unverified Commit c03b2593 authored by Maxime FRIESS's avatar Maxime FRIESS :blue_heart:
Browse files

[Command] Switch date on /menu

parent 83a0ab40
Branches
Tags
No related merge requests found
......@@ -18,8 +18,9 @@
*/
import { ApplicationCommandOptionType } from 'discord-api-types/v9';
import { MessageEmbed } from 'discord.js';
import { MessageActionRow, MessageButton, MessageEmbed } from 'discord.js';
import CrousApi from '../api/CrousApi.js';
import Bot from '../Bot.js';
import Command from '../Command.js';
import SoftConfig from '../config/SoftConfig.js';
import Cache from '../utils/Cache.js';
......@@ -27,6 +28,8 @@ import Cache from '../utils/Cache.js';
class MenuCommand extends Command {
constructor() {
super();
Bot.registerButton("menu_next", this.buttonNext.bind(this));
Bot.registerButton("menu_previous", this.buttonPrevious.bind(this));
}
getName() {
......@@ -71,7 +74,34 @@ class MenuCommand extends Command {
const [, day, month, year] = res;
return new Date(year, month - 1, day);
}
}
__getDateInMsg(interaction, daydiff) {
const res = /^.*([0-9]{2})\/([0-9]{2})\/([0-9]{4}).*$/.exec(interaction.message.content);
const [, day, month, year] = res;
return new Date(Date.UTC(year, month - 1, parseInt(day) + daydiff));
}
__getFilterInMsg(interaction) {
const res = /^.*(ETU|PER|ALL).*$/.exec(interaction.message.content);
return res[1];
}
async buttonNext(interaction) {
let date = this.__getDateInMsg(interaction, 1);
let filter = this.__getFilterInMsg(interaction);
await interaction.deferUpdate();
await interaction.editReply(await this.__getMessageContent(date, filter));
}
async buttonPrevious(interaction) {
let date = this.__getDateInMsg(interaction, -1);
console.log(date);
let filter = this.__getFilterInMsg(interaction);
await interaction.deferUpdate();
await interaction.editReply(await this.__getMessageContent(date, filter));
}
async execute(interaction) {
......@@ -84,28 +114,27 @@ class MenuCommand extends Command {
return;
}
await interaction.deferReply();
await interaction.editReply(await this.__getMessageContent(date, filter));
}
async __getMessageContent(date, filter) {
const menu = (await Cache.cache("crous.menu", 3600, () => CrousApi.menu())).data;
const row = new MessageActionRow().addComponents(
new MessageButton()
.setCustomId('menu_previous')
.setLabel("Jour précédent")
.setStyle("PRIMARY"),
new MessageButton()
.setCustomId('menu_next')
.setLabel("Jour suivant")
.setStyle("PRIMARY")
)
const zeroPad = (num, places) => String(num).padStart(places, '0');
const d = `${zeroPad(date.getDate(), 2)}/${zeroPad(date.getMonth() + 1, 2)}/${zeroPad(date.getUTCFullYear(), 4)}`;
const sortfunc = (a, b) => {
if (a.name.includes("PER") && b.name.includes("ETU")) {
return -1;
} else if (a.name.includes("ETU") && b.name.includes("PER")) {
return 1;
} else {
if (a.name.includes("ENTREES") || b.name.includes("DESSERTS")) {
return -1;
} else if (a.name.includes("DESSERTS") || b.name.includes("ENTREES")) {
return 1;
} else {
return a.name.localeCompare(b.name);
}
}
};
await interaction.deferReply();
for (let m of menu) {
if ((new Date(m.date)).setHours(0, 0, 0, 0) == date.setHours(0, 0, 0, 0)) {
......@@ -113,22 +142,34 @@ class MenuCommand extends Command {
if (meal.name == "midi") {
const embed = new MessageEmbed()
.setColor(SoftConfig.get("bot.color", "#fb963a"))
.setTitle(`Menu du ${d}`)
.setTimestamp(Cache.modified_date('crous.menu'))
for (let cat of meal.foodcategory.sort(sortfunc)) {
for (let cat of meal.foodcategory.sort(this.__sortfunc)) {
if (cat.name.includes(filter))
embed.addField(cat.name, cat.dishes.map((x) => ("> " + x.name)).join("\n"), false);
}
await interaction.editReply({ embeds: [embed] });
return;
return { embeds: [embed], content: `Menu ${filter} du ${d}`, components: [row] };
}
}
await interaction.editReply({ content: `Le menu de midi du ${d} n'est pas disponible.` });
return;
return { content: `Le menu ${filter} du ${d} n'est pas disponible.`, components: [row], embeds: [] };
}
}
return { content: `Le menu ${filter} du ${d} n'est pas disponible.`, components: [row], embeds: [] };
}
await interaction.editReply({ content: `Le menu du ${d} n'est pas disponible.` });
__sortfunc(a, b) {
if (a.name.includes("PER") && b.name.includes("ETU")) {
return -1;
} else if (a.name.includes("ETU") && b.name.includes("PER")) {
return 1;
} else {
if (a.name.includes("ENTREES") || b.name.includes("DESSERTS")) {
return -1;
} else if (a.name.includes("DESSERTS") || b.name.includes("ENTREES")) {
return 1;
} else {
return a.name.localeCompare(b.name);
}
}
}
}
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment