mirror of
https://github.com/ZeppelinBot/Zeppelin.git
synced 2025-05-10 12:25:02 +00:00
Add new multi-use !info command
This commit is contained in:
parent
7ad2458fd9
commit
106a959b4d
7 changed files with 184 additions and 61 deletions
58
backend/src/utils/resolveMessageTarget.ts
Normal file
58
backend/src/utils/resolveMessageTarget.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
import { disableInlineCode, isSnowflake } from "../utils";
|
||||
import { getChannelIdFromMessageId } from "../data/getChannelIdFromMessageId";
|
||||
import { PluginData, TypeConversionError } from "knub";
|
||||
import { TextChannel } from "eris";
|
||||
|
||||
const channelAndMessageIdRegex = /^(\d+)[\-\/](\d+)$/;
|
||||
const messageLinkRegex = /^https:\/\/(?:\w+\.)?discord(?:app)?\.com\/channels\/\d+\/(\d+)\/(\d+)$/i;
|
||||
|
||||
export interface MessageTarget {
|
||||
channel: TextChannel;
|
||||
messageId: string;
|
||||
}
|
||||
|
||||
export async function resolveMessageTarget(pluginData: PluginData<any>, value: string) {
|
||||
const result = await (async () => {
|
||||
if (isSnowflake(value)) {
|
||||
const channelId = await getChannelIdFromMessageId(value);
|
||||
if (!channelId) {
|
||||
return null;
|
||||
}
|
||||
|
||||
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],
|
||||
};
|
||||
}
|
||||
})();
|
||||
|
||||
if (!result) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const channel = pluginData.guild.channels.get(result.channelId);
|
||||
if (!channel || !(channel instanceof TextChannel)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return {
|
||||
channel,
|
||||
messageId: result.messageId,
|
||||
};
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue