How To Send A Discord.JS Startup Message To All Servers - node.js

I wanted to inquire on something I’ve been trying to do recently. I have a Discord.JS bot, and it’s pretty public (130+ servers), and I want it to send a message to any channel in every single server it’s in, when the bot starts up, the purpose is to create a “New Update” notification in one of the channels.

Please do not plan on doing this, as this will get you rate limited really quickly.
This is kind of possible. There is a problem, there is no default guild channel, so you cannot get their "main" channel just by their guild object. If you would want to do that you, would need them to select their channel and store that ID somewhere.
This is a simple solution, where you loop through every guild of your Discord client, and send some channel a message. You don't know what channel with this solution specifically, but you will send some channel a message if it is not a voice channel.
const Discord = require("discord.js");
require("dotenv").config();
// Creating our Client
const client = new Discord.Client();
// Event listener: when the bot is ready
client.on("ready", () => {
// Looping through every guild the bot is in
client.guilds.cache.forEach((guild) => {
// Getting one of their channels
let channel = guild.channels.cache.array()[2];
// Sending the channel a message
channel.send("Hey");
});
});
client.login(process.env.DISCORD_BOT_TOKEN);

Related

How to send DM messages after triggering comand Dsharp+

I'm making a Discord bot and i'm in trouble. Idk how to send direct message after triggering command by user, i tried to use RequireDirectMessage, but after this command just became inactive, i couldn't trigger it neither in discord Guild nor in direct message.
You need to create a DM channel and then send a message to that channel like this
[Command("senddm"]
public async Task SendDMExample(CommandContext ctx)
{
var DMChannel = await ctx.Member.CreateDmChannelAsync();
await DMChannel.SendMessageAsync("WhateverYouAreSending");
}

How can you detect if a user replied to a message of a bot in discord.js?

I have searched the discord.js documentation but I can't find a way to make the bot detect if their own messages were replied by a user in a discord server. Is there a way to do this?
Thank you so much,
Paul10
All messages have a message_reference property. This contains the guild, channel and message id. This property is null if it isn’t a reply. I assume you already know the channel.
//async function
//message is an instance of Message
let { channel, message_reference: reply } = message
const repliedTo = await channel.messages.fetch(reply.message_id);
//repliedTo is now the replied message with author, content, etc

How to get all non bot users in discord js using a discord bot in nodejs

I have created a discord bot by taking reference from this digital ocean link.
Now I can send message to any channel using the bot but my requirement is to send dm to user of that server.
For that I have tried many SO answers and followed other links, but all the solutions end up to be same.
I have tried this two way to get the users of a guild and send dm to any one selected user.
1st way - Get all users of guild (server)
const client_notification = new Discord.Client();
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
Output
Notification manager ready
===>>> discord notification
By this way it only returns me the bot name itself. Although the membersCount is 6.
2nd way - send dm to user directly (server)
client.users.cache.get('<id>').send('<message>');
It gives me undefined in output.
My configs,
Node version: 10.16.3
discord.js version: 12.5.1
My question is how to get all the guild members in discord.js?
Discord added privileged gateway intents recently. So to fetch all member data you need to enable that in the developer portal. After that you need to fetch all the members available, for that we can use the fetchAllMembers client option, and then we need to filter out bot users, so that we don't message them.
const client_notification = new Discord.Client({fetchAllMembers:true}); //Fetches all members available on startup.
client_notification.on('ready', () => {
console.log("Notification manager ready");
let guild = client_notification.guilds.cache.get("Server ID");
guild.members.cache.filter(member => !member.user.bot).forEach(member => console.log("===>>>", member.user.username));
});
client_notification.login("login");
The .filter() method filters out all the bots and gives only the real users.
You should avoid using fetchAllMembers when your bot gets larger though, as it could slow down your bot and use lots of memory.
I think the problem is related to updating the bot policy for discord. Do you have this checkbox checked in the bot settings?
https://discord.com/developers/applications
Some info about client.users.cache:
Its a cache collection, so if you restart bot, or bot never handle user message or actions before, this collection will be empty. Better use guild.members.cache.get('')

Telegram: Cannot send message/photo into a channel with Telegraf (NodeJs)

I would send a message on telegram channel with telegraf. I'v einvited the bot and put him admin.
I've tested with this code:
bot.on('text', (ctx) => {
// Explicit usage
ctx.telegram.sendMessage(ctx.message.chat.id, `Hello ${ctx.state.role}`)
// Using context shortcut
// ctx.reply(`Hello ${ctx.state.role}`)
})
bot.launch();
But it replies only if i wrote on private.
So why it doesn't work on a channel?
Than how can i send a message in that channel without a command? (For example with and interval?
I i try this one:
bot.use((ctx) => {
console.log(ctx.message)
})
when i use the bot on private chat (with him) it returns all the message data. On the channel i receive undefined
In your case CTX have current chat info, if you want to send message to channel provide correct id as documented for Telegraf sendMessage:
telegram.sendMessage(process.env.TELEGRAM_CHANNEL, ctx.message.text);
I am using a bot for a public channel, so in my case it's:
TELEGRAM_CHANNEL=#MY_PUBLIC_CHANNEL_NAME
The channel name is available in channel info settings t.me/MY_PUBLIC_CHANNEL_NAME

Send an unsolicited message to Discord channel using nodejs

Looking for a way to send completely unsolicited messages to my discord channel using discord.js.
Background: I have a nodejs app running on my desktop that is monitoring a few websites and I need to notify my team so they can deal with things as the problem evolves. It's an ongoing issue which I'm not getting into right now. Yes, I want to pay for a pro website monitor, but the bean-counters yadda yadda...
I've read through the docs but it seems everything is geared towards event-driven behaviour from the discord side e.g. a new user arrives, a typed command is entered etc
I don't want that. I just want to send a notification when the server that I'm monitoring does X thing.
Here is what I have so far:
const Discord = require('discord.js');
config.discord.client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
if(shitgoesdown){
new Discord.Message(config.discord.client, data, config.discord.channel);
}
I can't seem to find anything on how to just point this thing at my server. The docs are great and all but they are super dense and right now it's just information overload. If someone could just point at the sign that would be great.
You can fetch the channel using client.channels.fetch(/* Channel ID */) and send a message in the callback using channel.send().
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
// Code to execute when something happens
client.channels.fetch(/* Channel ID */).then(channel => {
channel.send('Some message');
});

Resources