Discord,js 12.3.1
How I make bot sent message to every members with all servers?
I want to make bot at on_ready
client.on('ready', async (message) => {
});
Depending on how many servers your bot is on and how many users are in that server, your bot will most likely get banned. I do not recommend this in the slightest.
You can use GuildMemberManager.fetch(), which will fetch all members in a server, partnered with a few for... of loops.
// const client = new Discord.Client();
client.on('ready', async () => {
for (const guild of client.guilds.cache)
for (const user of await guild.members.fetch())
user.send('message').catch(() => console.log('User had DMs disabled'));
});
One problem with this method is that some users may be DMd multiple times if they're in multiple of these servers (aside from the obvious problems being it takes up a huge amount of memory, and your bot will probably get banned)
You could also enable enable the fetchAllMembers option when initializing the client.
Whether to cache all guild members and users upon startup, as well as upon joining a guild (should be avoided whenever possible)
Again, as the description says, this should be avoided whenever possible (I'm only showing it to be thorough).
// const client = new Discord.Client({ fetchAllMembers: true });
client.on('ready', () => {
for (const user of client.users.cache)
user.send('message').catch(() => console.log('User had DMs disabled'));
});
I cannot stress enough how bad of an idea this is.
Related
I'm making a slash command bot for Discord and there is an issue with interactions. Apparently the webhook token is only valid for 15 minutes, after that I can no longer edit the message embed. This is usually not enough time for my users as the embed relies on a transaction to hit the blockchain (which these days can take about 30 minutes).
Is there a way to refresh the webhook token for my interaction so I can extend it past 15 minutes?
I do not believe that Discord supports refreshing webhook tokens, however, if you are waiting for your blockchain transaction to be completed before replying, you could defer the reply until that is done. You can do this with the MessageComponentInteraction.deferReply() method.
EDIT: After further thought, I realize that this may not actually solve your problem. You might be forced to use a standard message.reply() workflow for sending this.
Unfortunately, Discord (not discord.js) expires all interaction tokens after 15 minutes. This is a Discord limitation, and you will have to bring this up to Discord to give feedback. However, there is a workaround.
This is the recommended approach if you have a lot of requests coming through. According to your JSON data, you do not need ephemeral messages, which means that you can simply send the message as a bot user.
Note: This requires you to have the bot scope when users are inviting your bot.
const embed = new MessageEmbed()
.setTitle("Waiting for transaction to finish...")
// ...
await interaction.reply("See below message.");
const msg = await interaction.channel.send({ embeds: [embed] });
// Once the transaction finishes
const updatedEmbed = embed
.setTitle("Transaction finished!")
// ...
await msg.edit({ embeds: [updatedEmbed] });
By using a bot that sends a message, you get unlimited time to edit the message, even past 30 minutes.
Another workaround if you don't have the bot scope on your user's servers is to manually create server webhooks (not interactions) and edit them when needed. This requires them to invite the bot with webhook permissions, though.
This approach does not require the bot scope, however, requires webhook permissions.
const webhook = await interaction.channel.createWebhook("My Bot Name", {
avatar: "" // link to your bot's avatar (optional)
});
const embed = new MessageEmbed()
.setTitle("Waiting for transaction to finish...")
// ...
await interaction.reply("See below message.");
const msg = webhook.send({ embeds: [embed] });
// Once the transaction finishes
const updatedEmbed = embed
.setTitle("Transaction finished!")
// ...
await msg.edit({ embeds: [updatedEmbed] });
I'm setting up a cron job to run a bot command that unlocks/locks a channel at a certain time every day. Trying to get the channel returns either undefined or null, depending on how I go about it.
The bot is added to the discord server and is online
require('dotenv').config();
const Discord = require("discord.js");
const client = new Discord.Client();
client.login(process.env.TOKEN);
const chan = client.channels.cache.get("858211703946084352");
console.log(chan);
const channel = client.channels.fetch("858211703946084352").then(res => {
console.log(res);
});
console.log(channel);
When I run it in the console I get
undefined
Promise { <pending> }
null
I have looked at many many examples and solutions, but none seem to resolve my issue
Edit:
bot has admin permissions.
I did the 'right click on channel and copy ID' technique, which matched the ID I got when I used dev tools to examine the element containing the channel name
There is a MEE6 bot in the server so I know bots can send messages
Edit2:
For fun and profit I deleted the app and remade it, same issue
I tried using a channel the MEE6 bot sends to, same issue
Try running this code in the ready event
client.on('ready', () => {
const chan = client.channels.cache.get("858211703946084352");
console.log(chan);
});
I'm making a bot to detect when a user joined. The code below does not work when my test account joins. Does it only work when it's the first time a user joins. If so, how can I make it work everytime.
client.on('guildMemberAdd', member => {
// Send the message to a designated channel on a server:
const channel = member.guild.channels.cache.find(ch => ch.name === 'general-chat');
// Do nothing if the channel wasn't found on this server
if (!channel) return;
// Send the message, mentioning the member
channel.send(`Welcome to the server, ${member}`);
});
You might have to enable Presence Intent on your bot settings on the Discord Developer Portal, which makes it able to detect new members.Here's an screenshot.
My Goal
My goal is to figure out why Collection#find returns undefined when I try to find a user in my server, but they're offline and have no roles.
Expectation
Usually the console logs an array of all the properties of the user
Actual Result
The console logs Collection#find, as undefined if the user in my server is offline and has no roles
What I've Tried
I've tried using Collection#get, but it turns out that it returns the same response. I've tried searching this up on Google, but no one has asked this question before.
Reproduction Steps
const Discord = require('discord.js');
const client = new Discord.Client();
const {prefix, token} = require('./config.json');
client.once('ready', () => {
console.log('Client is online');
const user = client.users.cache.find(user => user.tag === 'Someone#1234');
console.log(user);
};
client.login(token);
Make sure that whoever is helping you, whether it's an alt account or your friend, that they have no roles, and they're completely offline in your server
Output:
Client is online undefined
I had the same problem. I don't know why this happens, but I used this line to fix that:
((await m.guild.members.fetch()).filter(mb => mb.id === "The ID")).first()`
Basically, this collect all the members, then filters them with the property you want, and the first() at the end is to make it a single object instead of a collection.
Instead of the user.tag property, try using the user.username and the user.discriminator properties. This worked for me
const user = client.users.cache.find(user => user.username === 'Someone' && user.discriminator === '1234');
Also check spelling, capitalization, ect.
A simple solution to my problem is just create an async function and use await guild.members.fetch() to cache the users and then use Collection#find to get the user.
const Discord = require('discord.js');
const bot = new Discord.Client();
const token = '';
bot.on('ready', () =>{
console.log('This bot is online');
})
bot.on('message', msg=>{
if(msg.content === "?rates"){
msg.reply('.')
}
})
bot.login(token);
This is what I have so far it is very basic, I understand, I'm just trying to get some sort of idea how to process. What I want is as soon as a website gets updated or changed. I would like it to tag everyone and in a certain channel and specifies what has changed. I know this will be a long process but I'm in for the ride :) would appreciate any help.
You need a webhook on the website and listen to it with your bot, if you have control over the site, this may help you, otherwise you could look if the site has one or perhaps ask an owner.
A probably working but not very nice (and not very clean) solution would be to save the text of the website every 5 seconds or so and compare it to the previous save. If it changed, you notify the members through sending a message.