MCPcopy Create free account
hub / github.com/AAA3A-AAA3A/AAA3A-cogs / get_code_from_context

Method get_code_from_context

runcode/runcode.py:252–372  ·  view source on GitHub ↗
(
        self,
        ctx: commands.Context,
        code: str | None,
        provided_language: str | None,
    )

Source from the content-addressed store, hash-verified

250 )
251
252 async def get_code_from_context(
253 self,
254 ctx: commands.Context,
255 code: str | None,
256 provided_language: str | None,
257 ) -> tuple[str, str]:
258 _language = provided_language
259 _code = None
260
261 if ctx.message.attachments:
262 file = ctx.message.attachments[0]
263 if file.size > 20000:
264 raise commands.UserFeedbackCheckFailure(_("File must be smaller than 20 kio."))
265 buffer = io.BytesIO()
266 await ctx.message.attachments[0].save(buffer)
267 code = buffer.read().decode("utf-8")
268 if (
269 _language is None
270 and (language_extension := ctx.message.attachments[0].filename.split(".")[-1])
271 != "txt"
272 ):
273 for language in LANGUAGES_FILES_EXTENSIONS:
274 if language_extension in LANGUAGES_FILES_EXTENSIONS[language]:
275 _language = language
276 break
277 _language = ctx.message.attachments[0].filename.split(".")[-1]
278 elif code is not None:
279 if code.strip().startswith("url="):
280 url = code[4:].strip()
281 regex_gist = r"^https:\/\/gist\.github\.com\/([a-zA-Z0-9_-]+)\/([a-zA-Z0-9]+)$"
282 regex_pastebin = r"^https:\/\/(www\.)?pastebin\.com\/(raw\/)?([a-zA-Z0-9]+)$"
283 if (match := re.match(regex_gist, url)) is not None:
284 __, gist_id = match.groups()
285 api_url = f"https://api.github.com/gists/{gist_id}"
286 async with self._session.get(api_url) as r:
287 response = await r.json()
288 code = response["files"][f"{gist_id}.txt"]["content"]
289 elif (match := re.match(regex_pastebin, url)) is not None:
290 paste_id = match[3]
291 api_url = f"https://pastebin.com/raw/{paste_id}"
292 async with self._session.get(api_url) as r:
293 code = await r.text()
294 else:
295 api_url = url
296 async with self._session.get(api_url) as r:
297 code = await r.text()
298 else:
299 raise commands.UserFeedbackCheckFailure(_("Please provide the code!"))
300
301 if ctx.interaction is None:
302 begin = code.find("```")
303 language_identifier = code[begin + 3 : code[begin + 3 :].find("\n") + begin + 3].lower()
304 no_code = False
305 try:
306 end = code[begin + 3 + len(language_identifier) :].rfind("```")
307 except IndexError:
308 no_code = True
309 if begin == -1 or end == -1:

Callers 2

runcodeMethod · 0.95
runtioMethod · 0.95

Calls 5

textMethod · 0.80
replaceMethod · 0.80
_Function · 0.50
saveMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected