How do I fix this Discord.JS CommandHandler code? - discord.js

const Discord = require("discord.js");
const prefix = ("d");
const client = new Discord.Client ();
client.on("ready", async () => {
console.log(`${client.user.username} is now working online!`);
});
client.on("message", message => {
if(message.author.bot) return;
if(message.channel.type === "dm") return;
let messageArray = message.content.split(" ");
let command = messageArray[0];
let args = messageArray.slice(1);
if(!command.startsWith(prefix)) return;
});
client.login("MY TOKEN");
When I try doing the command: "dhelp" that I've made and coded for example, it doesn't work. Why is this? People have said that I need a command handler and I think it's done but don't why it is still not working, why is this? Please help me, I really need help right now.

Well, to start off, it doesn't actually look like your code does anything regardless of what the value of command is. Perhaps using if/else statements would be a good place to start.
if (command === "help") {
/* some code here */
}
where /* some code here */ is the code for your bot.

Related

What am I doing wrong with my discord bot

I am writing my first discord bot with help from a tutorial. I am stuck because my bot hasn't been able to respond to commands and I've checked the tutorials and my code many times over. Is there something I'm doing wrong?
const discord = require('discord.js');
const client = new discord.Client();
const prefix = '!';
client.once('ready' , () => {
console.log('Zach Is Bad is online');
});
client.on('message', message =>{
if (!message.content.startswith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
}
});
client.login('redacted')
The only issue with your code is message.content.startswith(prefix).
It is startsWith, it's case sensitive.
if (!message.content.startsWith(prefix) || message.author.bot) return;
There is a typo at client.on('ready', () => ...), but this shouldn't be the cause of the error.
You wrote if (!message.content.startswith(prefix) || message.author.bot) return; but I'm pretty sure startwith must be startWith (caps are importants). Try modifying this, then restart your bot.
If it doesn't work put console.log("test OK") right before the ping command, restart and send !ping. If there is "test OK" in your bot logs, then the problem come from the ping command. If you doesn't see this log, try moving the console.log line before the if statement. This is a simple yet effective method to know where does a problem come from.

Moving a mentioned user into a specific voice channel

I am trying to move the tagged user into the voice channel using the channel ID. I saw someone use .setUserChannel but it tells me that it isn't a function. Can someone tell me what I need to fix please :)
if(command === 'ping'){
message.channel.send('pong');
} else if (command === 'abuse'){
//const AbuseTown = client.channel.cache.get(760535995329282161);
const taggedUser = message.mentions.users.first();
message.channel.send(`You want to abuse: ${taggedUser.username}`);
taggedUser.setVoiceChannel('776202269569712168');
return;
}
First of all, you need the GuildMember object, not the User object. Learn more about the difference here
// change:
const taggedUser = message.mentions.users.first();
// to:
const taggedUser = message.mentions.members.first();
Second of all, GuildMember.setVoiceChannel() is an outdated function, so it won't work if you're using v12.x. Instead, use VoiceState.setChannel().
const taggedUser = message.mentions.members.first();
message.channel.send(`You want to abuse: ${taggedUser.username}`);
taggedUser.voice.setChannel('776202269569712168');

How do I make a command that triggers even though the command is followed by other words?

I'm trying to make a command for my Discord bot which the bot replies regardless if the user says some other text inside the command triggering message, like an 8ball command. Is this possible? If you don't know what I mean, here's an example
if ("message.content === "*8ball");
message.channel.send(variablename)
User: *8ball am i cool
Bot: 🎱 | yes
I'm guessing I have to change message.content to something different?
client.on("message", (message) => {
const prefix = "-";
const arguments = message.content.slice(prefix.length).trim().split(/ +/g);
const commandName = arguments.shift().toLowerCase();
if (message.content.startsWith(prefix) && commandName == "8ball") {
message.channel.send("reply here");
};
});

TypeError: message.mentions.channels is not a function

I am currently working on an announce command. I only have problem, when I run the command I get an error.
Code:
const Discord = require("discord.js");
module.exports.run = async (bot, message, args) => {
let channel = message.mentions.channels();
let announcement = args.slice(1).join(" ");
channel.send(announcement);
}
module.exports.help = {
name: "ann"
}
Error:
TypeError: message.mentions.channels is not a function
I hope someone can help me! :-)
channels() is not a thing.
And you also did not specify which channel from the mentions you want as there could be multiple at the same time, that's why you need to use first() to choose the first mentioned channel.
let channel = message.mentions.channels.first();

Figuring out how to execute a command within a specific channel

The entire command works fine, when executed it shows what expected, however it can be executed in any channel when it should only be executable within a specific channel.
Ive tried basically everything that popped into my mind
const Discord = require('discord.js');
const guilds = require('../data/guilds.json');
module.exports.run = async (bot, message, args) => {
if (!message.channel.id === guilds[message.guild.id].botChannelID)
return;
var img
if (args[0] == 'aea') {
img = bot.utils.randomSelection([
I'm really stupid and probably made a dumb mistake so if anyone could please help me with this that would be great
Try to replace:
if (!message.channel.id === guilds[message.guild.id].botChannelID)
To:
if (message.channel.id !== guilds[message.guild.id].botChannelID)
You used a „!“ in front of the message.channel.id although you should use it in your „===“.

Resources