feat: time command (#26)

Co-authored-by: Evo <85353424+EvolutionX-10@users.noreply.github.com>
This commit is contained in:
2022-11-15 17:28:51 +01:00
committed by GitHub
parent d17e3af572
commit ffe6ac6f8f
5 changed files with 2289 additions and 2119 deletions

167
src/commands/time.ts Normal file
View File

@@ -0,0 +1,167 @@
import { commandModule, CommandType } from "@sern/handler";
import { ApplicationCommandOptionType, GuildMember } from "discord.js";
import { publish } from "#plugins";
import { fetch } from "undici";
import { readFileSync } from "fs";
export default commandModule({
type: CommandType.Slash,
plugins: [publish()],
description: "Get the time of a person.",
options: [
{
name: "create",
description: "Create the timezone where you are in the db.",
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: "timezone",
description: "The timezone where you are located.",
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
command: {
onEvent: [],
execute: async (autocomplete) => {
const input = autocomplete.options.getFocused();
return autocomplete.respond(fuzz(input)).catch(() => null);
},
},
},
],
},
{
name: "get",
description: "Get the time of a user",
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: "locale",
description: "The locale code to format the date",
type: ApplicationCommandOptionType.String,
required: true,
autocomplete: true,
command: {
onEvent: [],
execute: async (autocomplete) => {
const input = autocomplete.options.getFocused();
return autocomplete.respond(fuzz(input, true)).catch(() => null);
},
},
},
{
name: "user",
description: "The user",
type: ApplicationCommandOptionType.User,
required: true,
},
],
},
{
name: "delete",
description: "Delete your entry in the database",
type: ApplicationCommandOptionType.Subcommand,
},
],
execute: async (ctx, [, options]) => {
switch (options.getSubcommand()) {
case "create": {
const reqData = {
timezone: options.getString("timezone", true),
key: process.env.TIME_KEY!,
userid: ctx.user.id,
};
const request = await fetch("https://api.srizan.ml/sern/newTime", {
method: "POST",
body: JSON.stringify(reqData),
headers: {
"Content-Type": "application/json",
},
}).catch(() => null);
const data = (await request?.json()) as Record<string, string>;
if (!data)
return ctx.reply({
content: `Oops, the response errored out for some reason, you could try again...`,
ephemeral: true,
});
return ctx.reply({
content:
data?.ok ?? data?.error ?? "Something went wrong! Please try again",
ephemeral: true,
});
}
case "get": {
const option = options.getMember("user") as GuildMember;
const request = await fetch(
`https://api.srizan.ml/sern/getTime?userid=${option.id}`
).catch(() => null);
const data = (await request?.json()) as APIResponse;
if (!data)
return ctx.reply({
content: `Oopsies, I tried to connect to the API, but something went wrong. Try again, it should work`,
ephemeral: true,
});
if (data.error)
return ctx.reply({
content: `${option}'s timezone data doesn't exist in the database!`,
ephemeral: true,
});
const dateConvert = new Date().toLocaleString("en-GB", {
timeZone: data.timezone,
timeStyle: "full",
dateStyle: "medium",
});
return ctx.reply({
content: `Current time for ${option} is \`${dateConvert}\``,
allowedMentions: { parse: [] },
});
}
case "delete": {
const request = await fetch(
`https://api.srizan.ml/sern/deleteTime?userid=${ctx.user.id}&key=${process.env.TIME_KEY}`,
{
method: "DELETE",
}
).catch(() => null);
const data = (await request?.json()) as Record<string, string>;
if (!data)
return ctx.reply({
content: `Oops, the response errored out for some reason, you could try again...`,
ephemeral: true,
});
return ctx.reply({
content:
data?.ok ?? data?.error ?? "Something went wrong! Please try again",
ephemeral: true,
});
}
}
},
});
function fuzz(s: string, locale = false) {
const path = `./time/${locale ? "countrylocalecodes" : "timezone"}.txt`;
let zones: string[] = JSON.parse(`${readFileSync(path)}`);
zones = zones.filter((choice) =>
choice.toLowerCase().includes(s.toLowerCase())
);
return zones.slice(0, 25).map((z) => ({ name: z, value: z }));
}
interface APIResponse {
error?: string;
timezone?: string;
}

View File

