!timezone: add fuzzy matching for timezone name; add reset
This commit is contained in:
parent
4ae8cf85a3
commit
753ceda5ec
6 changed files with 90 additions and 7 deletions
|
@ -45,4 +45,11 @@ export class GuildMemberTimezones extends BaseGuildRepository {
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
reset(memberId: string) {
|
||||
return this.memberTimezones.delete({
|
||||
guild_id: this.guildId,
|
||||
member_id: memberId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ import { getGuildTz } from "./functions/getGuildTz";
|
|||
import { getMemberTz } from "./functions/getMemberTz";
|
||||
import { getDateFormat } from "./functions/getDateFormat";
|
||||
import { inMemberTz } from "./functions/inMemberTz";
|
||||
import { ResetTimezoneCmd } from "./commands/ResetTimezoneCmd";
|
||||
|
||||
const defaultOptions: PluginOptions<TimeAndDatePluginType> = {
|
||||
config: {
|
||||
|
@ -34,7 +35,12 @@ export const TimeAndDatePlugin = zeppelinPlugin<TimeAndDatePluginType>()("time_a
|
|||
configSchema: ConfigSchema,
|
||||
defaultOptions,
|
||||
|
||||
commands: [SetTimezoneCmd, ViewTimezoneCmd],
|
||||
// prettier-ignore
|
||||
commands: [
|
||||
ResetTimezoneCmd,
|
||||
SetTimezoneCmd,
|
||||
ViewTimezoneCmd,
|
||||
],
|
||||
|
||||
public: {
|
||||
getGuildTz: mapToPublicFn(getGuildTz),
|
||||
|
|
21
backend/src/plugins/TimeAndDate/commands/ResetTimezoneCmd.ts
Normal file
21
backend/src/plugins/TimeAndDate/commands/ResetTimezoneCmd.ts
Normal file
|
@ -0,0 +1,21 @@
|
|||
import { timeAndDateCmd } from "../types";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { sendSuccessMessage } from "../../../pluginUtils";
|
||||
import { getGuildTz } from "../functions/getGuildTz";
|
||||
|
||||
export const ResetTimezoneCmd = timeAndDateCmd({
|
||||
trigger: "timezone reset",
|
||||
permission: "can_set_timezone",
|
||||
|
||||
signature: {},
|
||||
|
||||
async run({ pluginData, message }) {
|
||||
await pluginData.state.memberTimezones.reset(message.author.id);
|
||||
const serverTimezone = getGuildTz(pluginData);
|
||||
sendSuccessMessage(
|
||||
pluginData,
|
||||
message.channel,
|
||||
`Your timezone has been reset to server default, **${serverTimezone}**`,
|
||||
);
|
||||
},
|
||||
});
|
|
@ -1,17 +1,34 @@
|
|||
import { timeAndDateCmd } from "../types";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { sendSuccessMessage } from "../../../pluginUtils";
|
||||
import { sendErrorMessage, sendSuccessMessage } from "../../../pluginUtils";
|
||||
import { isValidTimezone } from "../../../utils/isValidTimezone";
|
||||
import { disableInlineCode, trimLines } from "../../../utils";
|
||||
import { parseFuzzyTimezone } from "../../../utils/parseFuzzyTimezone";
|
||||
|
||||
export const SetTimezoneCmd = timeAndDateCmd({
|
||||
trigger: "timezone",
|
||||
permission: "can_set_timezone",
|
||||
|
||||
signature: {
|
||||
timezone: ct.timezone(),
|
||||
timezone: ct.string(),
|
||||
},
|
||||
|
||||
async run({ pluginData, message, args }) {
|
||||
await pluginData.state.memberTimezones.set(message.author.id, args.timezone);
|
||||
sendSuccessMessage(pluginData, message.channel, `Your timezone is now set to **${args.timezone}**`);
|
||||
const parsedTz = parseFuzzyTimezone(args.timezone);
|
||||
if (!parsedTz) {
|
||||
sendErrorMessage(
|
||||
pluginData,
|
||||
message.channel,
|
||||
trimLines(`
|
||||
Invalid timezone: \`${disableInlineCode(args.timezone)}\`
|
||||
Zeppelin uses timezone locations rather than specific timezone names.
|
||||
See the **TZ database name** column at <https://en.wikipedia.org/wiki/List_of_tz_database_time_zones> for a list of valid options.
|
||||
`),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
await pluginData.state.memberTimezones.set(message.author.id, parsedTz);
|
||||
sendSuccessMessage(pluginData, message.channel, `Your timezone is now set to **${parsedTz}**`);
|
||||
},
|
||||
});
|
||||
|
|
|
@ -51,13 +51,13 @@ export async function getServerInfoEmbed(
|
|||
});
|
||||
|
||||
const basicInformation = [];
|
||||
basicInformation.push(`Created: **${serverAge} ago** (${prettyCreatedAt})`);
|
||||
basicInformation.push(`Created: **${serverAge} ago** (\`${prettyCreatedAt}\`)`);
|
||||
|
||||
if (thisServer) {
|
||||
const owner = await resolveUser(pluginData.client, thisServer.ownerID);
|
||||
const ownerName = `${owner.username}#${owner.discriminator}`;
|
||||
|
||||
basicInformation.push(`Owner: **${ownerName}** (${thisServer.ownerID})`);
|
||||
basicInformation.push(`Owner: **${ownerName}** (\`${thisServer.ownerID}\`)`);
|
||||
basicInformation.push(`Voice region: **${thisServer.region}**`);
|
||||
}
|
||||
|
||||
|
|
32
backend/src/utils/parseFuzzyTimezone.ts
Normal file
32
backend/src/utils/parseFuzzyTimezone.ts
Normal file
|
@ -0,0 +1,32 @@
|
|||
import moment from "moment-timezone";
|
||||
import escapeStringRegexp from "escape-string-regexp";
|
||||
|
||||
const normalizeTzName = str => str.replace(/[^a-zA-Z0-9+\-]/g, "").toLowerCase();
|
||||
|
||||
const validTimezones = moment.tz.names();
|
||||
const normalizedTimezoneMap = validTimezones.reduce((map, tz) => {
|
||||
map.set(normalizeTzName(tz), tz);
|
||||
return map;
|
||||
}, new Map());
|
||||
const normalizedTimezones = Array.from(normalizedTimezoneMap.keys());
|
||||
|
||||
export function parseFuzzyTimezone(input: string) {
|
||||
const normalizedInput = normalizeTzName(input);
|
||||
|
||||
if (normalizedTimezoneMap.has(normalizedInput)) {
|
||||
return normalizedTimezoneMap.get(normalizedInput);
|
||||
}
|
||||
|
||||
const searchRegex = new RegExp(`.*${escapeStringRegexp(normalizedInput)}.*`);
|
||||
for (const tz of normalizedTimezones) {
|
||||
if (searchRegex.test(tz)) {
|
||||
const result = normalizedTimezoneMap.get(tz);
|
||||
// Ignore Etc/GMT timezones unless explicitly specified, as they have confusing functionality
|
||||
// with the inverted +/- sign
|
||||
if (result.startsWith("Etc/GMT")) continue;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue