Reputation: 17
I'm a beginner to JavaScript, but I've coded a project in C# before. Sorry if I make rookie mistakes!
I've recently gotten into coding a Discord bot for a server I happen to moderate, and I've made a channel where it spam tags a user in the spam chat. There's only a slight issue that I cannot for the life of me resolve. The command I made doesn't work, and I'm not sure where the code goes wrong. VS Code doesn't detect any errors either. The code is hosted locally on my laptop.
I've been following a tutorial by CodeLyon, and their code works beautifully. I'm just not sure why my code can't work, although it follows a similar format and general code.
I have gotten nowhere doing research by search queries. Multiple solutions haven't worked, including a solution where I'd put a console.log
line where it'd make a line in the terminal indicating the bot detected the prefix in the message I sent. That solution didn't work, so I'm lost. All of my code looks runnable, and obviously, the bot is online due to it being able to spam messages in a certain channel. Has there been an update that changed the way discord.js detects messages?
Here's the index.js code (only 46 lines thankfully), and the second snippet of code is the test.js file the if
statement near the end is expecting.
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");
}
}
(I provided all of the code I could because I wouldn't want to give out random chunks of code, even though the issue lies in another.)
All of the code looks acceptable, but I'm not sure where the code is dropping off that would make the bot unresponsive to my commands! If you could help out, it'd be greatly appreciated. Have a nice day!
Upvotes: 1
Views: 251
Reputation: 1091
To get the length of a string, use .length
instead of .Length
.
const args = message.content.slice(commandPrefix.Length).split("/ +/");
should be
const args = message.content.slice(commandPrefix.length).split("/ +/");
Upvotes: 2