javascript - 为什么我的 Discord 命令没有响应,虽然在线并且没有检测到错误?
问题描述
我是 JavaScript 的初学者,但我之前用 C# 编写过一个项目。对不起,如果我犯了菜鸟的错误!
我最近开始为我碰巧主持的服务器编写一个 Discord 机器人,并且我创建了一个频道,它在垃圾邮件聊天中标记用户的垃圾邮件。只有一个小问题,我无法终生解决。我做的命令不起作用,我不确定代码哪里出错了。VS Code 也没有检测到任何错误。代码本地托管在我的笔记本电脑上。
我一直在关注 CodeLyon 的教程,他们的代码运行良好。我只是不确定为什么我的代码不能工作,尽管它遵循类似的格式和通用代码。
我没有通过搜索查询进行研究。多种解决方案都没有奏效,包括我console.log
在终端中放置一行的解决方案,表明机器人检测到我发送的消息中的前缀。该解决方案不起作用,所以我迷路了。我的所有代码看起来都可以运行,而且很明显,该机器人是在线的,因为它能够在某个频道中发送垃圾邮件。是否有更新改变了 discord.js 检测消息的方式?
这是 index.js 代码(幸好只有 46 行),第二段代码是 test.js 文件,if
接近结尾的语句所期望的。
const Discord = require('discord.js');
const client = new Discord.Client();
const commandPrefix = 'j!';
const fs = require('fs');
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
// (client.login token would go here, but removed for censorship's sake)
client.on('ready', () => {
console.log('"jaka-bot!!~" is now online!');
var spamChannel = client.channels.cache.get('(are channel tags meant to be censored?)');
setInterval(() => {
spamChannel.send("||(user id removed for censorship)|| Hi there! I'm **jaka-bot**, a bot made by jakanz! ( Or as you may know them as *'jaka-chan'* or simply *'jaka.'* )\n\n**Make sure to DM jaka-chan if these messages ever stop!** It means that his code must've stopped due to lost internet connection or his laptop crapped out.");
}, 1)
})
client.commands = new Discord.Collection();
for(const file of commandFiles){
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// bot cmds
client.on('message', message => {
// if the message content does not start with the prefix or the message author is the bot, ignore
if(!message.content.startsWith(commandPrefix) || message.author.bot) return;
// variables for cmds
const args = message.content.slice(commandPrefix.Length).split("/ +/");
const command = args.shift().toLowerCase();
// socials
if(command == "test"){
client.commands.get("test").execute(message, args);
}
})
module.exports = {
name: 'test',
description: "test",
execute(message, args){
message.channel.send("test test test");
}
}
(我提供了所有我能提供的代码,因为我不想给出随机的代码块,即使问题出在另一个问题上。)
所有代码看起来都可以接受,但我不确定代码在哪里丢失,这会使机器人对我的命令没有响应!如果您能提供帮助,将不胜感激。祝你今天过得愉快!
解决方案
要获取字符串的长度,请使用.length
而不是.Length
.
const args = message.content.slice(commandPrefix.Length).split("/ +/");
应该
const args = message.content.slice(commandPrefix.length).split("/ +/");
推荐阅读
- graphql - GraphQL.ExecutionError:尝试解决时出错
- mysql - mySQL 查询 - 使用 GROUP 和 HAVING 的唯一值
- javascript - React Native:在快速重新渲染屏幕期间无法按下按钮
- excel - 循环遍历所有单元格,检查单元格值是否为 X,然后将单元格复制到不同的位置
- vb.net - 按行添加数据时格式化 DataTable 列
- elasticsearch - 数组上的 Elasticsearch 术语聚合——过滤到与您的查询匹配的存储桶?
- python - 使用未与已安装的 Hydra 应用程序打包的配置
- c# - 我正在关注 Unity 路径跟踪教程,并且在 Int 类型上使用 .Select 时遇到此错误,但我不知道该行的作用
- python - 安装 web3.py 时如何消除错误
- python - 为什么我的带有 if 语句的 while 循环不起作用?