我正在使用JavaScript Discord(v12.4.0)漫游器,正在尝试创建channelinfo命令,但是在定义和查找message.channel的属性时出现错误。
这是 channelinfo.js
const { MessageEmbed } = require("discord.js")
module.exports = {
name: "channelinfo",
cooldown: 5,
aliases: ["ci", "infochannel"],
description: "Displays info about a channel!",
guildOnly: true,
execute(client, message, args) {
const Discord = require("discord.js")
//Definimos discord.
// const channel = message.mentions.channels.first() || message.guild.channels.cache.find(x => x.id == args[0])
//Definimos el canal del cual sacaremos informacion. Obteniendo el primer canal mencionado o la primera id.
//if (!channel) return message.reply("You must enter a channel!")
//Si no menciono un canal o no puso una id, retorna.
//Definimos el embed que enviaremos
const cha = new Discord.MessageEmbed()
.addField(`Name:`, `- ${Discord.Util.escapeMarkdown(message.channel.name)}`)
//Obtenemos el nombre del canal.
.addField(`Mention:`, `- \`${message.channel}\``)
//Un simple ejemplo de como se menciona este.
.addField(`ID:`, `- ${message.channel.id}`)
//Se obtiene la id del canal.
.addField(`Type`, `- ${message.channel.type}`)
//Obtenemos el tipo de canal, noticias, texto, voz etc
.addField(`Is it nsfw?`, `${message.channel.nsfw}`)
//Revisamos si el canal es nswf, mediante un boolean (false | true)
.addField(`Topic:`, `- ${message.channel.topic.toString().length < 1 ? "There's no topic" : message.channel.topic}`)
//Se obtiene el tema del canal, si el contenido es menor a 1 caracter (Se hace esto por que a veces se buguean los temas) retorne a "No hay un tema."
//Si hay un tema, lo envia.
.addField(`Category:`, `- ${message.channel.parent.name}`)
//Obtenemos la categoria en el que esta el canal.
.setColor("RANDOM") //Color(?
message.channel.send(cha)
}}
这是控制台日志错误
2020-10-26T13:29:59.413677+00:00 app[worker.1]: TypeError: Cannot read property 'channel' of undefined
2020-10-26T13:29:59.413677+00:00 app[worker.1]: at Object.execute (/app/commands/channelinfo.js:26:72)
首先,我尝试添加一个const,该const将获得带有提及或ID的频道。代码是这样的:
const channel = message.mentions.channels.first() || message.guild.channels.cache.get(args[0]) || message.channel;
由于它不起作用,我尝试仅将channel定义为message.channel(常量channel = message.channel),但是我遇到了相同的错误。
然后,我删除了const行,并在嵌入文件中使用了message.channel
而不是channel
,但是仍然遇到相同的错误。
我确信这不是命令处理程序引起的错误,因为所有其他命令都可以正常工作。
好奇心:我尝试执行类似的命令,但显示的角色信息与错误相同(TypeError:无法读取未定义的属性“ roles”)
这是 bot.js
// require fs para el command handler
const fs = require('fs');
// require the discord.js module
const Discord = require('discord.js');
const { token, prefix, yt_api_key } = require('./config.json');
// create a new Discord client
const client = new Discord.Client();
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
const event = require(`./events/${file}`);
let eventName = file.split(".")[0];
client.on(eventName, event.bind(null, client));
});
});
client.noprefixcommands = new Discord.Collection();
fs.readdir("./noprefixcommands/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./noprefixcommands/${file}`);
let noprefixcommandName = file.split(".")[0];
client.noprefixcommands.set(noprefixcommandName, props);
});
});
client.commands = new Discord.Collection();
const cooldowns = new Discord.Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
client.on('message', message => {
if (!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const commandName = args.shift().toLowerCase();
const command = client.commands.get(commandName)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandName));
if (!command) return;
if (command.guildOnly && message.channel.type === 'dm') {
return message.reply('I can\'t execute that command inside DMs!');
}
if (command.args && !args.length) {
let reply = `You didn't provide any arguments, ${message.author}!`;
if (command.usage) {
reply += `\nThe proper usage would be: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
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;
return message.reply(`please wait ${timeLeft.toFixed(1)} more second(s) before using the \`${command.name}\` command.`);
}
}
timestamps.set(message.author.id, now);
setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply('there was an error trying to execute that command!');
}
});
client.login(process.env.BOT_TOKEN);
我用Github编辑代码,用Heroku托管。
当我在Glitch上开始构建命令时,我开始遇到这个问题,但是我回到了Github,这个问题就像Glitch上的问题。
请有人帮助 我会很伟大的