0

I am trying to get my bot to detect a message from a specific person. If this message is a link/image/file, I want it to automatically react with the specified emojis.

Everything works correctly, except for the "if message is a link". This does not seem to work at all. I've tried searching the documentation, and can't seem to find the solution.

Any help is appreciated!

client.on('messageCreate', message => {
    if (message.author.id === '217900978756780032') { //works
    if (message.content.includes("https://")) { //does NOT work
    if (message.attachments.size > 0) { //works
        message.react('<:facebook:1009793997872562287>')
        message.react('<a:siren:886047171768635432>')
        
    }}}
})
isherwood
  • 58,414
  • 16
  • 114
  • 157

1 Answers1

2

This solution uses Regular Expressions to check if a link is an http[s] link and validates under these conditions:

  • link starts with http
  • link has content after the ://

Also, for better practice, you should use Guard Clauses as I have inserted into your code below to write cleaner code.

client.on('messageCreate', message => {
    if (message.author.id !== '217900978756780032') return;
    const isLink = new RegExp(/https?:\/\/\S+/g).test(message.content);
    if (!isLink) return;
    if (message.attachments.size > 0) return;

    message.react('<:facebook:1009793997872562287>');
    message.react('<a:siren:886047171768635432>');
});
Ethan R
  • 338
  • 2
  • 9
  • TypeError: First argument to String.prototype.includes must not be a regular expression at String.includes () When I replaced the code with yours, the code had no "problems" and the bot started correctly - but when I triggered this script, it crashed with the above mention. – PestFromTheWest Oct 07 '22 at 14:55
  • Check the edit I just posted, I used RegExp.test() instead of includes. – Ethan R Oct 07 '22 at 14:57