Discord.js V12 Client.emit and TypeError:
Discord.js V12 Client.emit and TypeError:
我尝试改进不起作用的代码,所以我回到旧代码然后得到这个错误
at Client.emit (node:events:527:28)
at Object.module.exports [as MESSAGE_CREATE] (/workspace/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at MessageCreateAction.handle (/workspace/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at WebSocketManager.handlePacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/workspace/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:527:28)
TypeError: Cannot read properties of undefined (reading 'id')
at Object.run (/workspace/commands/verify.js:11:29)
at Client.<anonymous> (/workspace/index.js:112:21)
at MessageCreateAction.handle (/workspace/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Client.emit (node:events:527:28)
at Object.module.exports [as MESSAGE_CREATE] (/workspace/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/workspace/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:527:28)
index.js
const Discord = require('discord.js');
const client = new Discord.Client();
const { token, default_prefix } = require('./config.json');
const { readdirSync } = require('fs');
const { join } = require('path');
const canvas = require('discord-canvas')
const express = require('express');
const bodyParser = require("body-parser")
const app = express();
const cors = require("cors");
app.use(cors());
const config = require('./config.json');
client.config = config;
client.commands = new Discord.Collection();
const cooldowns = new Discord.Collection();
const commandFiles = readdirSync(join(__dirname, "commands")).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
client.on("error", console.error);
client.on('ready', async () => {
console.log('I am ready');
client.user.setActivity("Unfair Top", {
type: 'WATCHING'
})
})
client.on("guildMemberAdd", async member => {
c = client.channels.cache.get("943205142151434331")
client.channels.cache.get("943204976761663629").send(`Welcome to Exodium ${member}, for support go to ${c}.`)
const image = await new canvas.Welcome()
.setUsername(member.user.username)
.setDiscriminator(member.user.discriminator)
.setMemberCount(member.guild.memberCount)
.setGuildName(member.guild.name)
.setAvatar(member.user.displayAvatarURL({ dynamic: true, format: "png" }))
.setColor("border", "#8015EA")
.setColor("username-box", "#8015EA")
.setColor("discriminator-box", "#8015EA")
.setColor("message-box", "#8015EA")
.setColor("title", "#8015EA")
.setColor("avatar", "#8015EA")
.setBackground("https://thumbs.dreamstime.com/b/degree-equirectangular-projection-space-background-nebula-stars-environment-map-hdri-spherical-panorama-d-illustration-200369002.jpg")
.toAttachment()
const attachment = new Discord.MessageAttachment(image.toBuffer(), "welcome.png");
client.channels.cache.get("943204976761663629").send(attachment)
})
client.on("message", async message => {
if (message.author.bot) return;
if (message.channel.id == 944770295590432860) {
if (message.content != "-verify") {
return message.delete()
}
}
let prefix = default_prefix
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return message.reply("Unknown Command!");
if (command.args && !args.length) {
let reply = `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
return message.channel.send("Unknown Command!");
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
let timeout = new Discord.MessageEmbed()
.setDescription(`❌ Wait ${timeLeft.toFixed(1)} to use ${command.name} again.`)
.setColor("RED")
return message.reply(timeout);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.run(client, message, args);
} catch (error) {
console.error(error);
}
}
});
//Express
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/', (req, res) => {
let id = req.body.id
verify(id);
});
async function verify(i) {
try {
var g = await client.guilds.cache.get("943025185295044608");
var mem = await g.members.cache.get(i)
var r = await g.roles.cache.get("943202823217221652");
mem.roles.add(r);
let userverembed = new Discord.MessageEmbed()
.setTitle(`✅ ${mem.username} have been verified`)
.setFooter(mem.username + " may now enter Unfair.top server!", message.author.displayAvatarURL())
client.channels.cache.get("944841210462367794").send(userverembed)
let verifyedembed = new Discord.MessageEmbed()
.setTitle(":white_check_mark: You have been verified")
.setFooter(mem.username + " may now enter Unfair.top server!", message.author.displayAvatarURL())
mem.send(verifyedembed)
} catch (e) {
client.channels.cache.get("944841210462367794").send(`${i} was not verified`)
}
}
const port = process.env.PORT || 3081;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
client.login(token);
Verify.js
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "verify",
description: "Verify yourself to enter the discord server",
cooldown: "10",
run(message, args) {
let member = message.author
if (message.channel.id != 944770295590432860) return;
message.delete()
let verifyembed = new MessageEmbed()
.setTitle("Unfair Top Verification")
.setDescription(`[Click here to begin](https://exodium.digital/verify/?id=${member.id})`)
.setFooter("Verification started for " + message.author.username, message.author.displayAvatarURL())
member.send(verifyembed)
}
}
这可能是一个小错误,但多年来我一直在努力修复它。
我试过将“消息”切换为“messageCreate”
我试图转换到 v13,但后来卡住了,所以我回到旧的 v12,然后开始出现错误。
在 client.on('message')
中执行您的命令时,您将给它三个参数:command.run(client, message, args)
,而在您的 verify.js
文件中,您只接受两个不在正确的顺序,所以 verify.js
文件中的 message
参数实际上是 client
,而 args
参数是 message
。要修复您的错误,您只需将 verify.js
文件中的参数顺序更改为:
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "verify",
description: "Verify yourself to enter the discord server",
cooldown: "10",
run(client, message, args) {
let member = message.author
if (message.channel.id != '944770295590432860') return;
message.delete()
let verifyembed = new MessageEmbed()
.setTitle("Unfair Top Verification")
.setDescription(`[Click here to begin](https://exodium.digital/verify/?id=${member.id})`)
.setFooter("Verification started for " + message.author.username, message.author.displayAvatarURL())
member.send(verifyembed)
}
}
(注意:比较消息的频道id时,你是在比较一个数字。在discord.js
中,ids存储为字符串,所以我也更正了
我尝试改进不起作用的代码,所以我回到旧代码然后得到这个错误
at Client.emit (node:events:527:28)
at Object.module.exports [as MESSAGE_CREATE] (/workspace/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at MessageCreateAction.handle (/workspace/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at WebSocketManager.handlePacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/workspace/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:527:28)
TypeError: Cannot read properties of undefined (reading 'id')
at Object.run (/workspace/commands/verify.js:11:29)
at Client.<anonymous> (/workspace/index.js:112:21)
at MessageCreateAction.handle (/workspace/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Client.emit (node:events:527:28)
at Object.module.exports [as MESSAGE_CREATE] (/workspace/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/workspace/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
at WebSocket.onMessage (/workspace/node_modules/ws/lib/event-target.js:132:16)
at WebSocket.emit (node:events:527:28)
index.js
const Discord = require('discord.js');
const client = new Discord.Client();
const { token, default_prefix } = require('./config.json');
const { readdirSync } = require('fs');
const { join } = require('path');
const canvas = require('discord-canvas')
const express = require('express');
const bodyParser = require("body-parser")
const app = express();
const cors = require("cors");
app.use(cors());
const config = require('./config.json');
client.config = config;
client.commands = new Discord.Collection();
const cooldowns = new Discord.Collection();
const commandFiles = readdirSync(join(__dirname, "commands")).filter(file => file.endsWith(".js"));
for (const file of commandFiles) {
const command = require(join(__dirname, "commands", `${file}`));
client.commands.set(command.name, command);
}
client.on("error", console.error);
client.on('ready', async () => {
console.log('I am ready');
client.user.setActivity("Unfair Top", {
type: 'WATCHING'
})
})
client.on("guildMemberAdd", async member => {
c = client.channels.cache.get("943205142151434331")
client.channels.cache.get("943204976761663629").send(`Welcome to Exodium ${member}, for support go to ${c}.`)
const image = await new canvas.Welcome()
.setUsername(member.user.username)
.setDiscriminator(member.user.discriminator)
.setMemberCount(member.guild.memberCount)
.setGuildName(member.guild.name)
.setAvatar(member.user.displayAvatarURL({ dynamic: true, format: "png" }))
.setColor("border", "#8015EA")
.setColor("username-box", "#8015EA")
.setColor("discriminator-box", "#8015EA")
.setColor("message-box", "#8015EA")
.setColor("title", "#8015EA")
.setColor("avatar", "#8015EA")
.setBackground("https://thumbs.dreamstime.com/b/degree-equirectangular-projection-space-background-nebula-stars-environment-map-hdri-spherical-panorama-d-illustration-200369002.jpg")
.toAttachment()
const attachment = new Discord.MessageAttachment(image.toBuffer(), "welcome.png");
client.channels.cache.get("943204976761663629").send(attachment)
})
client.on("message", async message => {
if (message.author.bot) return;
if (message.channel.id == 944770295590432860) {
if (message.content != "-verify") {
return message.delete()
}
}
let prefix = default_prefix
if (message.content.startsWith(prefix)) {
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return message.reply("Unknown Command!");
if (command.args && !args.length) {
let reply = `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
return message.channel.send("Unknown Command!");
}
if (!cooldowns.has(command.name)) {
cooldowns.set(command.name, new Discord.Collection());
}
const now = Date.now();
const timestamps = cooldowns.get(command.name);
const cooldownAmount = (command.cooldown || 3) * 1000;
if (timestamps.has(message.author.id)) {
const expirationTime = timestamps.get(message.author.id) + cooldownAmount;
if (now < expirationTime) {
const timeLeft = (expirationTime - now) / 1000;
let timeout = new Discord.MessageEmbed()
.setDescription(`❌ Wait ${timeLeft.toFixed(1)} to use ${command.name} again.`)
.setColor("RED")
return message.reply(timeout);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.run(client, message, args);
} catch (error) {
console.error(error);
}
}
});
//Express
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.post('/', (req, res) => {
let id = req.body.id
verify(id);
});
async function verify(i) {
try {
var g = await client.guilds.cache.get("943025185295044608");
var mem = await g.members.cache.get(i)
var r = await g.roles.cache.get("943202823217221652");
mem.roles.add(r);
let userverembed = new Discord.MessageEmbed()
.setTitle(`✅ ${mem.username} have been verified`)
.setFooter(mem.username + " may now enter Unfair.top server!", message.author.displayAvatarURL())
client.channels.cache.get("944841210462367794").send(userverembed)
let verifyedembed = new Discord.MessageEmbed()
.setTitle(":white_check_mark: You have been verified")
.setFooter(mem.username + " may now enter Unfair.top server!", message.author.displayAvatarURL())
mem.send(verifyedembed)
} catch (e) {
client.channels.cache.get("944841210462367794").send(`${i} was not verified`)
}
}
const port = process.env.PORT || 3081;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
client.login(token);
Verify.js
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "verify",
description: "Verify yourself to enter the discord server",
cooldown: "10",
run(message, args) {
let member = message.author
if (message.channel.id != 944770295590432860) return;
message.delete()
let verifyembed = new MessageEmbed()
.setTitle("Unfair Top Verification")
.setDescription(`[Click here to begin](https://exodium.digital/verify/?id=${member.id})`)
.setFooter("Verification started for " + message.author.username, message.author.displayAvatarURL())
member.send(verifyembed)
}
}
这可能是一个小错误,但多年来我一直在努力修复它。 我试过将“消息”切换为“messageCreate”
我试图转换到 v13,但后来卡住了,所以我回到旧的 v12,然后开始出现错误。
在 client.on('message')
中执行您的命令时,您将给它三个参数:command.run(client, message, args)
,而在您的 verify.js
文件中,您只接受两个不在正确的顺序,所以 verify.js
文件中的 message
参数实际上是 client
,而 args
参数是 message
。要修复您的错误,您只需将 verify.js
文件中的参数顺序更改为:
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "verify",
description: "Verify yourself to enter the discord server",
cooldown: "10",
run(client, message, args) {
let member = message.author
if (message.channel.id != '944770295590432860') return;
message.delete()
let verifyembed = new MessageEmbed()
.setTitle("Unfair Top Verification")
.setDescription(`[Click here to begin](https://exodium.digital/verify/?id=${member.id})`)
.setFooter("Verification started for " + message.author.username, message.author.displayAvatarURL())
member.send(verifyembed)
}
}
(注意:比较消息的频道id时,你是在比较一个数字。在discord.js
中,ids存储为字符串,所以我也更正了