Skip to content

Commit

Permalink
sed now respects g
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinMcKechney authored and pbui committed Mar 13, 2024
1 parent ccec8cd commit b048751
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/bobbit/modules/sed.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,27 @@
import re

# Metadata

NAME = 'sed'
ENABLE = True
PATTERN = '^s/(?P<pattern>[^/]+)/(?P<replacement>[^/]*)[/]*$'
PATTERN = '^s/(?P<pattern>[^/]+)/(?P<replacement>[^/]*)[/]?(?P<repeat>[g])?$'
USAGE = '''Usage: s/pattern/replacement/
This searches the channel's history for the most recent line that has the
pattern and then performs the replacement.
'''

# Command

async def sed(bot, message, pattern, replacement):
async def sed(bot, message, pattern, replacement, repeat=None):
replacement = bot.client.format_text('{bold}{}{bold}', replacement)
for original in bot.history.search(message.channel, pattern=pattern, limit=5, reverse=True):
if re.search(PATTERN, original.body):
continue

replaced = re.sub(pattern, replacement, original.body)
if repeat == None:
replaced = re.sub(pattern, replacement, original.body, count=1)
else:
replaced = re.sub(pattern, replacement, original.body,)

return original.copy(body=replaced)

# Register
Expand Down

0 comments on commit b048751

Please sign in to comment.