Skip to content
This repository was archived by the owner on Sep 9, 2024. It is now read-only.

Commit e79acf8

Browse files
committed
Add format cmd
1 parent 6bc6a73 commit e79acf8

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

_orangcbot/extensions/format.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from __future__ import annotations
2+
3+
import nextcord
4+
from nextcord.ext import commands
5+
from nextcord import Message, message_command, slash_command, SlashOption, Interaction
6+
7+
8+
from nextcord import ui
9+
10+
import black
11+
12+
13+
class FormatModal(ui.Modal):
14+
def __init__(self) -> None:
15+
self.code = ui.TextInput(
16+
label="Code to format",
17+
style=nextcord.TextInputStyle.paragraph,
18+
required=True,
19+
)
20+
self.add_item(self.code)
21+
super().__init__(title="Format code", timeout=900)
22+
23+
async def callback(self, interaction: Interaction) -> None:
24+
await interaction.response.defer()
25+
response = black.format_str(self.code.value) # type: ignore -- it is required lmao
26+
await interaction.send(
27+
embed=nextcord.Embed(
28+
title="Formatted code",
29+
description=f"`{response}`",
30+
color=nextcord.Color.green(),
31+
)
32+
)
33+
34+
35+
class Format(commands.Cog):
36+
def __init__(self, bot: commands.Bot) -> None:
37+
self._bot: commands.Bot = bot
38+
39+
@slash_command()
40+
async def format_code(self, interaction: Interaction) -> None: # type: ignore
41+
pass
42+
43+
@format_code.subcommand(name="black")
44+
async def black_(
45+
self,
46+
interaction: Interaction,
47+
code=SlashOption(description="Code to format", required=False),
48+
) -> None:
49+
"""Format code with Black."""
50+
if not code:
51+
return await interaction.response.send_modal(FormatModal())
52+
await interaction.response.defer()
53+
54+
response = black.format_str(code, mode=black.Mode())
55+
await interaction.send(
56+
embed=nextcord.Embed(
57+
title="Formatted code",
58+
description=f"```{response}```",
59+
color=nextcord.Color.green(),
60+
)
61+
)
62+
63+
64+
def setup(bot: commands.Bot):
65+
bot.add_cog(Format(bot))
66+
return

0 commit comments

Comments
 (0)