Discord.JS getting all attachments from a message - node.js

Hi I'm tying to make a say command that sends attachments that was sent while using the command for example someone uses !say while attaching 3 attachment and it sends the 3 attachments last time I tried to do it I could only get the first attachment but I want to get all the attachments that was attached with the message

If you want to get the files or images that are attached to a message, you can access the attachments property of the message object. This will return a Collection of attachments that you can iterate through and attach to your new message.
e.g.
client.on("message", message => {
if (message.attachments) {
let attachments = message.attachments;
for (let file of attachments) {
message.channel.send({files: [file]});
}
}
})
You can get more information about this in the Discord.js documentation.

Related

How to get messageID with pub/sub gmail Api

I'm getting notifications using Pub/Sub, but the messageId I get is not the messageId to see the email content, how can I get this messageId?
I get this data on my endpoint
{
message:
{
data:"eyJlbWFpbEFkZHJlc3MiOiAidXNlckBleGFtcGxlLmNXXXXXX,
"messageId": "2070443601311540",
"publishTime": "2021-02-26T19:13:55.749Z",
}
subscription: "projects/myproject/subscriptions/mysubscription"
}
The pub/sub messageId has nothing to do with the gmail messageId.
I need to get the messageId from gmail whenever a new email is received in my inbox.
You can get the historyId from the data part after base64 decoding it.
By history id you can get all the messages that are being affected in that history record.
Save this historyId in your local DB or Json file.
next time whenever you receive a new notification from gmail pub/sub you have to call user_history->list by providing the previous history_id that you saved in DB. And save that latest historyId in your DB for future.
call users_history->listUsersHistory(), to get the history object.
The history object also have messagesAdded, messagesDeleted, labelsAdded, labeslRemoved Collection. In general messages collection you may get duplicate messages try being specific.
call usersMessages->get(), to get the specific message.
$service = new Google_Service_Gmail($client);
$response = $service->users_history->listUsersHistory('me', ['startHistoryId' => $historyId]);
$historyList = $response->getHistory();
foreach ($historyList as $history) {
foreach ($history->messages as $message) {
$message = $service->users_messages->get('me', $message->id);
}
}
Note: if you try to get history record from the latest history Id (received in response) you will get nothing because that is the new history Id which contains nothing now.
you have to get the history list starting from the previous history Id
for more details, read these
https://developers.google.com/gmail/api/reference/rest/v1/users.history/list
https://medium.com/#eagnir/understanding-gmails-push-notifications-via-google-cloud-pub-sub-3a002f9350ef

How can I make discord.js bot detect an attachment

I'm trying to make my Discord bot invert an attachment with REST API but I don't know how to change "${message.author.avatarURL}" so it can detect the message attachment instead of the user's avatar. What should I do?
else if (message.content.toLowerCase() === 'invert'){
let link = `https://some-random-api.ml/canvas/invert/?avatar=${message.author.avatarURL({ format: 'png'})}`
let attachment = new MessageAttachment(link, 'invert.png');
message.channel.send(attachment);
}
Message#attachments
message.attachments is a message object property that returns an array of all attachment objects inside of a message. Knowing that we can now get the first message attachment and get its URL using the following code:
else if (message.content.toLowerCase() === 'invert'){
const attachmentURL = message.attachments.first();
if (!attachmentURL) return message.reply('Please provide an image!');
let link = `https://some-random-api.ml/canvas/invert/?avatar=${attachmentURL.url}`
let attachment = new MessageAttachment(link, 'invert.png');
message.channel.send(attachment);
}

Is there a way to obtain Discord message ID upon posting msg to channel from node server?

Using Discord.js in an Express/Node.js app, I'm trying to build a bot that grabs external data periodically and updates Discord with an embed msg containing some elements of that data. I'm trying to add a feature that will check if that data was deleted from the external source(no longer existing upon the next grab), then delete the specific msg in Discord that contains that data that was sent.
Some of the msgs posted in Discord may have duplicate data items, so I want to delete by specific msg ID, but it seems that msg ID is assigned when posted to Discord.
Is there a way to programmatically grab or return this msg ID when sending from Discord.js, rather than manually copy/pasting the msg ID from the Discord GUI? In other words, I need my bot to know which message to delete if it sees that msg's source data is no longer being grabbed.
// FOR-LOOP TO POST DATA TO DISCORD
// see if those IDs are found in persistent array
for (var i = 0; i < newIDs.length; i++) {
if (currentIDs.indexOf(newIDs[i]) == -1) {
currentIDs.push(newIDs[i]); // add to persistent array
TD.getTicket(33, newIDs[i]) // get ticket object
.then(ticket => { postDiscord(ticket); }) // post to DISCORD!
}
}
// if an ID in the persistent array is not in temp array,
// delete from persistent array & existing DISCORD msg.
// message.delete() - need message ID to get msg object...
// var msg = channel.fetchMessage(messageID) ?
Let me refer you to:
https://discord.js.org/#/docs/main/stable/class/Message
Assuming you are using async/await, you would have something like:
async () => {
let message = await channel.send(some message);
//now you can grab the ID from it like
console.log(message.id)
}
If you are going to use .then for promises, it is the same idea:
channel.send(some message)
.then(message => {
console.log(message.id)
});
ID is a property of messages, and you will only get the ID after you receive a response from the Discord API. This means you have to handle them asynchronously.

How to send message to tagged channel?

So I'm trying to make a command that sends an embed to the tagged channel by command for example !embed #games Cool game
So, I was trying with args[0].id and embedargs[0].id and normal args and embedargs, but bot still doesn't know where to send it and throws an error.
Thank you in advance for any help! <3
This should be what you're after:
message.mentions.channels.first().send("Message Content", embed);
This looks for the first mentioned channel, then sends a message to it. You'll just have to split the first two parts of the message to receive your embed, like this:
var embedText = message.content.split(' ').slice(2).join(' ');
// Remove the first 2 Words ("!embed <#channel>")
var embed = new RichEmbed()
.setTitle(embedText);
//Create the Embed using this as the title
message.mentions.channels.first().send("Message Content", embed);
//Send the Embed

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))
}

Resources