Discord bot won't send a message [duplicate] - javascript

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:

Related

Discord.js V14 - client.on message create not firing [duplicate]

This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 4 months ago.
I'm trying out V14 of Discord.js, and there are so many new things! But this whole intents thing, I'm not getting for some reason. My messageCreate is not firing, and from other posts, I believe it has something to do with intents. I've messed around with intents, but nothing seems to work. Here 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("messageCreate", (message) => {
console.log("new message...")
console.log(message.message)
console.log(message.content)
if (message.mentions.users.first() === client) {
message.reply("Hey!.")
}
});
client.login(process.env.token);
Thanks!!
Figured it out! You need to turn on message content intents in the developer portal:
Also, you need to have messageContent intent at the top:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [
GatewayIntentBits.DirectMessages,
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildBans,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,] });
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (message) => {
if (message.content === "<#1023320497469005938>") {
message.reply("Hey!")
}
});
client.login(process.env.token);
As for getting the ping from the message, not my initial question but something I needed to fix, just use
if (message.content) === "<#BotID>" {CODE HERE}
You need the GuildMessages intent.
Note: You do not need the MessageContent intent in messages that ping the bot, as bots can receive content from messages that ping them without the intent.
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages
]
});
// ...
client.on("messageCreate", (message) => {
// ...
if (message.mentions.users.has(client.user.id)) { // this could be modified to make it have to *start* with or have only a ping
message.reply("Hey!.")
}
});
// ...

How can I properly import Intents from discord.js? [duplicate]

This question already has an answer here:
Discord.js v13 code breaks when upgrading to v14
(1 answer)
Closed 6 months ago.
I am having an issue getting my intents to import from discord.js.
import { Client, Intents } from 'discord.js'
const client = new Discord.Client({ intents: [Discord.Intents.FLAGS.GUILDS, Discord.Intents.FLAGS.GUILD_MESSAGES] });
const token = 'mytoken'
client.on('ready', () => {
console.log('bot logged in')
})
client.login(token)
When I run npm index.js I get this error:
Intent error
Could my installation of the discord.js package be corrupt or maybe didnt install all the way/properly? This is my first time messing with the discord api so im not too familiar with it all yet. Reading documentation and still confused as to why mine wont import properly.
Apparently in v14 of Discord.js Intents was changed to GatewayIntentBits: https://discordjs.guide/additional-info/changes-in-v14.html#enum-values
// import Discord from 'discord.js'
import { Client, GatewayIntentBits } from 'discord.js'
// const Discord = require("discord.js");
// const { Client, GatewayIntentBits } = require("discord.js")
const client = new Client({
intents: [
GatewayIntentBits.GUILDS,
GatewayIntentBits.GUILD_MESSAGES
]
});
const token = 'mytoken'
client.on('ready', () => {
console.log('bot logged in')
})
client.login(token)
I think you missed to import Discord on the first place. You are almost there. Just try this out,
import { Client, GatewayIntentBits } from 'discord.js';
const client = new Client({ intents: [GatewayIntentBits.Flags.Guilds, GatewayIntentBits.Flags.GuildMessages] });
const token = 'mytoken'
client.on('ready', () => {
console.log('bot logged in')
})
client.login(token)

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

How come my "message.reply" is not working (Discord.JS)

So I am learning discord.Js and I am trying to figure out why my message.reply function is not working. I created an event for the bot to listen to messages and when a message with the content of "hello" is sent it should reply with "hello buddy" here is the code:
require('dotenv').config()
const discord = require('discord.js')
const client = new discord.Client({
intents: []
});
client.login(process.env.TOKEN);
client.on('ready', () => {
console.log(`${client.user.tag} has logged in`);
});
client.on('message', function(message){
if(message.content === 'hello') {
message.reply('hello buddy')
}
});
The bot in discord has all the permissions required to send messages in the server I am testing it in.
Your bot does not detect messages. You need GUILDS and GUILD_MESSAGES intents
const client = new discord.Client({
intents: ["GUILDS", "GUILD_MESSAGES"]
})
Also, the message event is deprecated. Use messageCreate instead
client.on('messageCreate', (message) => {
if(message.content === 'hello') {
message.reply('hello buddy')
}
})
Note that I used an arrow function. They are cleaner syntax and you should use them in callbacks

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