#๐Ÿ”’ using link replacements, but doesn't pick up multiple links in one message

18 messages ยท Page 1 of 1 (latest)

lone fiber
#

i'm using the following code that makes the bot reply to the message with the x.com link from the message but replaced with fxtwitter.com, but only picks up the first link sent.

how could i modify this code for the bot to reply with ALL links in the message and send them in a reply with each link on a separate line?

help is much appreciated, thank you very much.

if 'twitter.com' in message.content and 'fxtwitter.com' not in message.content and 'fixupx.com' not in message.content and 'vxtwitter.com' not in message.content:
        urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', message.content)        
        await message.edit(suppress=True)
        await message.reply(urls[0].replace("twitter.com", "fxtwitter.com"), mention_author=False) 
        await asyncio.sleep(3)
        await message.edit(suppress=True) #in case the embed doesn't get removed from the original message the first time, this happened many times and this fixes that issue
void urchinBOT
#

@lone fiber

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

proven iris
lone fiber
lone fiber
# proven iris Are you familiar with for loops?

so ive tried

for url in urls:
  await message.reply(url.replace("x.com", "fxtwitter.com"), mention_author=False)```

this sends the links in seperate replies, is there a way to make it all links in one reply, each link on its own line, perhaps?
proven iris
#

Then

#

!join

void urchinBOT
#
Joining iterables

If you want to display a list (or some other iterable), you can write:

colors = ['red', 'green', 'blue', 'yellow']
output = ""
separator = ", "
for color in colors:
    output += color + separator
print(output)
# Prints 'red, green, blue, yellow, '

However, the separator is still added to the last element, and it is relatively slow.

A better solution is to use str.join.

colors = ['red', 'green', 'blue', 'yellow']
separator = ", "
print(separator.join(colors))
# Prints 'red, green, blue, yellow'

An important thing to note is that you can only str.join strings. For a list of ints,
you must convert each element to a string before joining.

integers = [1, 3, 6, 10, 15]
print(", ".join(str(e) for e in integers))
# Prints '1, 3, 6, 10, 15'
proven iris
#

Then send that as a reply

#

Instead of ", " in the join use "\n" for a newline

lone fiber
#

yeah, this here seems to have done the job

if 'x.com' in message.content and 'fxtwitter.com' not in message.content and 'fixupx.com' not in message.content and 'vxtwitter.com' not in message.content and '/media' not in message.content and '/novx' not in message.content and '/nofx' not in message.content:
        urls = re.findall(r'http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', message.content)        
        await message.edit(suppress=True)
        test = ""
        for url in urls:
          test += url.replace("x.com", "fxtwitter.com") + "\n"
        await message.reply(test, mention_author=False)
        await asyncio.sleep(3)
        await message.edit(suppress=True)
proven iris
#

nice

void urchinBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.