PetPet: Fix Upload Image Option (#2461)
This commit is contained in:
parent
f4d6461690
commit
4da8b9aad7
8 changed files with 45 additions and 28 deletions
|
@ -24,22 +24,20 @@ import { closeAllModals } from "@utils/modal";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
import { maybePromptToUpdate } from "@utils/updater";
|
import { maybePromptToUpdate } from "@utils/updater";
|
||||||
import { filters, findBulk, proxyLazyWebpack } from "@webpack";
|
import { filters, findBulk, proxyLazyWebpack } from "@webpack";
|
||||||
import { FluxDispatcher, NavigationRouter, SelectedChannelStore } from "@webpack/common";
|
import { DraftType, FluxDispatcher, NavigationRouter, SelectedChannelStore } from "@webpack/common";
|
||||||
|
|
||||||
const CrashHandlerLogger = new Logger("CrashHandler");
|
const CrashHandlerLogger = new Logger("CrashHandler");
|
||||||
const { ModalStack, DraftManager, DraftType, closeExpressionPicker } = proxyLazyWebpack(() => {
|
|
||||||
const modules = findBulk(
|
const { ModalStack, DraftManager, closeExpressionPicker } = proxyLazyWebpack(() => {
|
||||||
|
const [ModalStack, DraftManager, ExpressionManager] = findBulk(
|
||||||
filters.byProps("pushLazy", "popAll"),
|
filters.byProps("pushLazy", "popAll"),
|
||||||
filters.byProps("clearDraft", "saveDraft"),
|
filters.byProps("clearDraft", "saveDraft"),
|
||||||
filters.byProps("DraftType"),
|
filters.byProps("closeExpressionPicker", "openExpressionPicker"),);
|
||||||
filters.byProps("closeExpressionPicker", "openExpressionPicker"),
|
|
||||||
);
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
ModalStack: modules[0],
|
ModalStack,
|
||||||
DraftManager: modules[1],
|
DraftManager,
|
||||||
DraftType: modules[2]?.DraftType,
|
closeExpressionPicker: ExpressionManager?.closeExpressionPicker,
|
||||||
closeExpressionPicker: modules[3]?.closeExpressionPicker,
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -137,8 +135,11 @@ export default definePlugin({
|
||||||
try {
|
try {
|
||||||
const channelId = SelectedChannelStore.getChannelId();
|
const channelId = SelectedChannelStore.getChannelId();
|
||||||
|
|
||||||
DraftManager.clearDraft(channelId, DraftType.ChannelMessage);
|
for (const key in DraftType) {
|
||||||
DraftManager.clearDraft(channelId, DraftType.FirstThreadMessage);
|
if (!Number.isNaN(Number(key))) continue;
|
||||||
|
|
||||||
|
DraftManager.clearDraft(channelId, DraftType[key]);
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
CrashHandlerLogger.debug("Failed to clear drafts.", err);
|
CrashHandlerLogger.debug("Failed to clear drafts.", err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,13 +24,12 @@ import { getCurrentGuild } from "@utils/discord";
|
||||||
import { Logger } from "@utils/Logger";
|
import { Logger } from "@utils/Logger";
|
||||||
import definePlugin, { OptionType } from "@utils/types";
|
import definePlugin, { OptionType } from "@utils/types";
|
||||||
import { findByPropsLazy, findStoreLazy, proxyLazyWebpack } from "@webpack";
|
import { findByPropsLazy, findStoreLazy, proxyLazyWebpack } from "@webpack";
|
||||||
import { Alerts, ChannelStore, EmojiStore, FluxDispatcher, Forms, IconUtils, lodash, Parser, PermissionsBits, PermissionStore, UploadHandler, UserSettingsActionCreators, UserStore } from "@webpack/common";
|
import { Alerts, ChannelStore, DraftType, EmojiStore, FluxDispatcher, Forms, IconUtils, lodash, Parser, PermissionsBits, PermissionStore, UploadHandler, UserSettingsActionCreators, UserStore } from "@webpack/common";
|
||||||
import type { CustomEmoji } from "@webpack/types";
|
import type { CustomEmoji } from "@webpack/types";
|
||||||
import type { Message } from "discord-types/general";
|
import type { Message } from "discord-types/general";
|
||||||
import { applyPalette, GIFEncoder, quantize } from "gifenc";
|
import { applyPalette, GIFEncoder, quantize } from "gifenc";
|
||||||
import type { ReactElement, ReactNode } from "react";
|
import type { ReactElement, ReactNode } from "react";
|
||||||
|
|
||||||
const DRAFT_TYPE = 0;
|
|
||||||
const StickerStore = findStoreLazy("StickersStore") as {
|
const StickerStore = findStoreLazy("StickersStore") as {
|
||||||
getPremiumPacks(): StickerPack[];
|
getPremiumPacks(): StickerPack[];
|
||||||
getAllGuildStickers(): Map<string, Sticker[]>;
|
getAllGuildStickers(): Map<string, Sticker[]>;
|
||||||
|
@ -807,7 +806,7 @@ export default definePlugin({
|
||||||
gif.finish();
|
gif.finish();
|
||||||
|
|
||||||
const file = new File([gif.bytesView()], `${stickerId}.gif`, { type: "image/gif" });
|
const file = new File([gif.bytesView()], `${stickerId}.gif`, { type: "image/gif" });
|
||||||
UploadHandler.promptToUpload([file], ChannelStore.getChannel(channelId), DRAFT_TYPE);
|
UploadHandler.promptToUpload([file], ChannelStore.getChannel(channelId), DraftType.ChannelMessage);
|
||||||
},
|
},
|
||||||
|
|
||||||
canUseEmote(e: CustomEmoji, channelId: string) {
|
canUseEmote(e: CustomEmoji, channelId: string) {
|
||||||
|
|
|
@ -21,10 +21,9 @@ import { Devs } from "@utils/constants";
|
||||||
import { makeLazy } from "@utils/lazy";
|
import { makeLazy } from "@utils/lazy";
|
||||||
import definePlugin from "@utils/types";
|
import definePlugin from "@utils/types";
|
||||||
import { findByPropsLazy } from "@webpack";
|
import { findByPropsLazy } from "@webpack";
|
||||||
import { UploadHandler, UserUtils } from "@webpack/common";
|
import { DraftType, UploadHandler, UploadManager, UserUtils } from "@webpack/common";
|
||||||
import { applyPalette, GIFEncoder, quantize } from "gifenc";
|
import { applyPalette, GIFEncoder, quantize } from "gifenc";
|
||||||
|
|
||||||
const DRAFT_TYPE = 0;
|
|
||||||
const DEFAULT_DELAY = 20;
|
const DEFAULT_DELAY = 20;
|
||||||
const DEFAULT_RESOLUTION = 128;
|
const DEFAULT_RESOLUTION = 128;
|
||||||
const FRAMES = 10;
|
const FRAMES = 10;
|
||||||
|
@ -59,9 +58,12 @@ async function resolveImage(options: Argument[], ctx: CommandContext, noServerPf
|
||||||
for (const opt of options) {
|
for (const opt of options) {
|
||||||
switch (opt.name) {
|
switch (opt.name) {
|
||||||
case "image":
|
case "image":
|
||||||
const upload = UploadStore.getUploads(ctx.channel.id, DRAFT_TYPE)[0];
|
const upload = UploadStore.getUpload(ctx.channel.id, opt.name, DraftType.SlashCommand);
|
||||||
if (upload) {
|
if (upload) {
|
||||||
if (!upload.isImage) throw "Upload is not an image";
|
if (!upload.isImage) {
|
||||||
|
UploadManager.clearAll(ctx.channel.id, DraftType.SlashCommand);
|
||||||
|
throw "Upload is not an image";
|
||||||
|
}
|
||||||
return upload.item.file;
|
return upload.item.file;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
@ -73,10 +75,12 @@ async function resolveImage(options: Argument[], ctx: CommandContext, noServerPf
|
||||||
return user.getAvatarURL(noServerPfp ? void 0 : ctx.guild?.id, 2048).replace(/\?size=\d+$/, "?size=2048");
|
return user.getAvatarURL(noServerPfp ? void 0 : ctx.guild?.id, 2048).replace(/\?size=\d+$/, "?size=2048");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[petpet] Failed to fetch user\n", err);
|
console.error("[petpet] Failed to fetch user\n", err);
|
||||||
|
UploadManager.clearAll(ctx.channel.id, DraftType.SlashCommand);
|
||||||
throw "Failed to fetch user. Check the console for more info.";
|
throw "Failed to fetch user. Check the console for more info.";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
UploadManager.clearAll(ctx.channel.id, DraftType.SlashCommand);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,6 +134,7 @@ export default definePlugin({
|
||||||
var url = await resolveImage(opts, cmdCtx, noServerPfp);
|
var url = await resolveImage(opts, cmdCtx, noServerPfp);
|
||||||
if (!url) throw "No Image specified!";
|
if (!url) throw "No Image specified!";
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
UploadManager.clearAll(cmdCtx.channel.id, DraftType.SlashCommand);
|
||||||
sendBotMessage(cmdCtx.channel.id, {
|
sendBotMessage(cmdCtx.channel.id, {
|
||||||
content: String(err),
|
content: String(err),
|
||||||
});
|
});
|
||||||
|
@ -147,6 +152,8 @@ export default definePlugin({
|
||||||
canvas.width = canvas.height = resolution;
|
canvas.width = canvas.height = resolution;
|
||||||
const ctx = canvas.getContext("2d")!;
|
const ctx = canvas.getContext("2d")!;
|
||||||
|
|
||||||
|
UploadManager.clearAll(cmdCtx.channel.id, DraftType.SlashCommand);
|
||||||
|
|
||||||
for (let i = 0; i < FRAMES; i++) {
|
for (let i = 0; i < FRAMES; i++) {
|
||||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
|
||||||
|
@ -174,7 +181,7 @@ export default definePlugin({
|
||||||
const file = new File([gif.bytesView()], "petpet.gif", { type: "image/gif" });
|
const file = new File([gif.bytesView()], "petpet.gif", { type: "image/gif" });
|
||||||
// Immediately after the command finishes, Discord clears all input, including pending attachments.
|
// Immediately after the command finishes, Discord clears all input, including pending attachments.
|
||||||
// Thus, setTimeout is needed to make this execute after Discord cleared the input
|
// Thus, setTimeout is needed to make this execute after Discord cleared the input
|
||||||
setTimeout(() => UploadHandler.promptToUpload([file], cmdCtx.channel, DRAFT_TYPE), 10);
|
setTimeout(() => UploadHandler.promptToUpload([file], cmdCtx.channel, DraftType.ChannelMessage), 10);
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|
|
@ -485,7 +485,11 @@ export const Devs = /* #__PURE__*/ Object.freeze({
|
||||||
xocherry: {
|
xocherry: {
|
||||||
name: "xocherry",
|
name: "xocherry",
|
||||||
id: 221288171013406720n
|
id: 221288171013406720n
|
||||||
}
|
},
|
||||||
|
ScattrdBlade: {
|
||||||
|
name: "ScattrdBlade",
|
||||||
|
id: 678007540608532491n
|
||||||
|
},
|
||||||
} satisfies Record<string, Dev>);
|
} satisfies Record<string, Dev>);
|
||||||
|
|
||||||
// iife so #__PURE__ works correctly
|
// iife so #__PURE__ works correctly
|
||||||
|
|
|
@ -27,12 +27,7 @@ export const Flux: t.Flux = findByPropsLazy("connectStores");
|
||||||
|
|
||||||
export type GenericStore = t.FluxStore & Record<string, any>;
|
export type GenericStore = t.FluxStore & Record<string, any>;
|
||||||
|
|
||||||
export enum DraftType {
|
export const { DraftType }: { DraftType: typeof t.DraftType; } = findByPropsLazy("DraftType");
|
||||||
ChannelMessage = 0,
|
|
||||||
ThreadSettings = 1,
|
|
||||||
FirstThreadMessage = 2,
|
|
||||||
ApplicationLauncherCommand = 3
|
|
||||||
}
|
|
||||||
|
|
||||||
export let MessageStore: Omit<Stores.MessageStore, "getMessages"> & {
|
export let MessageStore: Omit<Stores.MessageStore, "getMessages"> & {
|
||||||
getMessages(chanId: string): any;
|
getMessages(chanId: string): any;
|
||||||
|
|
9
src/webpack/common/types/stores.d.ts
vendored
9
src/webpack/common/types/stores.d.ts
vendored
|
@ -173,6 +173,15 @@ export class DraftStore extends FluxStore {
|
||||||
getThreadSettings(channelId: string): any | null;
|
getThreadSettings(channelId: string): any | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum DraftType {
|
||||||
|
ChannelMessage,
|
||||||
|
ThreadSettings,
|
||||||
|
FirstThreadMessage,
|
||||||
|
ApplicationLauncherCommand,
|
||||||
|
Poll,
|
||||||
|
SlashCommand,
|
||||||
|
}
|
||||||
|
|
||||||
export class GuildStore extends FluxStore {
|
export class GuildStore extends FluxStore {
|
||||||
getGuild(guildId: string): Guild;
|
getGuild(guildId: string): Guild;
|
||||||
getGuildCount(): number;
|
getGuildCount(): number;
|
||||||
|
|
|
@ -119,6 +119,8 @@ export function showToast(message: string, type = ToastType.MESSAGE) {
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UserUtils = findByPropsLazy("getUser", "fetchCurrentUser") as { getUser: (id: string) => Promise<User>; };
|
export const UserUtils = findByPropsLazy("getUser", "fetchCurrentUser") as { getUser: (id: string) => Promise<User>; };
|
||||||
|
|
||||||
|
export const UploadManager = findByPropsLazy("clearAll", "addFile");
|
||||||
export const UploadHandler = findByPropsLazy("showUploadFileSizeExceededError", "promptToUpload") as {
|
export const UploadHandler = findByPropsLazy("showUploadFileSizeExceededError", "promptToUpload") as {
|
||||||
promptToUpload: (files: File[], channel: Channel, draftType: Number) => void;
|
promptToUpload: (files: File[], channel: Channel, draftType: Number) => void;
|
||||||
};
|
};
|
||||||
|
|
|
@ -432,7 +432,7 @@ export async function extractAndLoadChunks(code: string[], matcher: RegExp = Def
|
||||||
}
|
}
|
||||||
|
|
||||||
const [, rawChunkIds, entryPointId] = match;
|
const [, rawChunkIds, entryPointId] = match;
|
||||||
if (Number.isNaN(entryPointId)) {
|
if (Number.isNaN(Number(entryPointId))) {
|
||||||
const err = new Error("extractAndLoadChunks: Matcher didn't return a capturing group with the chunk ids array, or the entry point id returned as the second group wasn't a number");
|
const err = new Error("extractAndLoadChunks: Matcher didn't return a capturing group with the chunk ids array, or the entry point id returned as the second group wasn't a number");
|
||||||
logger.warn(err, "Code:", code, "Matcher:", matcher);
|
logger.warn(err, "Code:", code, "Matcher:", matcher);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue