izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

Help with subreddit linker bot? #296

Closed Scout339 closed 5 years ago

Scout339 commented 5 years ago

How might I make a bot that listens for a keyword, and only repeats what is after the keyword up until a space is detected? Here is what I have so far:

if (fromID === me && text.includes("r/") ) {
    bot.sendMessage({ to: channel, message: txt.replace('https://www.reddit.com/r/','') });
}

But i'm thinking that it will continue to copy the message after the spaces, such as the outcome of "check out r/github you guys!"

Bot returns: "https://www.reddit.com/r/github you guys!"

CheweyZ commented 5 years ago
if (fromID === me && text.includes("r/") ) {
    bot.sendMessage({ to: channel, message: text.replace(/(.*)r\/([^ ]+).*/,"https://www.reddit.com/r/$2")  });
}

Above will produce for input check out r/github you guys! with result https://www.reddit.com/r/github

If you want the text after the space then /(.*)r\/([^ ]+.*)/ which will produce https://www.reddit.com/r/github you guys! `

Scout339 commented 5 years ago

Thank you! That is all I needed for now.

Edit: Faulty code that I had to rework with a lot of help... Ended up scrapping all of it.