【发布时间】:2021-12-02 08:04:46
【问题描述】:
嘿,我已经尝试了这里的几乎所有教程,但它仍然向我显示未捕获的打字错误..即使在我看到并做了其他人也有过的事情(我的意思是同样的问题)之后,它仍然会继续这样做(我的意思是同样的问题)但它只是赢了不行 每件事都没有奏效,我不知道该怎么办 我也尝试过表达意图,但它说 const client = new Discord.Client();需要改变或不知道该怎么做任何帮助对我来说真的很棒。 我的主要脚本是这样的:
require('dotenv').config();
const Discord = require('discord.js');
const fs = require('fs');
if(!fs.existsSync('./data/servers.json')) {
console.log('servers.json does not exist');
console.log('Creating servers.json...');
fs.writeFileSync('./data/servers.json', '{}', err => {
console.error(err);
});
console.log('Success!');
}
if(!fs.existsSync('./data/config.json')) {
console.log('config.json does not exist');
console.log('Creating config.json...');
fs.writeFileSync('./data/config.json', '{"defaultPrefix": "!"}', err => {
console.error(err);
});
console.log('Success!');
}
if(!process.env.DISCORD_TOKEN) {
console.log('The DISCORD_TOKEN environment variable is not defined');
if(!fs.existsSync('./.env')) {
console.log('.env does not exist');
console.log('Creating .env...');
fs.writeFileSync('./.env', 'DISCORD_TOKEN=', err => {
console.error(err);
});
console.log('Success!');
}
console.log('Please, set the DISCORD_TOKEN environment variable or add it in .env');
}
const servers = require('../exports/exports.js');
const defaultPrefix = require('../data/config.json').defaultPrefix;
const client = new Discord.Client();
client.commands = 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.once('ready', () => {
client.user.setStatus('online');
client.user.setActivity(defaultPrefix + 'help', { type: 'LISTENING' });
console.log('Ready!');
});
client.on('message', message => {
if (message.author.bot) return;
let prefix = servers.getPrefix(message.guild.id);
if(message.content.toLowerCase().startsWith(prefix)) {
const args = message.content.toLowerCase().slice(prefix.length).split(' ');
if(!client.commands.has(args[0])) return;
try {
client.commands.get(args[0]).execute(message, args.slice(1, args.length), prefix);
}
catch(err) {
console.error(err);
message.reply('Something went wrong while executing that command');
}
}
else if(message.content.toLowerCase().startsWith(defaultPrefix + 'help')) {
const args = message.content.toLowerCase().slice(prefix.length).split(' ');
try {
client.commands.get('help').execute(message, args.slice(1, args.length), prefix);
}
catch(err) {
message.reply('Something went wrong while executing that command');
}
}
});
async function close() {
console.log('Received terminate signal');
console.log('Closing');
process.exit();
}
process.on('SIGINT', close);
process.on('SIGTERM', close);
client.login(process.env.DISCORD_TOKEN);
【问题讨论】:
-
您正在查看多年前的教程,专为 discord.js v11 设计。最新版本是 discord.js v13,情况发生了变化。在构建客户端时,您需要提供“意图”。查看official guide,并查看docs 上的意图列表。
-
这个问题也是下一个问题的重复,可以回答你的问题:How do I fix CLIENT_MISSING_INTENTS error - Disord.js
标签: javascript node.js json discord.js client