在 discord.js 中制作带有前缀的命令处理程序时出现问题 [重复]
Posted
技术标签:
【中文标题】在 discord.js 中制作带有前缀的命令处理程序时出现问题 [重复]【英文标题】:Having problem making a command handler with prefix in discord.js [duplicate] 【发布时间】:2021-10-26 19:37:44 【问题描述】:我一直在尝试制作一个带前缀的命令处理程序,但是我遇到了一个问题,机器人不会响应并且也没有错误
// Index
---// Collections
client.functions = new Collection();
client.commands = new Collection();
client.interactions = new Collection();
---// Handlers
const handlers = fs
.readdirSync("./config/handlers/")
.filter((file) => file.endsWith(".js"));
const commandFolders = fs.readdirSync("./config/commands/");
const interactionFolders = fs.readdirSync("./config/interactions");
const eventFiles = fs
.readdirSync("./config/events/")
.filter((file) => file.endsWith(".js"));
(async () =>
for (file of handlers)
require(`./config/handlers/$file`)(client);
client.handleCommands(commandFolders);
client.handleInteractions(interactionFolders);
client.handleEvents(eventFiles);
client.login(token);
)();
// The event handler
module.exports = (client) =>
client.handleEvents = async (eventFiles) =>
for (const file of eventFiles)
const event = require(`../events/$file`);
if (event.once)
client.once(event.name, (...args) => event.execute(...args, client));
else
client.on(event.name, (...args) => event.execute(...args, client));
console.log(`[EVENT HANDLER] - $file has been loaded.`);
;
;
// command handler
const fs = require("fs");
module.exports = (client) =>
client.handleCommands = async (commandFolders) =>
for (const folder of commandFolders)
const commandFiles = fs
.readdirSync(`./config/commands/$folder`)
.filter((file) => file.endsWith(".js"));
for (const file of commandFiles)
const command = require(`../commands/$folder/$file`);
client.commands.set(command.name, command);
console.log(`[COMMANDS HANDLER] - $file has been loaded`);
;
;
// and in the last my messageCreate.js
const prefix = ".";
module.exports =
name: "messageCreate",
async execute(message, client)
const args = message.content.slice(prefix.length).trim().split(/ +/);
const command = args.shift().toLowerCase();
if (!message.user.id === "798928603201929306") return;
console.log(
`$message.user.tag used $command in #$message.channel triggered an command.`
);
if (
!message.content.startsWith(prefix) ||
message.author.bot ||
message.channel.type === "DM"
)
return;
if (!client.commands.has(command)) return;
try
client.commands.get(command).execute(message, args, client);
catch (error)
console.error(error);
message.reply("there was an error trying to execute that command!");
,
;
// ping command
module.exports =
name: "ping",
async execute(message, args, client)
message.channel.send(`Pong $client.ws.ping`)
// I haven't tried this yet because I'm getting an error in the messageCreate.js
注意事项:
1.抱歉,代码块太长了
2.希望我输入了所有内容以获得足够的帮助/信息
3.我的 discord.js 版本是 v13
4.如果您需要有关我的文件的更多信息,请在 cmets 中告诉我
【问题讨论】:
我尝试并添加了console.log(
$command 已触发。);
在 messageCreate.js 中的事件之后,我发现没有任何记录
这能回答你的问题吗? “message event listener not working properly”、“Having trouble sending a message to a channel with Discord.js”、“Discord bot is not replying to messages”。
【参考方案1】:
您很可能使用错误的intents 创建了您的客户端。如果您需要收听服务器中的消息,请确保使用 GUILD_MESSAGES
意图。
const bot = new Discord.Client(intents:[Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES])
【讨论】:
意图不应该是discord.Intents.FLAGS.GUILDS, discord.Intents.FLAGS.GUILD_MEMBERS
吗?我认为把它们写出来是 djs 的旧版本,但不完全确定以上是关于在 discord.js 中制作带有前缀的命令处理程序时出现问题 [重复]的主要内容,如果未能解决你的问题,请参考以下文章
Discord.js 从带有前缀的句子中过滤命令(比如 Hey Bot 给我一块蛋糕)
Discord bot 更改前缀命令出错 (discord.js)