Skip to content

Commit d633fbc

Browse files
committed
SNIPPET: Add basic snippet-releated views.
1 parent ae250ef commit d633fbc

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

bot/libs/snippets/views.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
from libs.utils import RoboView, GuildContext
2+
import discord.ui
3+
4+
from rodhaj import Rodhaj
5+
6+
7+
class SnippetCreationModal(discord.ui.Modal, title="Editing Snippet"):
8+
content = discord.ui.TextInput(
9+
label="Snippet message",
10+
placeholder="Call me Ishmael. Some years ago—never mind "
11+
+ "how long precisely...",
12+
style=discord.TextStyle.paragraph,
13+
)
14+
15+
def __init__(self, bot: Rodhaj, context: GuildContext, name: str):
16+
super().__init__(timeout=12 * 3600)
17+
self._bot = bot
18+
self._ctx = context
19+
self._snippet_name = name
20+
self.title = f"Creating Snippet {name}"
21+
22+
async def on_submit(self, interaction: discord.Interaction):
23+
await interaction.response.defer()
24+
self._bot.dispatch(
25+
"snippet_create",
26+
self._ctx.guild,
27+
self._ctx.author,
28+
self._snippet_name,
29+
self.content.value,
30+
self._ctx,
31+
)
32+
self.stop()
33+
34+
35+
class SnippetPreCreationConfirmationView(discord.ui.View):
36+
def __init__(self, bot: Rodhaj, ctx: GuildContext, snippet_name: str, timeout=15):
37+
super().__init__(timeout=timeout)
38+
self._bot = bot
39+
self._ctx = ctx
40+
self._snippet_name = snippet_name
41+
42+
@discord.ui.button(label="Create Snippet", style=discord.ButtonStyle.green)
43+
async def create_snippet(
44+
self, interaction: discord.Interaction, button: discord.ui.Button
45+
):
46+
if interaction.user.id != self._ctx.author.id:
47+
return
48+
button.disabled = True
49+
modal = SnippetCreationModal(self._bot, self._ctx, self._snippet_name)
50+
await interaction.response.send_modal(modal)
51+
await interaction.edit_original_response(
52+
content="Creating Snippet...", view=None
53+
)
54+
await modal.wait()
55+
await interaction.delete_original_response()
56+
self.stop()
57+
58+
async def on_timeout(self):
59+
self.clear_items()
60+
self.stop()
61+
62+
63+
class SnippetInfoView(RoboView):
64+
pass

0 commit comments

Comments
 (0)