-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/timezone
command, allow to set your TZ & compare with others (#68)
* feat: adds timezone command, allows to set your TZ & compare with another user
- Loading branch information
Showing
16 changed files
with
1,162 additions
and
434 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,3 +14,4 @@ | |
/node_modules/ | ||
/src/config.ts | ||
build/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"ISDEV", | ||
"kubectl", | ||
"Kubernetes", | ||
"luxon", | ||
"MYAPP", | ||
"openai", | ||
"perma", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { SlashCommandBuilder } from '@discordjs/builders' | ||
import { CommandInteraction } from 'discord.js' | ||
import { timezonesController } from '../controllers/plugins/timezones.controller' | ||
import { logger } from '../utils/debugging' | ||
|
||
module.exports = { | ||
ephemeral: true, | ||
data: new SlashCommandBuilder() | ||
.setName('timezone') | ||
.setDescription('Set your timezone & get the time of another user') | ||
.setDefaultMemberPermissions('0') | ||
.addSubcommand((command) => | ||
command | ||
.setName('set') | ||
.setDescription('Set your timezone') | ||
.addStringOption((option) => | ||
option.setName('zone').setDescription('Your timezone(TZ Identifier)').setRequired(true), | ||
), | ||
) | ||
.addSubcommand((command) => | ||
command | ||
.setName('difference') | ||
.setDescription('Shows the differences between your timezone and another user') | ||
.addUserOption((option) => | ||
option.setName('user').setDescription('@username').setRequired(true), | ||
), | ||
) | ||
.addSubcommand((command) => | ||
command.setName('list').setDescription('Lists all the available timezones'), | ||
), | ||
async execute(interaction: CommandInteraction) { | ||
try { | ||
if (!interaction.isChatInputCommand()) return | ||
|
||
if (interaction.options.getSubcommand() === 'list') { | ||
return interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: 'Timezones list', | ||
description: `Find your timezone 🔗[here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) and copy the data on the **TZ Identifier**`, | ||
}, | ||
], | ||
}) | ||
} else { | ||
await timezonesController(interaction) | ||
} | ||
} catch (error) { | ||
logger('❌ Command: timezone: ', error) | ||
} | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import formatDistance from 'date-fns/formatDistance' | ||
import { ChatInputCommandInteraction, Colors } from 'discord.js' | ||
import { TIMEZONES_LIST } from '../../data/timezones' | ||
import supabase from '../../libs/supabase' | ||
import { extractHours, getTimeZonesTime } from '../../utils/dates' | ||
import { TIME_ZONES_REGEX } from '../../utils/regex' | ||
|
||
export const timezonesController = async (interaction: ChatInputCommandInteraction) => { | ||
try { | ||
const command = interaction.options.getSubcommand() | ||
if (command === 'set') { | ||
const timezone = interaction.options.getString('zone', true).trim() | ||
|
||
if (!TIME_ZONES_REGEX.test(timezone) || !TIMEZONES_LIST.includes(timezone)) { | ||
return interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: '❗️ Invalid timezone', | ||
description: `The timezone **${timezone}** you provided is invalid. Please use the following format: **America/New_York**. You can find a list of valid timezones [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)`, | ||
color: Colors.Red, | ||
}, | ||
], | ||
}) | ||
} | ||
|
||
// Check if user already has a timezone set | ||
const { data } = await supabase | ||
.from('users_settings') | ||
.select('*') | ||
.eq('user_id', interaction.user.id) | ||
.eq('type', 'timezone') | ||
.single() | ||
|
||
// If not, insert it, otherwise update it | ||
if (!data) { | ||
await supabase | ||
.from('users_settings') | ||
.insert({ | ||
user_id: interaction.user.id, | ||
type: 'timezone', | ||
metadata: { timezone }, | ||
}) | ||
.select() | ||
} else { | ||
await supabase | ||
.from('users_settings') | ||
.update({ | ||
metadata: { timezone }, | ||
}) | ||
.eq('user_id', interaction.user.id) | ||
.eq('type', 'timezone') | ||
} | ||
|
||
interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: 'Timezone set', | ||
description: `Your timezone has been set to **${timezone}**`, | ||
}, | ||
], | ||
}) | ||
} else if (command === 'compare') { | ||
// Get the target & author user | ||
const targetUser = interaction.options.getUser('user', true) | ||
const authorUser = interaction.user | ||
|
||
// Check if both users have a timezone set | ||
const { data } = await supabase | ||
.from('users_settings') | ||
.select('*') | ||
.in('user_id', [targetUser.id, authorUser.id]) | ||
|
||
const targetUserData = data.filter((d) => d.user_id === targetUser.id)[0] | ||
const authorUserData = data.filter((d) => d.user_id === authorUser.id)[0] | ||
|
||
// If both users have a timezone set, compare them | ||
if (data[0] && data[1]) { | ||
const targetUserTimezone = (targetUserData.metadata as { timezone?: string })?.timezone | ||
const authorTimezone = (authorUserData.metadata as { timezone?: string })?.timezone | ||
|
||
const getTimeZones = getTimeZonesTime(targetUserTimezone, authorTimezone) | ||
|
||
const timeDifference = formatDistance( | ||
new Date(getTimeZones.authorLocalTime), | ||
new Date(getTimeZones.targetLocalTime), | ||
) | ||
|
||
const targetTimezoneIsInFuture = | ||
new Date(getTimeZones.targetLocalTime) > new Date(getTimeZones.authorLocalTime) | ||
|
||
return interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: '🕑 Timezone difference', | ||
description: `**${targetUser.username}** is currently **${timeDifference} ${ | ||
targetTimezoneIsInFuture ? 'ahead' : 'behind' | ||
}** with his local time being **${extractHours( | ||
getTimeZones.targetLocalTime, | ||
)}** and yours being **${extractHours(getTimeZones.authorLocalTime)}**`, | ||
fields: [ | ||
{ | ||
name: 'Your timezone', | ||
value: `${authorTimezone} (${extractHours(getTimeZones.authorLocalTime)})`, | ||
inline: true, | ||
}, | ||
{ | ||
name: `${targetUser.username}'s timezone`, | ||
value: `${targetUserTimezone} (${extractHours(getTimeZones.targetLocalTime)})`, | ||
inline: true, | ||
}, | ||
], | ||
color: Colors.Green, | ||
}, | ||
], | ||
}) | ||
} else { | ||
return interaction.editReply({ | ||
embeds: [ | ||
{ | ||
title: '❗️ Timezone not set', | ||
description: `${targetUser.username} has not set their timezone yet.`, | ||
color: Colors.Red, | ||
}, | ||
], | ||
}) | ||
} | ||
} | ||
} catch (error) { | ||
console.log('💢 ERROR: timezonesController(): ', error) | ||
throw Error(error.message) | ||
} | ||
} |
Oops, something went wrong.