Skip to content

Commit

Permalink
Add new config option to working hours field
Browse files Browse the repository at this point in the history
  • Loading branch information
ralphkb committed Apr 27, 2024
1 parent df35750 commit 2a874d5
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 35 deletions.
5 changes: 3 additions & 2 deletions config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,11 @@ workingHours:
- day: "sunday"
min: "09:00"
max: "12:00"
blockTicketCreation: true
blockTicketCreation: true
addField: true # If working hours is enabled, this option will enable or disable the addition of the working hours field to the ticket creation embed
fieldTitle: "Working Hours" # The title of the working hours field
fieldValue: "> {day}: {openingTime} to {closingTime}" # The value of "each" working hours field (depending on how many days you configured above), use {day} for today's day of the week, {openingTime} for the opening time and {closingTime} for the closing time
valueDays: "TODAY" # Options are: "TODAY" which will only show today's working hours or "ALL" which will show the configured working hours for each day
fieldValue: "> {day}: {openingTime} to {closingTime}" # The value of each working hours field, use {day} for today's day of the week, {openingTime} for the opening time and {closingTime} for the closing time

# The embed that is sent when the ticket creation is blocked due to being outside the working hours
workingHoursEmbed:
Expand Down
83 changes: 50 additions & 33 deletions events/interactionCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -1885,42 +1885,59 @@ module.exports = {

if (config.workingHours.enabled && config.workingHours.addField) {
let workingHoursText = "";
const currentDay = userCurrentTime.format("dddd").toLowerCase();
for (const day in workingHours) {
const { min, max } = workingHours[day];
const isCurrentDay = day === currentDay;
const dayText = isCurrentDay
? `**${day.charAt(0).toUpperCase() + day.slice(1)}**`
: day.charAt(0).toUpperCase() + day.slice(1);
let openTime = min || config.workingHours.default.min;
let closeTime = max || config.workingHours.default.max;

const openTimeToday = userCurrentTime
.clone()
.startOf("day")
.set({
hour: openTime.split(":")[0],
minute: openTime.split(":")[1],
});
if (config.workingHours.valueDays === "ALL") {
const currentDay = userCurrentTime.format("dddd").toLowerCase();
for (const day in workingHours) {
const { min, max } = workingHours[day];
const isCurrentDay = day === currentDay;
const dayText = isCurrentDay
? `**${day.charAt(0).toUpperCase() + day.slice(1)}**`
: day.charAt(0).toUpperCase() + day.slice(1);
let openTime = min || config.workingHours.default.min;
let closeTime = max || config.workingHours.default.max;

const openTimeToday = userCurrentTime
.clone()
.startOf("day")
.set({
hour: openTime.split(":")[0],
minute: openTime.split(":")[1],
});

const closeTimeToday = userCurrentTime
.clone()
.startOf("day")
.set({
hour: closeTime.split(":")[0],
minute: closeTime.split(":")[1],
});
const closeTimeToday = userCurrentTime
.clone()
.startOf("day")
.set({
hour: closeTime.split(":")[0],
minute: closeTime.split(":")[1],
});

const openingTimestamp = `<t:${openTimeToday.unix()}:t>`;
const closingTimestamp = `<t:${closeTimeToday.unix()}:t>`;
const openingTimestamp = `<t:${openTimeToday.unix()}:t>`;
const closingTimestamp = `<t:${closeTimeToday.unix()}:t>`;

const workingHoursField = config.workingHours.fieldValue
? `${config.workingHours.fieldValue}\n`
: `> {day}: {openingTime} to {closingTime}\n`;
workingHoursText += workingHoursField
.replace(/\{day\}/g, dayText)
.replace(/\{openingTime\}/g, openingTimestamp)
.replace(/\{closingTime\}/g, closingTimestamp);
const workingHoursField = config.workingHours.fieldValue
? `${config.workingHours.fieldValue}\n`
: `> {day}: {openingTime} to {closingTime}\n`;
workingHoursText += workingHoursField
.replace(/\{day\}/g, dayText)
.replace(/\{openingTime\}/g, openingTimestamp)
.replace(/\{closingTime\}/g, closingTimestamp);
}
} else if (config.workingHours.valueDays === "TODAY") {
workingHoursText +=
`${config.workingHours.fieldValue || "> {day}: {openingTime} to {closingTime}"}`
.replace(
/\{day\}/g,
dayToday.charAt(0).toUpperCase() + dayToday.slice(1),
)
.replace(
/\{openingTime\}/g,
`<t:${openingTimeToday.unix()}:t>`,
)
.replace(
/\{closingTime\}/g,
`<t:${closingTimeToday.unix()}:t>`,
);
}
ticketOpenEmbed.addFields({
name: config.workingHours.fieldTitle || "Working Hours",
Expand Down

0 comments on commit 2a874d5

Please sign in to comment.