Skip to content

Commit

Permalink
Upload source as a file if possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorialis committed Mar 31, 2021
1 parent 31dabf7 commit 9099fe0
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions jishaku/features/invocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

import contextlib
import inspect
import io
import pathlib
import time

import discord
Expand Down Expand Up @@ -138,12 +140,28 @@ async def jsk_source(self, ctx: commands.Context, *, command_name: str):
except (TypeError, OSError):
return await ctx.send(f"Was unable to retrieve the source for `{command}` for some reason.")

filename = "source.py"

try:
filename = pathlib.Path(inspect.getfile(command.callback)).name
except (TypeError, OSError):
pass

# getsourcelines for some reason returns WITH line endings
source_lines = ''.join(source_lines).split('\n')
source_text = ''.join(source_lines)

# Guild's advertised limit minus 1KiB for the HTTP content
filesize_threshold = (ctx.guild.filesize_limit if ctx.guild else 8 * 1024 * 1024) - 1024

if len(source_text) < filesize_threshold:
await ctx.send(file=discord.File(
filename=filename,
fp=io.BytesIO(source_text.encode('utf-8'))
))
else:
paginator = WrappedPaginator(prefix='```py', suffix='```', max_size=1985)

paginator = WrappedPaginator(prefix='```py', suffix='```', max_size=1985)
for line in source_lines:
paginator.add_line(discord.utils.escape_markdown(line))
paginator.add_line(source_text.replace('```', '``\N{zero width space}`'))

interface = PaginatorInterface(ctx.bot, paginator, owner=ctx.author)
await interface.send_to(ctx)
interface = PaginatorInterface(ctx.bot, paginator, owner=ctx.author)
await interface.send_to(ctx)

0 comments on commit 9099fe0

Please sign in to comment.