2022-10-21 19:17:06 -04:00
|
|
|
/*
|
|
|
|
* Vencord, a modification for Discord's desktop app
|
|
|
|
* Copyright (c) 2022 Vendicated and contributors
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
import { addPreEditListener, addPreSendListener, removePreEditListener, removePreSendListener } from "../api/MessageEvents";
|
2022-10-05 18:42:58 -04:00
|
|
|
import { Devs } from "../utils/constants";
|
2022-11-07 16:23:34 -05:00
|
|
|
import { ApngDisposeOp, getGifEncoder, importApngJs } from "../utils/dependencies";
|
2022-11-07 17:34:14 -05:00
|
|
|
import { lazyWebpack } from "../utils/misc";
|
2022-10-22 12:18:41 -04:00
|
|
|
import definePlugin, { OptionType } from "../utils/types";
|
2022-10-21 07:37:53 -04:00
|
|
|
import { Settings } from "../Vencord";
|
2022-11-07 16:23:34 -05:00
|
|
|
import { filters } from "../webpack";
|
|
|
|
import { ChannelStore, UserStore } from "../webpack/common";
|
|
|
|
|
|
|
|
const DRAFT_TYPE = 0;
|
|
|
|
const promptToUpload = lazyWebpack(filters.byCode("UPLOAD_FILE_LIMIT_ERROR"));
|
|
|
|
|
2022-11-12 10:25:28 -05:00
|
|
|
interface BaseSticker {
|
2022-11-07 16:23:34 -05:00
|
|
|
available: boolean;
|
|
|
|
description: string;
|
|
|
|
format_type: number;
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
tags: string;
|
|
|
|
type: number;
|
|
|
|
}
|
2022-11-12 10:25:28 -05:00
|
|
|
interface GuildSticker extends BaseSticker {
|
|
|
|
guild_id: string;
|
|
|
|
}
|
|
|
|
interface DiscordSticker extends BaseSticker {
|
|
|
|
pack_id: string;
|
|
|
|
}
|
|
|
|
type Sticker = GuildSticker | DiscordSticker;
|
2022-11-07 16:23:34 -05:00
|
|
|
|
|
|
|
interface StickerPack {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
|
|
|
sku_id: string;
|
|
|
|
description: string;
|
|
|
|
cover_sticker_id: string;
|
|
|
|
banner_asset_id: string;
|
|
|
|
stickers: Sticker[];
|
|
|
|
}
|
2022-08-31 14:53:36 -04:00
|
|
|
|
|
|
|
export default definePlugin({
|
2022-10-01 11:04:57 -04:00
|
|
|
name: "NitroBypass",
|
2022-11-11 17:50:09 -05:00
|
|
|
authors: [Devs.Arjix, Devs.D3SOX, Devs.Ven],
|
2022-11-07 16:23:34 -05:00
|
|
|
description: "Allows you to stream in nitro quality and send fake emojis/stickers.",
|
2022-08-31 14:58:21 -04:00
|
|
|
dependencies: ["MessageEventsAPI"],
|
2022-11-07 16:23:34 -05:00
|
|
|
|
2022-08-31 14:53:36 -04:00
|
|
|
patches: [
|
|
|
|
{
|
2022-10-05 18:42:58 -04:00
|
|
|
find: "canUseAnimatedEmojis:function",
|
2022-10-21 07:37:53 -04:00
|
|
|
predicate: () => Settings.plugins.NitroBypass.enableEmojiBypass === true,
|
2022-08-31 14:53:36 -04:00
|
|
|
replacement: [
|
|
|
|
"canUseAnimatedEmojis",
|
2022-10-21 07:37:53 -04:00
|
|
|
"canUseEmojisEverywhere"
|
|
|
|
].map(func => {
|
|
|
|
return {
|
|
|
|
match: new RegExp(`${func}:function\\(.+?}`),
|
2022-11-07 16:23:34 -05:00
|
|
|
replace: `${func}:function(e){return true;}`
|
2022-10-21 07:37:53 -04:00
|
|
|
};
|
|
|
|
})
|
|
|
|
},
|
2022-11-07 16:23:34 -05:00
|
|
|
{
|
|
|
|
find: "canUseAnimatedEmojis:function",
|
|
|
|
predicate: () => Settings.plugins.NitroBypass.enableStickerBypass === true,
|
|
|
|
replacement: {
|
|
|
|
match: /canUseStickersEverywhere:function\(.+?}/,
|
|
|
|
replace: "canUseStickersEverywhere:function(e){return true;}"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
find: "\"SENDABLE\"",
|
|
|
|
replacement: {
|
|
|
|
match: /(\w+)\.available\?/,
|
|
|
|
replace: "true?"
|
|
|
|
}
|
|
|
|
},
|
2022-10-21 07:37:53 -04:00
|
|
|
{
|
|
|
|
find: "canUseAnimatedEmojis:function",
|
|
|
|
predicate: () => Settings.plugins.NitroBypass.enableStreamQualityBypass === true,
|
|
|
|
replacement: [
|
2022-10-19 06:27:20 -04:00
|
|
|
"canUseHighVideoUploadQuality",
|
2022-10-06 10:33:30 -04:00
|
|
|
"canStreamHighQuality",
|
|
|
|
"canStreamMidQuality"
|
2022-08-31 14:53:36 -04:00
|
|
|
].map(func => {
|
|
|
|
return {
|
|
|
|
match: new RegExp(`${func}:function\\(.+?}`),
|
2022-11-07 16:23:34 -05:00
|
|
|
replace: `${func}:function(e){return true;}`
|
2022-08-31 14:55:58 -04:00
|
|
|
};
|
2022-08-31 14:53:36 -04:00
|
|
|
})
|
|
|
|
},
|
2022-10-01 11:04:57 -04:00
|
|
|
{
|
|
|
|
find: "STREAM_FPS_OPTION.format",
|
2022-10-21 07:37:53 -04:00
|
|
|
predicate: () => Settings.plugins.NitroBypass.enableStreamQualityBypass === true,
|
2022-10-01 11:04:57 -04:00
|
|
|
replacement: {
|
|
|
|
match: /(userPremiumType|guildPremiumTier):.{0,10}TIER_\d,?/g,
|
|
|
|
replace: ""
|
|
|
|
}
|
2022-11-07 16:23:34 -05:00
|
|
|
},
|
2022-08-31 14:53:36 -04:00
|
|
|
],
|
2022-11-07 16:23:34 -05:00
|
|
|
|
2022-10-21 07:37:53 -04:00
|
|
|
options: {
|
|
|
|
enableEmojiBypass: {
|
|
|
|
description: "Allow sending fake emojis",
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
default: true,
|
|
|
|
restartNeeded: true,
|
|
|
|
},
|
2022-11-07 16:23:34 -05:00
|
|
|
enableStickerBypass: {
|
|
|
|
description: "Allow sending fake stickers",
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
default: true,
|
|
|
|
restartNeeded: true,
|
|
|
|
},
|
|
|
|
stickerSize: {
|
|
|
|
description: "Size of the stickers when sending",
|
|
|
|
type: OptionType.SLIDER,
|
|
|
|
default: 160,
|
|
|
|
markers: [32, 64, 128, 160, 256, 512],
|
|
|
|
},
|
2022-10-21 07:37:53 -04:00
|
|
|
enableStreamQualityBypass: {
|
|
|
|
description: "Allow streaming in nitro quality",
|
|
|
|
type: OptionType.BOOLEAN,
|
|
|
|
default: true,
|
|
|
|
restartNeeded: true,
|
|
|
|
}
|
|
|
|
},
|
2022-08-31 16:08:05 -04:00
|
|
|
|
2022-10-01 11:04:57 -04:00
|
|
|
get guildId() {
|
|
|
|
return window.location.href.split("channels/")[1].split("/")[0];
|
|
|
|
},
|
|
|
|
|
|
|
|
get canUseEmotes() {
|
2022-11-08 11:51:09 -05:00
|
|
|
return (UserStore.getCurrentUser().premiumType ?? 0) > 0;
|
|
|
|
},
|
|
|
|
|
2022-11-12 10:25:28 -05:00
|
|
|
get canUseStickers() {
|
2022-11-08 11:51:09 -05:00
|
|
|
return (UserStore.getCurrentUser().premiumType ?? 0) > 1;
|
2022-10-01 11:04:57 -04:00
|
|
|
},
|
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
getStickerLink(stickerId: string) {
|
|
|
|
return `https://media.discordapp.net/stickers/${stickerId}.png?size=${Settings.plugins.NitroBypass.stickerSize}`;
|
|
|
|
},
|
|
|
|
|
|
|
|
async sendAnimatedSticker(stickerLink: string, stickerId: string, channelId: string) {
|
|
|
|
const [{ parseURL }, {
|
|
|
|
GIFEncoder,
|
|
|
|
quantize,
|
|
|
|
applyPalette
|
|
|
|
}] = await Promise.all([importApngJs(), getGifEncoder()]);
|
|
|
|
|
|
|
|
const { frames, width, height } = await parseURL(stickerLink);
|
|
|
|
|
|
|
|
const gif = new GIFEncoder();
|
|
|
|
const resolution = Settings.plugins.NitroBypass.stickerSize;
|
|
|
|
|
|
|
|
const canvas = document.createElement("canvas");
|
2022-11-09 11:30:37 -05:00
|
|
|
canvas.width = resolution;
|
|
|
|
canvas.height = resolution;
|
2022-11-07 16:23:34 -05:00
|
|
|
|
|
|
|
const ctx = canvas.getContext("2d", {
|
|
|
|
willReadFrequently: true
|
|
|
|
})!;
|
|
|
|
|
2022-11-09 11:30:37 -05:00
|
|
|
const scale = resolution / Math.max(width, height);
|
2022-11-07 16:23:34 -05:00
|
|
|
ctx.scale(scale, scale);
|
|
|
|
|
|
|
|
let lastImg: HTMLImageElement | null = null;
|
|
|
|
for (const { left, top, width, height, disposeOp, img, delay } of frames) {
|
|
|
|
ctx.drawImage(img, left, top, width, height);
|
|
|
|
|
|
|
|
const { data } = ctx.getImageData(0, 0, resolution, resolution);
|
|
|
|
|
|
|
|
const palette = quantize(data, 256);
|
|
|
|
const index = applyPalette(data, palette);
|
|
|
|
|
|
|
|
gif.writeFrame(index, resolution, resolution, {
|
|
|
|
transparent: true,
|
|
|
|
palette,
|
|
|
|
delay,
|
|
|
|
});
|
|
|
|
|
2022-11-09 14:29:35 -05:00
|
|
|
if (disposeOp === ApngDisposeOp.BACKGROUND) {
|
|
|
|
ctx.clearRect(left, top, width, height);
|
|
|
|
} else if (disposeOp === ApngDisposeOp.PREVIOUS && lastImg) {
|
2022-11-07 16:23:34 -05:00
|
|
|
ctx.drawImage(lastImg, left, top, width, height);
|
|
|
|
}
|
2022-11-09 14:29:35 -05:00
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
lastImg = img;
|
2022-10-21 07:37:53 -04:00
|
|
|
}
|
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
gif.finish();
|
|
|
|
const file = new File([gif.bytesView()], `${stickerId}.gif`, { type: "image/gif" });
|
|
|
|
promptToUpload([file], ChannelStore.getChannel(channelId), DRAFT_TYPE);
|
|
|
|
},
|
|
|
|
|
|
|
|
start() {
|
|
|
|
const settings = Settings.plugins.NitroBypass;
|
|
|
|
if (!settings.enableEmojiBypass && !settings.enableStickerBypass) {
|
2022-10-01 11:04:57 -04:00
|
|
|
return;
|
|
|
|
}
|
2022-08-31 14:53:36 -04:00
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
const EmojiStore = lazyWebpack(filters.byProps("getCustomEmojiById"));
|
|
|
|
const StickerStore = lazyWebpack(filters.byProps("getAllGuildStickers")) as {
|
|
|
|
getPremiumPacks(): StickerPack[];
|
|
|
|
getAllGuildStickers(): Map<string, Sticker[]>;
|
|
|
|
getStickerById(id: string): Sticker | undefined;
|
|
|
|
};
|
2022-08-31 14:53:36 -04:00
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
function getWordBoundary(origStr: string, offset: number) {
|
2022-10-02 16:12:48 -04:00
|
|
|
return (!origStr[offset] || /\s/.test(origStr[offset])) ? "" : " ";
|
|
|
|
}
|
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
this.preSend = addPreSendListener((channelId, messageObj, extra) => {
|
2022-10-08 14:36:57 -04:00
|
|
|
const { guildId } = this;
|
2022-10-01 11:04:57 -04:00
|
|
|
|
2022-11-12 10:25:28 -05:00
|
|
|
stickerBypass: {
|
|
|
|
if (!settings.enableStickerBypass)
|
|
|
|
break stickerBypass;
|
|
|
|
|
|
|
|
const sticker = StickerStore.getStickerById(extra?.stickerIds?.[0]!);
|
|
|
|
if (!sticker)
|
|
|
|
break stickerBypass;
|
|
|
|
|
|
|
|
if (sticker.available !== false && (this.canUseStickers || (sticker as GuildSticker)?.guild_id === guildId))
|
|
|
|
break stickerBypass;
|
|
|
|
|
|
|
|
let link = this.getStickerLink(sticker.id);
|
|
|
|
if (sticker.format_type === 2) {
|
|
|
|
this.sendAnimatedSticker(this.getStickerLink(sticker.id), sticker.id, channelId);
|
|
|
|
return { cancel: true };
|
|
|
|
} else {
|
|
|
|
if ("pack_id" in sticker) {
|
|
|
|
const packId = sticker.pack_id === "847199849233514549"
|
|
|
|
// Discord moved these stickers into a different pack at some point, but
|
|
|
|
// Distok still uses the old id
|
|
|
|
? "749043879713701898"
|
|
|
|
: sticker.pack_id;
|
|
|
|
|
|
|
|
link = `https://distok.top/stickers/${packId}/${sticker.id}.gif`;
|
2022-11-07 16:23:34 -05:00
|
|
|
}
|
|
|
|
|
2022-11-12 10:25:28 -05:00
|
|
|
delete extra.stickerIds;
|
|
|
|
messageObj.content += " " + link;
|
2022-11-07 16:23:34 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.canUseEmotes && settings.enableEmojiBypass) {
|
|
|
|
for (const emoji of messageObj.validNonShortcutEmojis) {
|
|
|
|
if (!emoji.require_colons) continue;
|
|
|
|
if (emoji.guildId === guildId && !emoji.animated) continue;
|
2022-08-31 14:53:36 -04:00
|
|
|
|
2022-11-07 16:23:34 -05:00
|
|
|
const emojiString = `<${emoji.animated ? "a" : ""}:${emoji.originalName || emoji.name}:${emoji.id}>`;
|
|
|
|
const url = emoji.url.replace(/\?size=\d+/, "?size=48");
|
|
|
|
messageObj.content = messageObj.content.replace(emojiString, (match, offset, origStr) => {
|
|
|
|
return `${getWordBoundary(origStr, offset - 1)}${url}${getWordBoundary(origStr, offset + match.length)}`;
|
|
|
|
});
|
|
|
|
}
|
2022-08-31 14:53:36 -04:00
|
|
|
}
|
2022-11-07 16:23:34 -05:00
|
|
|
|
|
|
|
return { cancel: false };
|
2022-08-31 14:55:58 -04:00
|
|
|
});
|
2022-11-07 16:23:34 -05:00
|
|
|
|
|
|
|
if (!this.canUseEmotes && settings.enableEmojiBypass) {
|
|
|
|
this.preEdit = addPreEditListener((_, __, messageObj) => {
|
|
|
|
const { guildId } = this;
|
|
|
|
|
|
|
|
for (const [emojiStr, _, emojiId] of messageObj.content.matchAll(/(?<!\\)<a?:(\w+):(\d+)>/ig)) {
|
|
|
|
const emoji = EmojiStore.getCustomEmojiById(emojiId);
|
|
|
|
if (emoji == null || (emoji.guildId === guildId && !emoji.animated)) continue;
|
|
|
|
if (!emoji.require_colons) continue;
|
|
|
|
|
|
|
|
const url = emoji.url.replace(/\?size=\d+/, "?size=48");
|
|
|
|
messageObj.content = messageObj.content.replace(emojiStr, (match, offset, origStr) => {
|
|
|
|
return `${getWordBoundary(origStr, offset - 1)}${url}${getWordBoundary(origStr, offset + match.length)}`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-08-31 14:53:36 -04:00
|
|
|
},
|
2022-08-31 16:08:05 -04:00
|
|
|
|
|
|
|
stop() {
|
|
|
|
removePreSendListener(this.preSend);
|
|
|
|
removePreEditListener(this.preEdit);
|
|
|
|
}
|
2022-08-31 14:55:58 -04:00
|
|
|
});
|