未定义的文档'画布/不和谐

发布于 2025-01-27 22:03:05 字数 2075 浏览 2 评论 0原文

我正在尝试将动画横幅放到Discord Server上,但是我有一些问题。我不知道为什么未定义文档。我尝试了很多方法,但它们都没有起作用。

try {
        let guild = await client.guilds.cache.get(guildId.id);

        const voiceChannels = guild.channels.cache.filter((c) => c.type === "GUILD_VOICE");
        let count = 0;
        for (const [, voiceChannel] of voiceChannels) count += voiceChannel.members.size;
        var canvas = createCanvas(420, 236);
        var ctx = canvas.getContext('2d');
        ctx.textAlign = "center"
        ctx.font = "20px Kanit Black"
        ctx.fillStyle = '000001'
        const encoder = new GIFEncoder(420, 236, {emptyColor: true, dynamicColors: true})
            encoder.setRepeat(0);
            encoder.setQuality(10)
        await gifFrames({ url: `${__dirname}/banner-beta.gif`, frames: 'all', outputType: 'canvas', cumulative: false}).then((frameData) => {
            for(let i = 0; i < frameData.length; i++) {
                encoder.setDelay(frameData[i].frameInfo.delay * 15);
                console.log(frameData[0])
                console.log(frameData[i])
                ctx.drawImage((frameData[i]).getImage(), 0, 0, 420, 236)
                ctx.fillText(`${count}`, 50, 90)
                console.log(ctx)
                encoder.addFrame(ctx);
            }
            encoder.finish();
            console.log(encoder.out.getData())
            var attachment = new MessageAttachment(encoder.out.getData(), 'banner.gif');
            guild.channels.cache.get("909860310171648050").send({ files: [attachment] }).catch(console.log);

        })
        //guild.setBanner(canvas.toBuffer()).catch(console.error);
    } catch (e) {
        console.log(e)
    }

错误:

ReferenceError: document is not defined
    at savePixels (C:\Users\hotab\Desktop\Root\node_modules\save-pixels-jpeg-js-upgrade\save-pixels.js:127:20)
    at Object.getImage (C:\Users\hotab\Desktop\Root\node_modules\gif-frames\gif-frames.js:105:20)
    at C:\Users\hotab\Desktop\Root\utils\banner.js:31:34

I'm trying make a animated banner to discord server, but I have some problems with that. I don't know why document is not defined. I tried a bunch of ways, but none of them worked.

try {
        let guild = await client.guilds.cache.get(guildId.id);

        const voiceChannels = guild.channels.cache.filter((c) => c.type === "GUILD_VOICE");
        let count = 0;
        for (const [, voiceChannel] of voiceChannels) count += voiceChannel.members.size;
        var canvas = createCanvas(420, 236);
        var ctx = canvas.getContext('2d');
        ctx.textAlign = "center"
        ctx.font = "20px Kanit Black"
        ctx.fillStyle = '000001'
        const encoder = new GIFEncoder(420, 236, {emptyColor: true, dynamicColors: true})
            encoder.setRepeat(0);
            encoder.setQuality(10)
        await gifFrames({ url: `${__dirname}/banner-beta.gif`, frames: 'all', outputType: 'canvas', cumulative: false}).then((frameData) => {
            for(let i = 0; i < frameData.length; i++) {
                encoder.setDelay(frameData[i].frameInfo.delay * 15);
                console.log(frameData[0])
                console.log(frameData[i])
                ctx.drawImage((frameData[i]).getImage(), 0, 0, 420, 236)
                ctx.fillText(`${count}`, 50, 90)
                console.log(ctx)
                encoder.addFrame(ctx);
            }
            encoder.finish();
            console.log(encoder.out.getData())
            var attachment = new MessageAttachment(encoder.out.getData(), 'banner.gif');
            guild.channels.cache.get("909860310171648050").send({ files: [attachment] }).catch(console.log);

        })
        //guild.setBanner(canvas.toBuffer()).catch(console.error);
    } catch (e) {
        console.log(e)
    }

error:

ReferenceError: document is not defined
    at savePixels (C:\Users\hotab\Desktop\Root\node_modules\save-pixels-jpeg-js-upgrade\save-pixels.js:127:20)
    at Object.getImage (C:\Users\hotab\Desktop\Root\node_modules\gif-frames\gif-frames.js:105:20)
    at C:\Users\hotab\Desktop\Root\utils\banner.js:31:34

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿萝 2025-02-03 22:03:05

tl; dr:

您的代码在nodejs中运行,其中没有文档对象,因为它在后端服务器上没有意义。因此,使用文档会导致异常。

您正在使用节点模块gif-frames的更多详细信息

,该取决于另一个节点模块,称为save-pixels-jpeg-js-upgrade,它试图访问>文档仅存在于浏览器上下文中的对象(请参阅代码在这里)。

gif-frames的文档表明它可以在节点和浏览器上下文中运行是不正确的,或者对某些API有一些限制,您在这里遇到的API。基于节点模块代码,它似乎仅限于Canvas Case,但我无法进一步帮助您...

TL;DR:

Your code runs in NodeJS, in which there is no document object because it does not make sense on a backend server. So using document results in an exception.

More details

You are using the node module gif-frames that depends on another node module called save-pixels-jpeg-js-upgrade which tries to access the document object that only exists in a browser context (see code here).

Either the documentation of gif-frames stating that it can run in both Node and browsers contexts is incorrect or there are some restrictions to some APIs, which you are facing here. Based on the node module code, it seems to be limited to the CANVAS case, but I can't help you any further...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文