Discord bot not responding to command javascript - javascript

I have been trying to make a Discord bot that responds to the user when they ask for help. Sadly I can't seem to get the bot to work. I am using Discord.JS on Node.JS. Any help would be welcome.
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '[token removed]';
const PREFIX = '!';
bot.on('ready', () => {
console.log('K08 Help Bot is online!');
})
bot.on('message', message=>{
let args = message.content.substring(PREFIX.lenght).split(" ");
switch(args[0]){
case 'help':
message.reply('HELLO');
break;
}
})
bot.login(token);

This might be the issue:
let args = message.content.substring(PREFIX.lenght).split(" "); - length is mistyped as lenght
On a side note: I've submitted an edit to hide your token in this question. For security, you should never share your token; it would allow someone to take over your bot!

Related

Bot isn't listening to guildMemberAdd

I signed up just a few moments ago because something was really bothering me:
I have the following code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('guildMemberAdd', (member) => {
console.log('New member.')
const welcomeEmbed = new Discord.MessageEmbed()
.setImage(member.user.avatarURL())
.setColor('#e9fa2a')
.setAuthor("Mangoly Assistant")
.setTitle("New member in server")
.setDescription('Welcome <#${member.id}> to the server! If you are new, please be sure to check out or rules channel and some useful links. We are glad to be having you here, everyone wave hello! :wave:')
.setFooter('Created by kostis;#4464. || Mangoly Assistant')
client.channels.cache.get('825130442197434418').send(welcomeEmbed)
});
client.once('ready', () => {
console.log('Bot is ready')
})
client.login(nice try);
For some reason, when I leave and rejoin the server, the embed isn't sending at all to the channel. I am getting no errors in the console. Any ideas on what may have gone wrong? Thanks. :)
You need 'Server Members Intent' enabled when you invite the bot. Go to Discord Developer Portal > Bot > Scroll to bottom > make sure server members intent is checked
You should also be able to enable it manually in your code, but idrk how to do it. I think it’s like this:
//Client declaration
const client = new Discord.Client({
ws: {
intents: ['GUILD_MEMBERS']
}
})
Also, quick thing: to use ${variableHere} in a string, it must be a string with backticks ( ` ), like this:
var a = 'abc';
var b = '${a}d' //returns ${a}d
var c = `${a}d` //returns abcs

how to make my bot mention the person who gave that bot command

I am making a discord bot, and I want to do this: someone type -ping and the bot responds with #theuserwhotyped-ping.
I have no errors, I just don't know how to code it, and I don't find any solution on the Internet.
Here is the code I made:
const Discord = require("discord.js");
const client = new Discord.Client();
const prefix = "-";
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("#{user}); //this is the line which dont work
}
});
You can either use message.reply:
message.reply('typed ping');
It sends a reply, like #peter, typed ping.
Or use the message.author:
message.channel.send(`${message.author} typed ping`);
It sends a message like #peter typed ping.

discord.js How to revoke a ban of a banned user using code?

Revoking A Ban Using Code
So, I am making a moderation discord bot using VS Code and I have already set up a ban command. I want to make a unban command (revoking a ban) too so that the user can easily unban a user and not have to go into Server Settings to do it.
I know you can do it because I have been using another bot, GAwesomeBot, that is able to do it.
Link to GAwesomeBot: https://gawesomebot.com
I am a little new to Stack Overflow and this is my first question so pardon me if I am doing anything wrong.
Consider using GuildMemberManager#unban
https://discord.js.org/#/docs/main/stable/class/GuildMemberManager?scrollTo=unban
let guildMemberManager, toUnbanSnowflake;
guildMemberManager.unban(toUnbanSnowflake); // Takes UserResolveable as argument
First you want to define the user that you are unbanning.
Because the user is already banned you will have to mention the user by their ID and then unbanning them.
let args = message.content.split(/ +/g); //Split the message by every space
let user = message.guild.members.cache.get(args[1]); //getting the user
user.unban({ reason: args[2].length > 0 ? args[2] : 'No reason provided.' }); //Unbanning the user
The full example:
//Define your variables
const Discord = require('discord.js');
const client = new Discord.Client();
var prefix = 'your-prefix-here';
//Add a message event listener
client.on('message', () => {
let args = message.content.split(/ +/g); //Split the message by every space
if (message.content.toLowerCase() === prefix + 'unban') {
let user = message.guild.members.cache.get(args[1]); //getting the user
if (!user) return message.channel.send('Please specify a user ID');
user.unban({ reason: args[2].length > 0 ? args[2] : 'No reason provided.' }).then(() => message.channel.send('Success');
}
});

discord bot replies 1 to 2 times

i am currently working on this discord bot but when i call a command it responds twice. would anyone know how to fix this?
bot.on('message', async message => {
let prefix = config.prefix;
let messageArray = message.content.split(' ');
let command = messageArray[0];
let args = messageArray.slice(1);
const { MessageEmbed } = require('discord.js');
if (command === `${prefix}hit`) {
let user2 = `${args}`
if (user2 === '')
user2 = `${bot.user.username}`
let user1 = message.author
message.reply(`hit you ${user2} \n https://media.giphy.com/media/43bOrDOasXG6Y/giphy.gif`)
}
})
Have you tried shutting down all other processes of your bot (Google Cloud, AWS ...)?
The bot could login twice and act like 2 bots, replying twice to the command
Happened to me once

Discord Bot - MessageEmbed

I am very new to javascript and I don't understand a lot of the actual code.
However, I have one query, how can I make the "cahbResponses" appear in a MessageEmbed?
I've looked at the literature on MessageEmbeds from the Discord JS guides but I still have no clue.
The code I am working on is:
client.on('message', function(message) {
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
var args = message.content.substring('!'.length).split(' ');
switch (args[0].toLowerCase()) {
case 'cahb':
var response =
cahbResponses[Math.floor(Math.random() * cahbResponses.length)];
message.channel
.send(response)
.then()
.catch(console.error);
break;
default:
break;
}
});
First, make sure you are importing Discord
// you probably have this on the beginning of your file
const Discord = require("discord.js");
Then
var response = cahbResponses [Math.floor(Math.random()*cahbResponses .length)];
// Create a MessageEmbed object and set the description
const embed = new Discord.MessageEmbed().setDescription(response);
// Then send the embed
message.channel.send(embed);
Read other MessageEmbed methods and properties here: https://discord.js.org/#/docs/main/stable/class/MessageEmbed

Categories

Resources