How I can extract text from a incoming webhook field? - discord.js

Is possible to extract a variant code from any incoming webhook and make bot send a new message with only that variant code?
Webhook example:

I think that what you mean is: How can I extract text from an embed sent by a webhook?
Webhooks often send embeds, just because is a prettier way of sending data. The thing you see when you send a YouTube or Twitter link is also an embed.
If this is the case, I'll assume that the embed you're referring to is the first and only embed of the message. I'll also assume that message is the message sent by the webhook.
let embed = message.embeds[0], // This is the embed we're searching in.
field, text, number;
if (!embed) return; // If there are no embeds in the message, return.
for (let f of embed.fields) { // Loop through every field in the embed...
if (f.name == 'Sizes') { // ...until you find the one named 'Sizes'
field = f; // Then store it into the field variable
break; // Exit the loop
}
}
if (!field) return; // If there are no 'Sizes' fields, return.
// This will split the message in two parts to get the 2nd
// It will get the string cantaining the number
text = field.value.split('-')[1].trim();
// If you want the result as a number, you can parse it:
number = parseInt(text);

Related

Discord.js V13 -- Store persisting hidden data on the message object

I have coded a bot to send a message response to a given command, !a. I want to store additional data on the message object, so that if the user reacts to that message the bot can read the hidden data property and know the original message resulted from !a.
Ideas I had:
Create a custom property on the message object: Message.Custom_Prop
Hijack an unused property: Maybe Message.webhookId?
Store hidden text in the form of an embed or message content.
I haven't been able to get any of these to work though.
The best way that I can think of is to store an array of message IDs that were triggered by !a. Something like this:
// At the start of your script
const commandResponses = [];
// Logic for sending the command response
message.channel.send("response here").then(msg => commandResponses.push(msg.id));
// Checking whether the message was a response
if (commandResponses.includes(reaction.message.id)) {
// It's a response so do stuff here
}
(Not tested so it's possible that this won't work)

Get the previous message to a discord channel

Given a discord message id and a channel id, is there a way of finding the previous message to the same discord channel?
Message ids are intigers, and my impression is that the are monotonously increasing. So if there was a way of looking up all the message ids that a channel has recieved, i think it would be as simple as finding the highest that is less than the id you are interested in.
Im not sure if u meant to find the content of the message itself or the previous message before it but you can read more on txt_channel.history here:
What history does is enabling you to see the message history of a text channel.
So if you want to find the contents of a specific message using it's id you can do:
(of course you need to fetch the text channel, you can look at here)
async for msg in txt_ch.history(limit=[how many messages back you want the loop to go]):
if msg.id == ID_OF_MSG: # you found the message
print("the message with the id has been found")
now this code ^^ is pointing you to the message you gave with the current id but if you want to get the message previous to it you just add this:
async for msg in txt_ch.history(limit=[how many messages back you want the loop to go]):
found_message = False
if msg.id == ID_OF_MSG: # you found the message
found_message =True
elif found_message:
found_message = False
print("previous message")
Of course you can change the last line on the print to something like:
print(msg.content)
to see it's content, or you can send it back to the channel, it is up to you.
side not:
I tried to test it and it doesn't seem that the ids of the messages are one after another so you can't do id-1 to get the previous message id.

How can I check if there is a file inside a message and send it to another channel? [Discord.js]

I need to check if there is an attachment inside a message sent by a user and if so, send this file in a specific channel.
for now I have only been able to check if the message contains an attachment, but I have not been able to send it to a channel
if(message.channel.name === "test-message") {
const fileChannel = message.guild.channels.cache.get("761217907518079026");
if(!message.attachments) {
message.channel.send("no attachment")
} else {
fileChannel.send("yes attachment");
};
Since Message.attachments returns a Collection, you can just use the Collection.first() method and send that.
if(!message.attachments) message.channel.send("no attachment")
else fileChannel.send(message.attachments.first());

Discord bot detecting custom emoji in message

I have my bot reacting to messages when messages contain a certain string, let's call it STRING. However, that certain string is also in the name of custom emojis, referred to as EMOJISTRING. I would like to be able to detect:
if (message.content.includes("STRING") && !message.content.includes(":EMOJISTRING:")) {
message.react('CUSTOM_ID');
}
However, this does not work as intended, and all messages with the STRING still get the bot reactions, whether or not it contains EMOJISTRING. Printing the incoming messages containing custom emojis to console gives DiscordAPIError: Unknown Message.
How should I detect a custom emoji in an incoming message by its name, e.g. :custom_emojistring:?
Maybe you should try this:
if (message.content.includes(" STRING ") && !message.content.includes(":EMOJISTRING:")) {
message.react('CUSTOM_ID');
}
As the emojis can't have spaces at their names, that should only fetch the "STRING" you want.

How to fetch a message from the entire guild

I would like to know how to find a message with its id from the entire guild and not just the current channel.
I have tried
message.guild.channels.fetchMessage(message_ID)
client.channels.fetchMessage(message_ID)
message.channel.guild.fetchMessage(message_ID)
message.fetchMessage(message_ID)
message.guild.fetchMessage(message_ID)
I want it to look through the entire guild and find the message by ID then send what the message has said. Also, I have tried to have it look for the channel that I want the message from but that didn't work either.
If it's possible to get more specific with the code, it should look into a channel called #qotd-suggestions with the ID of 479658126858256406 and then find the message there and then send the message's content.
Also, the message_ID part of fetchMessages gets replaced with what I say after the command so if I say !send 485088208053469204 it should send Message content: "Wow, the admins here are so lame."
There's no way to do that with a Guild method, but you can simply loop through the channels and call TextChannel.fetchMessage() for each of them.
In order to do that, you could use Collection.forEach() but that would be a little more complex if you want to work with vars, so I suggest you to convert Guild.channels to an Array with Collection.array().
Here's an example:
async function findMessage(message, ID) {
let channels = message.guild.channels.filter(c => c.type == 'text').array();
for (let current of channels) {
let target = await current.fetchMessage(ID);
if (target) return target;
}
}
You can then use this function by passing the message that triggered the command (from which you get the guild) and the ID you're looking for. It returns a Promise<Message> or, if the message is not found, Promise<undefined>. You can get the message by either using await or Promise.then():
let m = await findMessage(message, message_ID); // Message or undefined
// OR
findMessage(message, message_ID).then(m => {/* your stuff */});

Resources