@@ -0,0 +1 @@
["af-ZA","am-ET","ar-AE","ar-BH","ar-DZ","ar-EG","ar-IQ","ar-JO","ar-KW","ar-LB","ar-LY","ar-MA","arn-CL","ar-OM","ar-QA","ar-SA","ar-SD","ar-SY","ar-TN","ar-YE","as-IN","az-az","az-Cyrl-AZ","az-Latn-AZ","ba-RU","be-BY","bg-BG","bn-BD","bn-IN","bo-CN","br-FR","bs-Cyrl-BA","bs-Latn-BA","ca-ES","co-FR","cs-CZ","cy-GB","da-DK","de-AT","de-CH","de-DE","de-LI","de-LU","dsb-DE","dv-MV","el-CY","el-GR","en-029","en-AU","en-BZ","en-CA","en-cb","en-GB","en-IE","en-IN","en-JM","en-MT","en-MY","en-NZ","en-PH","en-SG","en-TT","en-US","en-ZA","en-ZW","es-AR","es-BO","es-CL","es-CO","es-CR","es-DO","es-EC","es-ES","es-GT","es-HN","es-MX","es-NI","es-PA","es-PE","es-PR","es-PY","es-SV","es-US","es-UY","es-VE","et-EE","eu-ES","fa-IR","fi-FI","fil-PH","fo-FO","fr-BE","fr-CA","fr-CH","fr-FR","fr-LU","fr-MC","fy-NL","ga-IE","gd-GB","gd-ie","gl-ES","gsw-FR","gu-IN","ha-Latn-NG","he-IL","hi-IN","hr-BA","hr-HR","hsb-DE","hu-HU","hy-AM","id-ID","ig-NG","ii-CN","in-ID","is-IS","it-CH","it-IT","iu-Cans-CA","iu-Latn-CA","iw-IL","ja-JP","ka-GE","kk-KZ","kl-GL","km-KH","kn-IN","kok-IN","ko-KR","ky-KG","lb-LU","lo-LA","lt-LT","lv-LV","mi-NZ","mk-MK","ml-IN","mn-MN","mn-Mong-CN","moh-CA","mr-IN","ms-BN","ms-MY","mt-MT","nb-NO","ne-NP","nl-BE","nl-NL","nn-NO","no-no","nso-ZA","oc-FR","or-IN","pa-IN","pl-PL","prs-AF","ps-AF","pt-BR","pt-PT","qut-GT","quz-BO","quz-EC","quz-PE","rm-CH","ro-mo","ro-RO","ru-mo","ru-RU","rw-RW","sah-RU","sa-IN","se-FI","se-NO","se-SE","si-LK","sk-SK","sl-SI","sma-NO","sma-SE","smj-NO","smj-SE","smn-FI","sms-FI","sq-AL","sr-BA","sr-CS","sr-Cyrl-BA","sr-Cyrl-CS","sr-Cyrl-ME","sr-Cyrl-RS","sr-Latn-BA","sr-Latn-CS","sr-Latn-ME","sr-Latn-RS","sr-ME","sr-RS","sr-sp","sv-FI","sv-SE","sw-KE","syr-SY","ta-IN","te-IN","tg-Cyrl-TJ","th-TH","tk-TM","tlh-QS","tn-ZA","tr-TR","tt-RU","tzm-Latn-DZ","ug-CN","uk-UA","ur-PK","uz-Cyrl-UZ","uz-Latn-UZ","uz-uz","vi-VN","wo-SN","xh-ZA","yo-NG","zh-CN","zh-HK","zh-MO","zh-SG","zh-TW","zu-ZA"]

1
time/timezone.txt Normal file

File diff suppressed because one or more lines are too long

View File

@@ -6,6 +6,7 @@
"module": "ESNext",
"outDir": "dist",
"strict": true,
"baseUrl": ".",
"esModuleInterop": true,
"noImplicitAny": true,
"strictNullChecks": true,
@@ -13,9 +14,9 @@
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"paths": {
"#plugins": ["./src/plugins/index.js"],
"#utils": ["./src/utils/index.js"],
"#constants": ["./src/constants.js"]
"#plugins": ["src/plugins/index.js"],
"#utils": ["src/utils/index.js"],
"#constants": ["src/constants.js"]
}
}
}

4232
yarn.lock

File diff suppressed because it is too large Load Diff