29 lines
845 B
Python
29 lines
845 B
Python
import difflib
|
|
from io import BytesIO
|
|
|
|
import discord
|
|
|
|
|
|
def diff_file(before: str, after: str, filename: str = "changes.diff") -> discord.File:
|
|
"""
|
|
Generates a diff from two strings and returns it as a discord.File.
|
|
|
|
Args:
|
|
before (str): The original string content.
|
|
after (str): The modified string content.
|
|
filename (str, optional): The name for the output file. Defaults to "changes.diff".
|
|
|
|
Returns:
|
|
discord.File: A file object ready to be sent in a Discord message.
|
|
"""
|
|
diff_generator = difflib.ndiff(
|
|
before.splitlines(),
|
|
after.splitlines(),
|
|
)
|
|
diff_text = "\n".join(diff_generator) or "No changes."
|
|
diff_bytes = diff_text.encode('utf-8')
|
|
|
|
with BytesIO(diff_bytes) as buffer:
|
|
buffer.seek(0)
|
|
return discord.File(buffer, filename=filename)
|