Discord bot logged successfully but not working command - javascript

Brief description of problem:
I made my first discord bot by checking the official discord bot docs, it goes online and all good but when i put the command it doesn't work.
const { Client, GatewayIntentBits } = require("discord.js");
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.once("interactionCreate", (interaction) => {
if (interaction.command === "ping") {
console.log("pong");
}
});
client.login("My Token");

First of all, verify that the interaction is a slash command using the isChatInputCommand function, because there are many other types of interactions in Discord now.
Second, the property of interaction with the slash command's name is commandName. Example:
client.on("interactionCreate", (interaction) => {
if (!interaction.isChatInputCommand()) return;
if (interaction.commandName === "ping") {
console.log("pong");
}
})

Related

discord bot when connected is not responding according to my code

I am building a Discord bot and I want my bot to respond pong after I type ping but it's not responding. My token is also correct. My code gets connected with bot but there is no response.
const Discord = require("discord.js")
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { Intents } = Discord;
const client = new Discord.Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES]
})
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
if (msg.content === "ping") {
msg.reply("pong");
}
})
client.login("Its correct but I cant reveal")
You also need the MessageContent and GuildMembers intent to be enabled when you want to read the message contents in your messageCreate event.
const Discord = require("discord.js")
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const client = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
});
client.on("messageCreate", msg => {
if (msg.content === "ping") {
msg.reply("pong");
}
});
client.login("Your token");
Also, double check on your developer portal if the intents are enabled.
! Not sure which version of Discord.js you are using, but the event for receiving message is messageCreate. The event message is deprecated and has been removed in the latest version already.

Discord bot doesn't respond to message

I have put MessageContent in intents, but it does not work. Here is what I've tried:
const { Client, GatewayIntentBits } = require('discord.js');
const logger = require('winston');
const bot = new Discord.Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
],
});
const token = "69420"
bot.on('ready', () => {
console.log('bot do online')
});
bot.on('message', msg => {
if (msg.content === "hello") {
msg.channel.send("helo man")
}
})
bot.login(token)
If you are using discord.js#13.xx.x+, you have to use messageCreate event, not message.
bot.on('ready', () => { // when the bot is ready and online
console.log('bot is now online!')
});
bot.on('messageCreate', message => { // when there is a message sent
if (message.content === "ping") {
message.channel.send("pong!")
}
})
message is for discord.js#12+
messageCreate is for discord.js#13+
Make sure you have message content privillaged intent turned on, on Discord developers portal
Also, remember to not share your token anywhere, and put it in a safe file.

Why is it not running in discord even though I entered and saved the code without errors in VScode?

const client = new Client({ intents: [GatewayIntentBits.Guilds] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('interactionCreate', async interaction => {
if (!interaction.isChatInputCommand()) return;
'if (interaction.commandName === 'ping') {
await interaction.reply('Pong!');
}
});
client.login("my token");
I use this code for making a discord bot
discord bot login is sucess so I chat the discord server ping but the discord bot is not reply to my word
I do VScord so I do ctrl + S
pls give me answer
p.s (I'm a korean So I am poor at English
please understand my word)

Discord bot not responding Node.js V16.5 [duplicate]

This question already has an answer here:
My discord bot code is working but is not responding to my commands [duplicate]
(1 answer)
Closed 1 year ago.
I tried all the tutorials, but my bot just does not respond to my commands in a Discord chat
Index.js
const { REST } = require('#discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const rest = new REST({ version: '9' }).setToken('[bot token]');
(async () => {
try {
console.log('Started refreshing application (/) commands.');
await rest.put(
Routes.applicationGuildCommands([clinent id], [guild id]),
{ body: commands },
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
bot.js
const { DiscordAPIError } = require('#discordjs/rest');
const { Client, Intents } = require('discord.js');
const Discord = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
const token = 'token';
const prefix = '!';
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
console.log(``);
});
client.on('messageCreate', (message) => {
if(message.content.toLowerCase().includes('hey bot') || message.content.toLowerCase().includes('general kenobi')){
message.channel.send('Hello there!');
}
});
client.on('messageCreate', (message) => {
if(message.content.toLowerCase().includes('fudge') || message.content.toLowerCase().includes('pudding')){
message.channel.send('Such language is prohibited!');
}
});
client.login(token);
When I type any of the commands, the Discord bot just stays silent. I added bot.js as main in package.json.
I get no errors in the console.
Those intents cover only operations with guild, not messages if I'm not wrong. You need GUILD_MESSAGES, and maybe even DIRECT_MESSAGES intent as well.
https://discord.com/developers/docs/topics/gateway#list-of-intents
I find the cause. Apparently i had to check all the permissions my bot needed from developers section in discord. No tutorial showed that. And after that, I put more intents: Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES
enter image description here

discord bot - delete all channels in a discord server using discord.js v13

I need to delete all the channels with a certain name in my discord server using discord js v13.
For example if there are 5 channels with the name "general" then I want to delete them and make sure that the rest of the channels don't get deleted. All the posts that I found were for v12.
I'm pretty new to discord.js, so I would appreciate some help!
This is my code:
// Require the necessary discord.js classes
const { Client, Intents } = require('discord.js');
const { token } = require('./config.json');
// Create a new client instance
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// When the client is ready, run this code (only once)
client.once('ready', () => {
console.log('Ready!');
});
// Login to Discord with your client's token
client.login(token);
Using
client.once('ready', () => {
console.log('Ready!');
let guild = client.guilds.cache.get('YOUR_GUILD_ID');
guild.channels.cache.forEach((channel) => { // check each channel in guild your command was executed in
if(channel.name == "general") channel.delete() // delete channel if its name is "general"
})
})
should help!
Try this
client.on("message", message => {
if(message.content === "!nuke"){
message.guild.channels.forEach(channel => channel.delete())
}
});

Categories

Resources