mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-03-15 05:41:51 +00:00
!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:
parent
14af94e7a3
commit
60aff76ebe
6 changed files with 137 additions and 12 deletions
|
@ -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),
|
||||
};
|
||||
|
|
17
backend/src/data/getChannelIdFromMessageId.ts
Normal file
17
backend/src/data/getChannelIdFromMessageId.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { SavedMessage } from "./entities/SavedMessage";
|
||||
import { Repository, getRepository } from "typeorm";
|
||||
|
||||
let repository: Repository<SavedMessage>;
|
||||
|
||||
export async function getChannelIdFromMessageId(messageId: string): Promise<string | null> {
|
||||
if (!repository) {
|
||||
repository = getRepository(SavedMessage);
|
||||
}
|
||||
|
||||
const savedMessage = await repository.findOne(messageId);
|
||||
if (savedMessage) {
|
||||
return savedMessage.channel_id;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
|
@ -71,6 +71,7 @@ const defaultOptions: PluginOptions<UtilityPluginType> = {
|
|||
can_context: true,
|
||||
can_jumbo: true,
|
||||
can_avatar: true,
|
||||
can_source: true,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -78,7 +79,6 @@ const defaultOptions: PluginOptions<UtilityPluginType> = {
|
|||
config: {
|
||||
can_reload_guild: true,
|
||||
can_ping: true,
|
||||
can_source: true,
|
||||
can_about: true,
|
||||
},
|
||||
},
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
import { utilityCmd } from "../types";
|
||||
import { commandTypeHelpers as ct } from "../../../commandTypes";
|
||||
import { errorMessage } from "../../../utils";
|
||||
import { getBaseUrl } from "../../../pluginUtils";
|
||||
import { getBaseUrl, sendErrorMessage } from "../../../pluginUtils";
|
||||
import moment from "moment-timezone";
|
||||
import { Constants, TextChannel } from "eris";
|
||||
import { hasPermissions } from "../../../utils/hasPermissions";
|
||||
import { canReadChannel } from "../../../utils/canReadChannel";
|
||||
|
||||
export const SourceCmd = utilityCmd({
|
||||
trigger: "source",
|
||||
|
@ -11,22 +14,36 @@ export const SourceCmd = utilityCmd({
|
|||
permission: "can_source",
|
||||
|
||||
signature: {
|
||||
messageId: ct.string(),
|
||||
message: ct.messageTarget(),
|
||||
},
|
||||
|
||||
async run({ message: msg, args, pluginData }) {
|
||||
const savedMessage = await pluginData.state.savedMessages.find(args.messageId);
|
||||
if (!savedMessage) {
|
||||
msg.channel.createMessage(errorMessage("Unknown message"));
|
||||
async run({ message: cmdMessage, args, pluginData }) {
|
||||
if (!canReadChannel(args.message.channel, cmdMessage.member.id)) {
|
||||
sendErrorMessage(pluginData, cmdMessage.channel, "Unknown message");
|
||||
return;
|
||||
}
|
||||
|
||||
const source =
|
||||
(savedMessage.data.content || "<no text content>") + "\n\nSource:\n\n" + JSON.stringify(savedMessage.data);
|
||||
const message = await pluginData.client
|
||||
.getMessage(args.message.channel.id, args.message.messageId)
|
||||
.catch(() => null);
|
||||
if (!message) {
|
||||
sendErrorMessage(pluginData, cmdMessage.channel, "Unknown message");
|
||||
return;
|
||||
}
|
||||
|
||||
const textSource = message.content || "<no text content>";
|
||||
const fullSource = JSON.stringify({
|
||||
id: message.id,
|
||||
content: message.content,
|
||||
attachments: message.attachments,
|
||||
embeds: message.embeds,
|
||||
});
|
||||
|
||||
const source = `${textSource}\n\nSource:\n\n${fullSource}`;
|
||||
|
||||
const archiveId = await pluginData.state.archives.create(source, moment().add(1, "hour"));
|
||||
const baseUrl = getBaseUrl(pluginData);
|
||||
const url = pluginData.state.archives.getUrl(baseUrl, archiveId);
|
||||
msg.channel.createMessage(`Message source: ${url}`);
|
||||
cmdMessage.channel.createMessage(`Message source: ${url}`);
|
||||
},
|
||||
});
|
||||
|
|
10
backend/src/utils/canReadChannel.ts
Normal file
10
backend/src/utils/canReadChannel.ts
Normal file
|
@ -0,0 +1,10 @@
|
|||
import { Constants, GuildChannel } from "eris";
|
||||
import { hasPermissions } from "./hasPermissions";
|
||||
|
||||
export function canReadChannel(channel: GuildChannel, memberId: string) {
|
||||
const channelPermissions = channel.permissionsOf(memberId);
|
||||
return hasPermissions(channelPermissions, [
|
||||
Constants.Permissions.readMessages,
|
||||
Constants.Permissions.readMessageHistory,
|
||||
]);
|
||||
}
|
17
backend/src/utils/hasPermissions.ts
Normal file
17
backend/src/utils/hasPermissions.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import { Constants, Permission } from "eris";
|
||||
|
||||
export function hasPermissions(channelPermissions: Permission, permissions: number | number[]) {
|
||||
if (Boolean(channelPermissions.allow & Constants.Permissions.administrator)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!Array.isArray(permissions)) {
|
||||
permissions = [permissions];
|
||||
}
|
||||
|
||||
for (const permission of permissions) {
|
||||
if (!(channelPermissions.allow & permission)) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
Loading…
Add table
Reference in a new issue