I've spent 3 hours building and tweaking a Node.js web scraper and over 4 hours trying to find a freaking way to broadcast a message to a channel on Discord. I have lost all hope at this time...
This is the code I have, and some parts work, like replying to a message. But I can't find any possible way to just send a message, without that message being a reply.
const discord = require('discord.js');
const bot = new discord.Client();
const cfg = require('./config.json')
bot.on('ready', () => {//this works
console.log(`Logged in as ${bot.user.tag}(${bot.user.id}) on ${bot.guilds.size} servers`)
});
bot.on('message', (msg) => {
switch (msg.content) {
case '/epicinfo':
msg.channel.send('w00t'); //this works
}
});
console.log(bot.users.get("id", "504658757419270144")) //undefined
console.log(bot.channels.get("name", "testbot")) //undefined
console.log(bot.users.find("id", "504658757419270144")) //undefined
console.log(bot.channels.find("name", "testbot")) //undefined
console.log(bot.guilds.get("504658757419270144")); //undefined
console.log(bot.channels.get(504658757419270144)) //undefined
bot.send((discord.Object(id = '504658757419270144'), 'Hello')) //discord.Object is not a function
bot.login(cfg.token);
That might be caused by the fact that you're running your code before the bot is logged in.
Every action has to be made after the bot has emitted the ready event, the only thing you can do outside the ready event is defining other event listeners.
Try putting that part of your code inside the ready event listener, or inside a function called by that event:
client.on('ready', () => {
console.log("Your stuff...");
});
// OR
function partA () {...}
function partB () {...}
client.on('ready', () => {
partA();
console.log("Your stuff...");
partB();
});
// OR
function load() {...}
client.on('ready', load);
In your situation:
client.on('ready', () => { // once the client is ready...
let guild = client.guilds.get('guild id here'); // ...get the guild.
if (!guild) throw new Error("The guild does not exist."); // if the guild doesn't exist, exit.
let channel = guild.channels.get('channel id here'); // if it does, get the channel
if (!channel) throw new Error("That channel does not exist in this guild."); // if it doesn't exist, exit.
channel.send("Your message here.") // if it does, send the message.
});
client.login('your token here')
Try:
bot.channels.find(channel => channel.id === '504658757419270144').send('Your-message');
also, if the channel you are trying to send the message to is in the bots guild, you can use:
bot.on('message' (msg) => {
msg.guild.channels.find(channel => channel.name === 'your-channel-name').send('your-message');
});
Related
I seem to be having serious trouble sending DM's to all users with a specific role.
Here is my bot code:
bot.on('message', async message => {
members = message.guild.roles.cache.find(role => role.id === "12345678998765").members.map(m => m.user.id);
members.forEach(member_id => {
sleep(5000).then(() => {
message.users.fetch(member_id, false).then((user) => {
user.send("some message");
});
});
});
});
This code gives me the error:
Cannot read properties of null (reading 'roles')
on this line:
members = message.guild.roles.cache.find(role => role.id === ....
However that is not the issue. When I comment out the sleep command to send the message and output the member roles using:
members.forEach(member_id => {
console.log(member_id)
//sleep(5000).then(() => {
// bot.users.fetch(member_id, false).then((user) => {
// user.send("some message");
// });
//});
});
I get a list returned in the console of all the user ID's.. So it must be returning the roles.
How do I send a message to all users with a specific role ID ?? I want to be able to loop through them and put a wait in to reduce the API requests and spam trigger.
To fix your first issue, message.guild will be null in DM. Make sure it isn't DM, or if it has to be, choose a guild with client.guilds.cache.get("id").
bot.on("message", async message => {
let { guild } = message
if (!guild) guild = bot.guilds.cache.get("id")
//...
})
To fix your other issue, you can run GuildMember#send() rather than getting the IDs and fetching the users
bot.on("message", async message => {
let { guild } = message
if (!guild) guild = bot.guilds.cache.get("id")
let members = guild.roles.cache.get("12345678998765").members
// I used .get here because it's getting by ID
members.forEach(member => {
sleep(5000).then(() => member.send("some message"));
});
})
The above code will get all the GuildMembers and loop through every one of them, "sleeping" for 5 seconds (if the sleep parameter is milliseconds) and send the member a DM
When I start the bot there comes the Ready message but wehen one of somebody send for example a link into a channel nothing happens. Ther comes no error. I had to reduce the code because of the stackoverflow limit. There were three other parts but they worked. Only these two parts doesnt work.
const Discord = require('discord.js');
const client = new Discord.Client();
const TOKEN = '';
const PREFIX = '!';
//ready
client.once('ready', () => {
console.log('Ready!');
});
//welcome
client.on('guildMemberAdd', member => {
console.log('User joind');
});
//commands
client.on('message', message => {
if (!message.content.startsWith(PREFIX)) return;
if (message.author.bot) return;
if (!message.guild) return;
}
//delete links
else if (message.content.includes('https://')) {
console.log('Link!!!');
}
});
client.login(TOKEN);
Most likely the message does not have the required prefix, thus the the function just returns.
So i want that the bot # the user he is talking to because ${member} (i saw that on youtube) doesnt work and so i want to ask what i have to write so that he writes "Hello #(the users name)..." remember please he is writing that as a dm.
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const client = new Discord.Client();
client.on('ready', () => {
console.log('This Bot is online!');
})
client.login(token);
client.on('guildMemberAdd', member => {
member.send('Hello ${member}, welcome to the PotatoHost Server!');
});
The problem isn`t with the member, is with the client.login(), it should always be at the end if the code!
I hope this will help you. have a great day!
Edit:Also, some members have locked dm's, so you should use a try-catch function, and if you got an err send the welcome message in the chat.
A try-catch function works like that:
try{
member.send("message here")
}catch(error){
member.guild.channels.get("here the Id of the channel you want to send the welcome message in").send("message here")
}
if you don't like the idea of sending the message in a channel in your server just put instead:
console.log(error)
I had the same problem then I started, this should help you solve the problem:
client.on("guildMemberAdd", async member => {
const dmErr = false;
try {
await member.send()
} catch (error) {
dmErr = true;
} if (dmErr === true) {
member.guild.channels.get("Id of the channel here").send()
}
});
I have a function that send messages, but it doesn't work because it's not inside
client.on('message', msg => {
How can I do?
You can send messages from outside client.on('message'..., All you need is channel ID and then you can simply do:
const myChannel = client.channels.get('CHANNEL_ID');
myChannel.send(`your message here!`);
EDIT
You can use this inside a function by passing it as a parameter:
const myChannel = client.channels.get('CHANNEL_ID');
function myFunc(channel) {
channel.send('MESSAGE');
}
Now you can call your function using myChannel:
myFunc(myChannel);
client.on("message") is an event that will trigger each time a new message is sent in a channel the bot can see. (DMs included.)
To send a message to a channel id, first you have to find the channel using the client.channels collection.
const Discord = require("discord.js");
const Client = new Discord.Client(); // Creating a new Discord Client.
Client.on("ready", () => {
const Channel = Client.channels.get("your channel id"); // Searching the channel in the collection channels.
if (!Channel) {
return console.error("No channel found!"); // If the channels does not exist or the bot cannot see it, it will return an error.
} else {
return Channel.send("Your message to send here!"); // We found the channel. Sending a message to it.
};
}); // Making sure the client is ready before sending anything.
Client.login("your bot token here"); // Logging into Discord with the bot's token.
I'm trying to get my bot to send me a direct message when it gets online/is ready
I've tried to use functions such as bot.guilds.members.find("id", "my id")
and bot.guilds.members.get("id", "my id") but it just returns find/get is not a function
bot.on("ready", async message => {
bot.guilds.members.find("id", "my id")
});
I want the bot to send me a message when it comes online
bot.on("ready", async () => {
bot.users.get("Your ID").send("Message")
});
The ready event does not provide any arguments (I hope that's the right way to phrase that). So your callback function's message parameter is undefined, and attempting to read message.channel gives you that "cannot read property 'channel' of undefined" error.
This should work:
const Client = new Discord.Client();
Client.login("YOUR TOKEN");
Client.on("ready", async () => {
let botDev = await Client.fetchUser("YOUR ID");
botDev.send("YOUR PRIVATE MESSAGE");
});