Send message As a reply to specific server's channel - node.js

I have 2 guilds and the BOT is in both guilds.
The Bot works like this.
When I send a message from guild 1, the BOT copies the message and sends it to guild 2.
The requirement of making a BOT is working well but I want to make it as a reply to the message when the BOT sends it to guild 2.
Here is the CODE
const guild = client.guilds.cache.get('881014432841490452')
const channel = guild.channels.cache.find(chname => chname.name === 'general')
channel.send(message)**
If someone knows please help me.
Thanks

You cannot make a message in the same time reply to a message and make that message appear on another guild. When you reply to a message it will be on the same channel as the original message.
The only solution you have is to send the message with the URL so people can click it and access the original message:
const guild = client.guilds.cache.get('881014432841490452')
const channel = guild.channels.cache.find(c => c.name === 'general')
channel.send(`Link : ${message.url}\nContent : ${message.content}`)

Related

Discord.js Network Bot : Getting the users message and sending it to the other channels

I am new to coding and I was trying to make a network bot for my server.
I am trying to have it so if someone sends a message in the network channel, then it will send that message to channels. I need some help as I have no idea what I am doing and can't find anything on Google.
I am going to set the time later but I want to get the first bit working.
Here's my code :
const network = "1022619449662124052"
const channels = [`1009882056970485852`, `1009924409714299040`]
const timeout = 1800000 // 30 minutes
client.on("messageCreate", message => {
if(message.channel.(network))// The channel that the bot watchs for a message / advertisement
let network = new Discord.MessageEmbed()
.setTitle(`Grow Togethers NetWork`)
.setDescription(`Ad Posted By : ${message.author.tag}`)
message.channel.get(channels).send(`${content}` {embed: [network]})
//${content} = the message that the user used ( advertisement )
})
Iterate through your array of ids and get the channel object. Then send to that channel
channels.forEach(id => {
const channel = message.guild.channels.cache.get(id);
channel
.send(...)
.catch(console.error);
});

I just made a welcome and goodbye message for my discord.js bot but it doesn't send the messages to the required channels

Here's my code,
Welcome message:
client.on('guildMemberAdd', async member => {
if(member.guild.id !== "737222740305641472")return;
const channel = guild.channels.cache.get("750952211659620413")
if (!channel) return;
let data = await canva.welcome(member, { link: "https://imgur.com/a/BPTpkDT", blur: false })
const attachment = new Discord.MessageAttachment(
data,
"welcome-image.png"
);
channel.send(
"Welcome message here",
attachment
);
});
And my goodbye message's code:
client.on('guildMemberRemove',(member) => {
if(member.guild.id !== "737222740305641472")return;
guild.channels.cache.get('781737515421138984').send(`Goodbye message here`);
});
I'd like to make it clear that I dont get any error's in my console and that I've given the bot all the perms it needs to send messages.
Also, I've already declared my guild before, so that isn't supposed to be the problem.
Can anyone tell what I'm doin wrong?
As directed by this comment, it is appearent that you have not enabled privilliged intents for your application. For your application to listen to events such as guildMemberAdd and guildMemberRemove you would have to enable to Members Intent from the Discord Developer Portal. Here is what you would be looking for:
This can be found under
Your Application > Bot > ( Scroll down ) Privilleged Gateway Intents > Server Members Intent ( toggleable ).
This was a change introduced in the Discord API version v8 for the intents to be mandatorily enabled for such API requests.

Sending message to specific channel, not working

Having trouble sending a bot message on a specific channel. I very simply want to have the bot send a message to #general when it activates, to let everyone know it's working again.
In the bot.on function, I've tried the classic client.channels.get().send(), but the error messages I'm getting show that it thinks client is undefined. (It says "cannot read property 'channels' of undefined")
bot.on('ready', client => {
console.log("MACsim online")
client.channels.get('#general').send("#here");
})
The bot crashes immediately, saying: Cannot read property 'channels' of undefined
The ready event doesn't pass any client parameter.
To get a channel by name use collection.find():
client.channels.find(channel => channel.name == "channel name here");
To get a channel by ID you can simply do collection.get("ID") because the channels are mapped by their IDs.
client.channels.get("channel_id");
Here is an example I made for you:
const Discord = require("discord.js");
const Client = new Discord.Client();
Client.on("ready", () => {
let Channel = Client.channels.find(channel => channel.name == "my_channel");
Channel.send("The bot is ready!");
});
Client.login("TOKEN");
You need to get the guild / server by ID, and then the channel by ID, so your code will be something like :
const discordjs = require("discord.js");
const client = new discordjs.Client();
bot.on('ready', client => {
console.log("MACsim online")
client.guilds.get("1836402401348").channels.get('199993777289').send("#here");
})
Edit : added client call
The ready event does not pass a parameter. To gain the bot's client, simply use the variable that you assigned when you created the client.
From your code, it looks like the bot variable, where you did const bot = new Discord.Client();.
From there, using the bot (which is the bot's client), you can access the channels property!

Deleting replies from a telegram bot

I have a telegram bot written in Python. It sends message on specific commands as per mentioned in code. I want to delete the replies sent by this bot suppose after X seconds. There is a telegram bot API that deletes the message
https://api.telegram.org/botBOTID/deleteMessage?chat_id=?&message_id=?
To delete the message we need chat id and message id. To get the chat id and message id of the replied message by the bot, I need to keep reading all the messages (even from users) and find these id's. This will increase a lot of overhead on the bot.
Is there any other way to find these id's without reading all the messages?
This is the Chat object. It contains the identifier of the chat.
This is the Message object. It contains the identifier for that message and a Chat object representing the coversation where it resides.
The sendMessage REST function returns the Message you sent on success.
So your solution here is to store the Message object you get when sending a message, and then call the delete api with the parameters from the stored objects (Message.message_id and Message.chat.id).
Regarding Python you can use the pickle module to store objects in files.
In nodeJs I use these codes to delete the replies sent by bot after 10 seconds:
let TelegramBot = require('node-telegram-bot-api');
let bot = new TelegramBot(token, {polling: true});
bot.on('message', (msg) => {
let chatId = msg.chat.id;
let botReply = "A response from the bot that will removed after 10 seconds"
bot.sendMessage(chatId ,botReply)
.then((result) => { setTimeout(() => {
bot.deleteMessage(chatId, result.message_id)
}, 10 * 1000)})
.catch(err => console.log(err))
}

Sending Direct Message to a user

Right now, my bot listens to a channel in slack called 'teams' and replies in that channel. I want it to reply to the user (say 'User1') via direct message instead. How can I construct a message to do that?
Thanks!
You can send a Direct Message as follows:
var response = await activityContext.ConnectorAPI.Conversations.CreateDirectConversationAsync(activity.Recipient, activity.From);
var reply = activity.CreateReply($"This is a direct message to {activity.From.Name ?? activity.From.Id} : {activity.Text}");
reply.Conversation = new ConversationAccount(id: response.Id);
reply.ReplyToId = null;
await activityContext.ConnectorAPI.Conversations.SendToConversationAsync(reply);

Resources