How can i fetch messages in a dm - node.js

I wanna fetch messages in a dm, like i can in a channel
So i have tried
<user>.messages.fetch(id)
But no luck
So what can i do to fetch messages in a dm
Full code
let id = args[0]
let react = args.slice(1).join(' ')
let fetched = message.author.messages.fetch(id)
fetched.react(react)
I tried fetching a channel from the id of the author
But also no luck

You can use message.author.dmChannel to get a DMChannel object of the particular user's DMs. Then, you can use .messages.fetch() to fetch all the messages in the channel, .messages.cache.get() to fetch a message based on its ID or .messages.cache.find() to fetch a message based on any property such as its content. An example:
const dmChannel = message.author.dmChannel
const messages = dmChannel.messages.fetch() // This gives you a list of all the message in the channel
const message = dmChannel.messages.cache.get('message-id') // This fetches the message with the particular message id given
const message = dmChannel.messages.cache.find('message-content') // This fetches the message with the particular message content given

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

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.

Reading contents of DB for channel.id

My logging command needs a channel to send messages, I do this with a >logging #channel-here command, it stores on better-sqlite3, my issue is I am not sure on how to read the contents and convert it to a channel.
I have been working on this for several days, and I have tried several different things, this was my latest attempt
const id = sql.prepare(`SELECT channel FROM logging WHERE guildid = ${message.guild.id};`).get();
const logs = client.channels.get(id);
if (!logs) return;
logs.send(`A message was deleted`);
const logs = needs to = the channel id that you see in the channel record if the guildid record matches the one that the message was deleted in.
Instead of saving the channels mention you should save the channels id.
<#channel-id> is used to mention a channel, but discord.js <guild>.channels.get(), takes only the ID.
So you should only store the Channel Id in the Database, in your code for >logging #channel-here just use const mentionedchannel = message.mentions.channels.first();
and then into your DB just write the mentionedchannel.id, then your .get() should work!

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