当一个成员加入我的服务器时,我正试图发送一个图像,其中有@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.