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

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

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.

noblox.js and discord.js, currently using discord.js and got a intent error, v13

Was watching a tutorial on making a discord.js and noblox.js bot. error is something about intents not being a constructor or whatever. tutorial playlist isnt even helpful like the discord server. im just following the tutorial to set up a bot account and some slash commands with noblox.js (a roblox js API i guess or a wrapper idk)
code:
const { Client, Intents } = require('discord.js')
const noblox = require('noblox.js')
const { Token } = require('./config.json')
// Create a new client instance
const intents = new Intents([
'GUILDS',
'GUILD_MEMBERS',
'GUILD_MESSAGES'
])
const bot = new Client({ Intents : intents })
bot.login(Token)
bot.on('ready', () => {
console.log(`${bot.user.tag} has booted up.`)
})
error:
TypeError: Intents is not a constructor
at Object.<anonymous> (/home/runner/ImpishDefiantClasses/index.js:7:17)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
IDE: Replit
First of all, You should require intents:
const { Client, Intents } = require("discord.js");
const { Token } = require("./config.json")
const bot = new Client({
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CREATE]
});
Then, the things we want to do when the bot is ready:
bot.on('ready', () => {
console.log(`${bot.user.tag} is now online!`)
})
Also, Make sure you put bot.login(Token) at the end of your code.
At the end, If you want to receive messages, you need to listen to messageCreate event:
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!ping')) {
message.reply('Pong!')
}
});
So your code should look like this:
const { Client, Intents } = require("discord.js");
const bot = new Client({
intents: [Intents.FLAGS.GUILDS,
Intents.FLAGS.GUILD_MEMBERS,
Intents.FLAGS.GUILD_MESSAGES,
Intents.FLAGS.MESSAGE_CREATE]
});
bot.on('ready', () => {
console.log(`${bot.user.tag} is now online!`)
})
client.on("messageCreate", async (message) => {
if (message.author.bot) return;
if (message.content.startsWith('!ping')) {
message.reply('Pong!')
}
});
bot.login(Token);
More info on intents.

Discord bot won't send a message [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 6 months ago.
I started a new project to make a bot for discord, but I always use the same setup every time I create a new discord bot project and update the setup if there is an update in discord.js. In here, I was trying to send a message when the user('s) sends a message.
import { Client, IntentsBitField } from "discord.js"
const client = new Client({
intents: [
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.Guilds
]
});
import dotenv from "dotenv"
dotenv.config();
client.on("ready", () => {
console.log("astoon bot is READY!");
});
// Detecting if there is a message created.
client.on("messageCreate", (message) => {
if (message.content === "ping") {
message.reply("Pong!");
};
});
client.login(process.env.TOKEN);
Change your code intent to:
import { Client, IntentsBitField } from "discord.js"
const client = new Client({
intents: [
IntentsBitField.Flags.GuildMessages,
IntentsBitField.Flags.Guilds,
IntentsBitField.Flags.MessageContent // Add this line.
]
});
import dotenv from "dotenv"
dotenv.config();
client.on("ready", () => {
console.log("astoon bot is READY!");
});
// Detecting if there is a message created.
client.on("messageCreate", (message) => {
if (message.content === "ping") {
message.reply("Pong!");
};
});
client.login(process.env.TOKEN);
If you don't do this, you'll get an empty string on message.content.
And remember this:

Discord bot logged successfully but not working command

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");
}
})

Why is my terminal not letting me run node index.js

I tried to run node index.js in terminal but this error came up: TypeError: Cannot read properties of undefined (reading 'Guilds'). The following is my code:
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.on('interactionCreate', async (interaction) => {
if (!interaction.isCommand()) return;
if (interaction.commandName === '!help') {
await interaction.reply('I dont want to haelp u');
}
});
client.login('OTQ0NjQyMDQ3MTI0NTgyNTEw.YhEkdg.e2AspV6x5JtTqKkq24DkeMmDlSo');
Maybe there is an issue with the Intent. Since you have not provided the error output, I suggest you to use
​   ​const​ ​{​Client​,​ Intents​,​ Collection​}​ ​=​ ​require​(​'discord.js'​)
​   ​const​ ​client​ ​=​ ​new​ ​Client​(​{​intents​:​[​Intents​.​FLAGS​.​GUILDS​,​Intents​.​FLAGS​.​GUILD_MESSAGES​]​}​)
Replace this code with your first two lines of code which is :
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });
And please don't share bot Token ever. Its like login Credentials for your bot. Please reset it on your discord developer console.

Categories

Resources