e n h a n c e /debug command

This commit is contained in:
janderedev 2022-04-26 14:34:33 +02:00
parent 129891e272
commit f2a25f8af4
Signed by: Lea
GPG key ID: 5D5E18ACB990F57A

View file

@ -1,17 +1,79 @@
import { decodeTime } from "ulid";
import CommandCategory from "../../struct/commands/CommandCategory"; import CommandCategory from "../../struct/commands/CommandCategory";
import SimpleCommand from "../../struct/commands/SimpleCommand"; import SimpleCommand from "../../struct/commands/SimpleCommand";
import MessageCommandContext from "../../struct/MessageCommandContext"; import MessageCommandContext from "../../struct/MessageCommandContext";
import { ULID_REGEX } from "../util";
export default { export default {
name: 'debug', name: 'debug',
aliases: null, aliases: null,
description: 'Gives info helpful for development and debugging', description: 'Gives info helpful for development and debugging',
syntax: '/debug [ULID|Discord ID|(empty)]',
category: CommandCategory.Misc, category: CommandCategory.Misc,
run: (message: MessageCommandContext, args: string[]) => { run: async (message: MessageCommandContext, args: string[]) => {
message.reply(`Server ID: ${message.channel?.server_id || 'None'}\n` if (ULID_REGEX.test(args[0])) {
+ `Server context: ${message.serverContext._id} ` const ts = decodeTime(args[0]);
+ `(${message.serverContext._id == message.channel?.server_id ? 'This server' : message.serverContext.name})\n` const tsSmall = Math.round(ts / 1000);
+ `Channel ID: ${message.channel_id}\n` await message.reply(
+ `User ID: ${message.author_id}`); `ULID: \`${args[0]}\`\n` +
`TS: \`${ts}\` (<t:${tsSmall}:F> / <t:${tsSmall}:R>)`,
false
);
} else if (validateSnowflake(args[0])) {
const date = convertSnowflakeToDate(args[0]);
const ts = date.getTime(),
tsSmall = Math.round(ts / 1000);
await message.reply(
`Discord Snowflake: \`${args[0]}\`\n` +
`TS: \`${ts}\` (<t:${tsSmall}:F> / <t:${tsSmall}:R>)`,
false
);
} else {
await message.reply(
`Server ID: ${message.channel?.server_id || 'None'}\n`
+ `Server context: ${message.serverContext._id} `
+ `(${message.serverContext._id == message.channel?.server_id ? 'This server' : message.serverContext.name})\n`
+ `Channel ID: ${message.channel_id}\n`
+ `User ID: ${message.author_id}`,
false
);
}
} }
} as SimpleCommand; } as SimpleCommand;
/* The below is yoinked from https://github.com/vegeta897/snow-stamp/blob/main/src/convert.js */
const DISCORD_EPOCH = 1420070400000;
// Converts a snowflake ID string into a JS Date object using the provided epoch (in ms), or Discord's epoch if not provided
function convertSnowflakeToDate(snowflake: string|bigint, epoch = DISCORD_EPOCH) {
// Convert snowflake to BigInt to extract timestamp bits
// https://discord.com/developers/docs/reference#snowflakes
const milliseconds = BigInt(snowflake) >> 22n;
return new Date(Number(milliseconds) + epoch);
}
// Validates a snowflake ID string and returns a JS Date object if valid
function validateSnowflake(snowflake: string, epoch?: number) {
if (isNaN(parseInt(snowflake))) return false;
if (parseInt(snowflake) < 4194304) {
//throw new Error(
// "That doesn't look like a snowflake. Snowflakes are much larger numbers."
//)
return false;
}
const timestamp = convertSnowflakeToDate(snowflake, epoch);
if (Number.isNaN(timestamp.getTime())) {
//throw new Error(
// "That doesn't look like a snowflake. Snowflakes have fewer digits."
//)
return false;
}
return timestamp;
}