!source: don't show source of messages you don't have access to; allow mods to use the command by default

This commit is contained in:
Dragory 2020-08-05 23:57:09 +03:00
parent 14af94e7a3
commit 60aff76ebe
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
6 changed files with 137 additions and 12 deletions

View file

@ -1,7 +1,24 @@
import { convertDelayStringToMS, disableCodeBlocks, resolveMember, resolveUser, UnknownUser } from "./utils";
import { GuildChannel, Member, User } from "eris";
import {
convertDelayStringToMS,
disableCodeBlocks,
disableInlineCode,
isSnowflake,
resolveMember,
resolveUser,
UnknownUser,
} from "./utils";
import { GuildChannel, Member, TextChannel, User } from "eris";
import { baseTypeConverters, baseTypeHelpers, CommandContext, TypeConversionError } from "knub";
import { createTypeHelper } from "knub-command-manager";
import { getChannelIdFromMessageId } from "./data/getChannelIdFromMessageId";
export interface MessageTarget {
channel: TextChannel;
messageId: string;
}
const channelAndMessageIdRegex = /^(\d+)[\-\/](\d+)$/;
const messageLinkRegex = /^https:\/\/(?:\w+\.)?discord(?:app)?\.com\/channels\/\d+\/(\d+)\/(\d+)$/i;
export const commandTypes = {
...baseTypeConverters,
@ -42,6 +59,52 @@ export const commandTypes = {
}
return result;
},
async messageTarget(value: string, context: CommandContext<any>) {
value = String(value).trim();
const result = await (async () => {
if (isSnowflake(value)) {
const channelId = await getChannelIdFromMessageId(value);
if (!channelId) {
throw new TypeConversionError(`Could not find channel for message ID \`${disableInlineCode(value)}\``);
}
return {
channelId,
messageId: value,
};
}
const channelAndMessageIdMatch = value.match(channelAndMessageIdRegex);
if (channelAndMessageIdMatch) {
return {
channelId: channelAndMessageIdMatch[1],
messageId: channelAndMessageIdMatch[2],
};
}
const messageLinkMatch = value.match(messageLinkRegex);
if (messageLinkMatch) {
return {
channelId: messageLinkMatch[1],
messageId: messageLinkMatch[2],
};
}
throw new TypeConversionError(`Invalid message ID \`${disableInlineCode(value)}\``);
})();
const channel = context.pluginData.guild.channels.get(result.channelId);
if (!channel || !(channel instanceof TextChannel)) {
throw new TypeConversionError(`Invalid channel ID \`${disableInlineCode(result.channelId)}\``);
}
return {
channel,
messageId: result.messageId,
};
},
};
export const commandTypeHelpers = {
@ -51,4 +114,5 @@ export const commandTypeHelpers = {
resolvedUser: createTypeHelper<Promise<User>>(commandTypes.resolvedUser),
resolvedUserLoose: createTypeHelper<Promise<User | UnknownUser>>(commandTypes.resolvedUserLoose),
resolvedMember: createTypeHelper<Promise<Member | null>>(commandTypes.resolvedMember),
messageTarget: createTypeHelper<Promise<MessageTarget>>(commandTypes.messageTarget),
};