当一个成员加入我的服务器时,我正试图发送一个图像,其中有@napi-rs/canvas个in discord.js 14.

当机器人try 发送消息时,会出现此错误.

我试着和discord的人相处.指南

Cannot read properties of undefined (reading 'cache')

这是我的活动代码:

const { Events, AttachmentBuilder} = require('discord.js');
const Canvas = require('@napi-rs/canvas');

module.exports = {
    name: Events.GuildMemberAdd,
    async execute(client) {
        const canvas = Canvas.createCanvas(1024, 500);
        const context = canvas.getContext('2d');

        const background = await Canvas.loadImage('./resources/canvas/images/welcome.png');


        context.drawImage(background, 0, 0, canvas.width, canvas.height);
        
        const attachment = new AttachmentBuilder(await canvas.encode('png'), { name: 'profile-image.png' });

console.log ('sending message')
        const channel = client.channels.cache.get('1099656104100769924');
        channel.send(({ files: [attachment] }));
            },
};

这是我的index.js%档案

const { Client, Collection, Events, GatewayIntentBits } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');

const { token, prefix } = require('./config.json');


const { channel } = require('node:diagnostics_channel');


const client = new Client({ intents: [GatewayIntentBits.Guilds, GatewayIntentBits.MessageContent, GatewayIntentBits.GuildMessages, GatewayIntentBits.GuildMembers]});



client.commands = new Collection();

const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath).filter(file => file.endsWith('.js'));

for (const folder of commandFolders) {
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require (filePath)

    if ('data' in command && 'execute' in command) {
        client.commands.set(command.data.name, command);
    } else {
        console.log(`[WARNING] The command on ${filePath} dont have  "data" or "execute" property.`);
    }
    }
}

const eventsPath = path.join(__dirname, 'events');
const eventFiles = fs.readdirSync(eventsPath).filter(file => file.endsWith('.js'));

for (const file of eventFiles) {
    const filePath = path.join(eventsPath, file);
    const event = require(filePath);
    if (event.once) {
        client.once(event.name, (...args) => event.execute(...args));
    } else {
        client.on(event.name, (...args) => event.execute(...args));
    }
}

client.login(token);

我try 了许多解决方案,但都不起作用,我使用的是最新版本的discord.js v14.

推荐答案

GuildMemberAdd事件只接受一个参数,即已加入行会的成员.这意味着async execute(client)中的client不是客户,而是GuildMember.

您可以将其更改为:

async execute(member) {
  // ...
}

由于GuildMember的属性为client,因此您可以简单地使用:

const channel = member.client.channels.cache.get('1099656104100769924');

Javascript相关问答推荐

fetch在本地设置相同来源的cookie,但部署时相同的代码不会设置cookie

有条件的悲剧

微软Edge Select 间隙鼠标退出问题

如何避免移动设备中出现虚假调整大小事件?

每次子路由重定向都会调用父加载器函数

Google Apps脚本中的discord邀请API响应的日期解析问题

将字符串UTC Date转换为ngx—counting leftTime配置值的数字

如何从URL获取令牌?

为什么123[';toString';].long返回1?

DOM不自动更新,尽管运行倒计时TS,JS

使用插件构建包含chart.js提供程序的Angular 库?

使用类型:assets资源 /资源&时,webpack的配置对象&无效

令牌JWT未过期

使每个<;li>;元素的 colored颜色 与随机生成的 colored颜色 列表不同(不重复

在渲染turbo流之后滚动到元素

如何在独立的Angular 应用程序中添加Lucide-Angel?

使用createBrowserRoutVS BrowserRouter的Reaction路由

我怎样才能点击一个元素,并获得一个与 puppeteer 师导航页面的URL?

P5play SecurityError:无法从';窗口';读取命名属性';Add';:阻止具有源的帧访问跨源帧

Refine.dev从不同的表取多条记录