Add time_and_date plugin. Use it for timezones and date formats around the bot.

This commit is contained in:
Dragory 2020-08-19 00:19:12 +03:00
parent cffb0dbd6b
commit 4ae8cf85a3
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
67 changed files with 543 additions and 177 deletions

View file

@ -33,6 +33,7 @@ import { MessageInfoCmd } from "./commands/MessageInfoCmd";
import { InfoCmd } from "./commands/InfoCmd";
import { SnowflakeInfoCmd } from "./commands/SnowflakeInfoCmd";
import { discardRegExpRunner, getRegExpRunner } from "../../regExpRunners";
import { TimeAndDatePlugin } from "../TimeAndDate/TimeAndDatePlugin";
const defaultOptions: PluginOptions<UtilityPluginType> = {
config: {
@ -101,6 +102,7 @@ export const UtilityPlugin = zeppelinPlugin<UtilityPluginType>()("utility", {
prettyName: "Utility",
},
dependencies: [TimeAndDatePlugin],
configSchema: ConfigSchema,
defaultOptions,

View file

@ -6,9 +6,8 @@ import humanizeDuration from "humanize-duration";
import LCL from "last-commit-log";
import path from "path";
import moment from "moment-timezone";
import { getGuildTz, inGuildTz } from "../../../utils/timezones";
import { rootDir } from "../../../paths";
import { getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
export const AboutCmd = utilityCmd({
trigger: "about",
@ -16,6 +15,8 @@ export const AboutCmd = utilityCmd({
permission: "can_about",
async run({ message: msg, pluginData }) {
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const uptime = getCurrentUptime();
const prettyUptime = humanizeDuration(uptime, { largest: 2, round: true });
@ -30,9 +31,9 @@ export const AboutCmd = utilityCmd({
let version;
if (lastCommit) {
lastUpdate = inGuildTz(pluginData, moment.utc(lastCommit.committer.date, "X")).format(
getDateFormat(pluginData, "pretty_datetime"),
);
lastUpdate = timeAndDate
.inGuildTz(moment.utc(lastCommit.committer.date, "X"))
.format(pluginData.getPlugin(TimeAndDatePlugin).getDateFormat("pretty_datetime"));
version = lastCommit.shortHash;
} else {
lastUpdate = "?";
@ -52,7 +53,7 @@ export const AboutCmd = utilityCmd({
["Last update", lastUpdate],
["Version", version],
["API latency", `${shard.latency}ms`],
["Server timezone", getGuildTz(pluginData)],
["Server timezone", timeAndDate.getGuildTz()],
];
const loadedPlugins = Array.from(

View file

@ -32,7 +32,7 @@ export const InfoCmd = utilityCmd({
const channelId = getChannelId(value);
const channel = channelId && pluginData.guild.channels.get(channelId);
if (channel) {
const embed = await getChannelInfoEmbed(pluginData, channelId);
const embed = await getChannelInfoEmbed(pluginData, channelId, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
@ -42,7 +42,7 @@ export const InfoCmd = utilityCmd({
// 2. Server
const guild = pluginData.client.guilds.get(value);
if (guild) {
const embed = await getServerInfoEmbed(pluginData, value);
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
@ -52,7 +52,7 @@ export const InfoCmd = utilityCmd({
// 3. User
const user = await resolveUser(pluginData.client, value);
if (user) {
const embed = await getUserInfoEmbed(pluginData, user.id, Boolean(args.compact));
const embed = await getUserInfoEmbed(pluginData, user.id, Boolean(args.compact), message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
@ -63,7 +63,12 @@ export const InfoCmd = utilityCmd({
const messageTarget = await resolveMessageTarget(pluginData, value);
if (messageTarget) {
if (canReadChannel(messageTarget.channel, message.member)) {
const embed = await getMessageInfoEmbed(pluginData, messageTarget.channel.id, messageTarget.messageId);
const embed = await getMessageInfoEmbed(
pluginData,
messageTarget.channel.id,
messageTarget.messageId,
message.author.id,
);
if (embed) {
message.channel.createMessage({ embed });
return;
@ -87,7 +92,7 @@ export const InfoCmd = utilityCmd({
// 6. Server again (fallback for discovery servers)
const serverPreview = getGuildPreview(pluginData.client, value).catch(() => null);
if (serverPreview) {
const embed = await getServerInfoEmbed(pluginData, value);
const embed = await getServerInfoEmbed(pluginData, value, message.author.id);
if (embed) {
message.channel.createMessage({ embed });
return;
@ -96,7 +101,7 @@ export const InfoCmd = utilityCmd({
// 7. Arbitrary ID
if (isValidSnowflake(value)) {
const embed = getSnowflakeInfoEmbed(pluginData, value, true);
const embed = await getSnowflakeInfoEmbed(pluginData, value, true, message.author.id);
message.channel.createMessage({ embed });
return;
}

View file

@ -20,7 +20,12 @@ export const MessageInfoCmd = utilityCmd({
return;
}
const embed = await getMessageInfoEmbed(pluginData, args.message.channel.id, args.message.messageId);
const embed = await getMessageInfoEmbed(
pluginData,
args.message.channel.id,
args.message.messageId,
message.author.id,
);
if (!embed) {
sendErrorMessage(pluginData, message.channel, "Unknown message");
return;

View file

@ -15,7 +15,7 @@ export const ServerCmd = utilityCmd({
async run({ message, pluginData, args }) {
const serverId = args.serverId || pluginData.guild.id;
const serverInfoEmbed = await getServerInfoEmbed(pluginData, serverId);
const serverInfoEmbed = await getServerInfoEmbed(pluginData, serverId, message.author.id);
if (!serverInfoEmbed) {
sendErrorMessage(pluginData, message.channel, "Could not find information for that server");
return;

View file

@ -14,8 +14,8 @@ export const SnowflakeInfoCmd = utilityCmd({
id: ct.anyId(),
},
run({ message, args, pluginData }) {
const embed = getSnowflakeInfoEmbed(pluginData, args.id);
async run({ message, args, pluginData }) {
const embed = await getSnowflakeInfoEmbed(pluginData, args.id, false, message.author.id);
message.channel.createMessage({ embed });
},
});

View file

@ -17,7 +17,7 @@ export const UserInfoCmd = utilityCmd({
async run({ message, args, pluginData }) {
const userId = args.user?.id || message.author.id;
const embed = await getUserInfoEmbed(pluginData, userId, args.compact);
const embed = await getUserInfoEmbed(pluginData, userId, args.compact, message.author.id);
if (!embed) {
sendErrorMessage(pluginData, message.channel, "User not found");
return;

View file

@ -4,8 +4,7 @@ import { Constants, EmbedOptions } from "eris";
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { formatNumber, preEmbedPadding, trimLines } from "../../../utils";
import { inGuildTz } from "../../../utils/timezones";
import { getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
const TEXT_CHANNEL_ICON =
"https://cdn.discordapp.com/attachments/740650744830623756/740656843545772062/text-channel.png";
@ -17,6 +16,7 @@ const ANNOUNCEMENT_CHANNEL_ICON =
export async function getChannelInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
channelId: string,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
const channel = pluginData.guild.channels.get(channelId);
if (!channel) {
@ -58,7 +58,11 @@ export async function getChannelInfoEmbed(
}
const createdAt = moment.utc(channel.createdAt, "x");
const prettyCreatedAt = inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"));
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const channelAge = humanizeDuration(Date.now() - channel.createdAt, {
largest: 2,
round: true,

View file

@ -5,8 +5,7 @@ import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { chunkMessageLines, messageLink, preEmbedPadding, trimEmptyLines, trimLines } from "../../../utils";
import { getDefaultPrefix } from "knub/dist/commands/commandUtils";
import { inGuildTz } from "../../../utils/timezones";
import { getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
const MESSAGE_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/740685652152025088/message.png";
@ -14,12 +13,15 @@ export async function getMessageInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
channelId: string,
messageId: string,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
const message = await pluginData.client.getMessage(channelId, messageId).catch(() => null);
if (!message) {
return null;
}
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const embed: EmbedOptions = {
fields: [],
};
@ -30,14 +32,20 @@ export async function getMessageInfoEmbed(
};
const createdAt = moment.utc(message.createdAt, "x");
const prettyCreatedAt = inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const messageAge = humanizeDuration(Date.now() - message.createdAt, {
largest: 2,
round: true,
});
const editedAt = message.editedTimestamp && moment.utc(message.editedTimestamp, "x");
const prettyEditedAt = inGuildTz(pluginData, editedAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzEditedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, editedAt)
: timeAndDate.inGuildTz(editedAt);
const prettyEditedAt = tzEditedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const editAge =
message.editedTimestamp &&
humanizeDuration(Date.now() - message.editedTimestamp, {
@ -75,18 +83,20 @@ export async function getMessageInfoEmbed(
});
const authorCreatedAt = moment.utc(message.author.createdAt, "x");
const prettyAuthorCreatedAt = inGuildTz(pluginData, authorCreatedAt).format(
getDateFormat(pluginData, "pretty_datetime"),
);
const tzAuthorCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, authorCreatedAt)
: timeAndDate.inGuildTz(authorCreatedAt);
const prettyAuthorCreatedAt = tzAuthorCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const authorAccountAge = humanizeDuration(Date.now() - message.author.createdAt, {
largest: 2,
round: true,
});
const authorJoinedAt = message.member && moment.utc(message.member.joinedAt, "x");
const prettyAuthorJoinedAt = inGuildTz(pluginData, authorJoinedAt).format(
getDateFormat(pluginData, "pretty_datetime"),
);
const tzAuthorJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, authorJoinedAt)
: timeAndDate.inGuildTz(authorJoinedAt);
const prettyAuthorJoinedAt = tzAuthorJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const authorServerAge =
message.member &&
humanizeDuration(Date.now() - message.member.joinedAt, {

View file

@ -5,12 +5,12 @@ import { CategoryChannel, EmbedOptions, Guild, RESTChannelInvite, TextChannel, V
import moment from "moment-timezone";
import humanizeDuration from "humanize-duration";
import { getGuildPreview } from "./getGuildPreview";
import { inGuildTz } from "../../../utils/timezones";
import { getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
export async function getServerInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
serverId: string,
requestMemberId?: string,
): Promise<EmbedOptions> {
const thisServer = serverId === pluginData.guild.id ? pluginData.guild : null;
const [restGuild, guildPreview] = await Promise.all([
@ -39,8 +39,12 @@ export async function getServerInfoEmbed(
};
// BASIC INFORMATION
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAt = moment.utc((guildPreview || restGuild).createdAt, "x");
const prettyCreatedAt = inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const serverAge = humanizeDuration(moment.utc().valueOf() - createdAt.valueOf(), {
largest: 2,
round: true,

View file

@ -6,16 +6,16 @@ import moment from "moment-timezone";
import { CaseTypes } from "src/data/CaseTypes";
import humanizeDuration from "humanize-duration";
import { snowflakeToTimestamp } from "../../../utils/snowflakeToTimestamp";
import { inGuildTz } from "../../../utils/timezones";
import { getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
const SNOWFLAKE_ICON = "https://cdn.discordapp.com/attachments/740650744830623756/742020790471491668/snowflake.png";
export function getSnowflakeInfoEmbed(
export async function getSnowflakeInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
snowflake: string,
showUnknownWarning = false,
): EmbedOptions {
requestMemberId?: string,
): Promise<EmbedOptions> {
const embed: EmbedOptions = {
fields: [],
};
@ -30,9 +30,13 @@ export function getSnowflakeInfoEmbed(
"This is a valid [snowflake ID](https://discord.com/developers/docs/reference#snowflakes), but I don't know what it's for.";
}
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
const createdAtMS = snowflakeToTimestamp(snowflake);
const createdAt = moment.utc(createdAtMS, "x");
const prettyCreatedAt = inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const snowflakeAge = humanizeDuration(Date.now() - createdAtMS, {
largest: 2,
round: true,

View file

@ -14,13 +14,13 @@ import {
import moment from "moment-timezone";
import { CaseTypes } from "src/data/CaseTypes";
import humanizeDuration from "humanize-duration";
import { inGuildTz } from "../../../utils/timezones";
import { getDateFormat } from "../../../utils/dateFormats";
import { TimeAndDatePlugin } from "../../TimeAndDate/TimeAndDatePlugin";
export async function getUserInfoEmbed(
pluginData: PluginData<UtilityPluginType>,
userId: string,
compact = false,
requestMemberId?: string,
): Promise<EmbedOptions | null> {
const user = await resolveUser(pluginData.client, userId);
if (!user || user instanceof UnknownUser) {
@ -33,6 +33,8 @@ export async function getUserInfoEmbed(
fields: [],
};
const timeAndDate = pluginData.getPlugin(TimeAndDatePlugin);
embed.author = {
name: `User: ${user.username}#${user.discriminator}`,
};
@ -41,7 +43,10 @@ export async function getUserInfoEmbed(
embed.author.icon_url = avatarURL;
const createdAt = moment.utc(user.createdAt, "x");
const prettyCreatedAt = inGuildTz(pluginData, createdAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzCreatedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, createdAt)
: timeAndDate.inGuildTz(createdAt);
const prettyCreatedAt = tzCreatedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const accountAge = humanizeDuration(moment.utc().valueOf() - user.createdAt, {
largest: 2,
round: true,
@ -57,7 +62,10 @@ export async function getUserInfoEmbed(
});
if (member) {
const joinedAt = moment.utc(member.joinedAt, "x");
const prettyJoinedAt = inGuildTz(pluginData, joinedAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
: timeAndDate.inGuildTz(joinedAt);
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedAt, {
largest: 2,
round: true,
@ -85,7 +93,10 @@ export async function getUserInfoEmbed(
if (member) {
const joinedAt = moment.utc(member.joinedAt, "x");
const prettyJoinedAt = inGuildTz(pluginData, joinedAt).format(getDateFormat(pluginData, "pretty_datetime"));
const tzJoinedAt = requestMemberId
? await timeAndDate.inMemberTz(requestMemberId, joinedAt)
: timeAndDate.inGuildTz(joinedAt);
const prettyJoinedAt = tzJoinedAt.format(timeAndDate.getDateFormat("pretty_datetime"));
const joinAge = humanizeDuration(moment.utc().valueOf() - member.joinedAt, {
largest: 2,
round: true,