Skip to content

Commit ae2a8fc

Browse files
committed
📦 Switch ConfirmView to components v2
This rly sucks to code ngl
1 parent 4ef79a8 commit ae2a8fc

File tree

6 files changed

+82
-66
lines changed

6 files changed

+82
-66
lines changed

‎killua/cogs/cards.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,12 @@ async def _sell_confirm_view(
284284
Asks the user if they want to sell the cards.
285285
Returns True if the user wants to sell the cards, False if they don't
286286
"""
287-
view = ConfirmButton(ctx.author.id, timeout=80)
288-
msg = await ctx.send(
287+
view = ConfirmButton(
288+
ctx.author.id,
289289
f"You will receive {to_be_gained} Jenny for selling {selling_what} cards, do you want to proceed?",
290+
timeout=80,
291+
)
292+
msg = await ctx.send(
290293
view=view,
291294
)
292295
await view.wait()
@@ -649,9 +652,12 @@ async def discard(self, ctx: commands.Context, card: str):
649652
if card.id == 0:
650653
return await ctx.send("You cannot discard this card!")
651654

652-
view = ConfirmButton(ctx.author.id, timeout=20)
653-
msg = await ctx.send(
655+
view = ConfirmButton(
656+
ctx.author.id,
654657
f"Do you really want to throw this card away? (if you want to throw a fake aware, make sure it's in the free slots (unless it's the only copy you own. You can switch cards between free and restricted slots with `{(await self.client.command_prefix(self.client, ctx.message))[2]}swap <card_id>`)",
658+
timeout=20,
659+
)
660+
msg = await ctx.send(
655661
view=view,
656662
allowed_mentions=discord.AllowedMentions.none(),
657663
)

‎killua/cogs/games.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,11 @@ async def play_against(
411411
)
412412

413413
if not replay:
414-
view = ConfirmButton(other.id)
415-
msg = await self.ctx.send(
414+
view = ConfirmButton(
415+
other.id,
416416
f"{other.mention}, {self.ctx.author.mention} wants to play a trivia against you. They chose the category `{next((t for t, v in TRIVIA_TOPICS.items() if v == self.category)) if self.category else 'Random category'}` with a difficulty of `{self.difficulty}`, playing for {jenny} jenny. Do you accept?",
417+
)
418+
msg = await self.ctx.send(
417419
view=view,
418420
)
419421
await view.wait()
@@ -646,9 +648,12 @@ async def multiplayer(self, replay: bool = False) -> Union[None, discord.Message
646648
if await blcheck(self.other.id) is True:
647649
return await self.ctx.send("You can't play against someone blacklisted")
648650

649-
view = ConfirmButton(self.other.id, timeout=80)
650-
msg = await self.ctx.send(
651+
view = ConfirmButton(
652+
self.other.id,
651653
f"{self.ctx.author.mention} challenged {self.other.mention} to a game of Rock Paper Scissors! Will **{self.other}** accept the challange?",
654+
timeout=80,
655+
)
656+
msg = await self.ctx.send(
652657
view=view,
653658
)
654659
await view.wait()

‎killua/cogs/shop.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -528,9 +528,8 @@ async def _todo(
528528
if todo_list.spots >= 100:
529529
return await ctx.send("You can't buy more than 100 spots")
530530

531-
view = ConfirmButton(ctx.author.id, timeout=10)
531+
view = ConfirmButton(ctx.author.id, f"Do you want to buy 10 more to-do spots for this list? \nCurrent spots: {todo_list.spots} \nCost: {int(todo_list.spots*100*0.5)} points", timeout=10)
532532
msg = await ctx.send(
533-
f"Do you want to buy 10 more to-do spots for this list? \nCurrent spots: {todo_list.spots} \nCost: {int(todo_list.spots*100*0.5)} points",
534533
view=view,
535534
)
536535
await view.wait()

‎killua/cogs/todo.py

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,7 @@ async def create(
297297
):
298298
"""Lets you create your todo list in an interactive menu"""
299299

300-
user_todo_lists = [
301-
x async for x in DB.todo.find({"owner": ctx.author.id})
302-
]
300+
user_todo_lists = [x async for x in DB.todo.find({"owner": ctx.author.id})]
303301

304302
if len(user_todo_lists) == 5:
305303
return await ctx.send(
@@ -866,26 +864,31 @@ async def invite(
866864
"The user you are trying to invite has their dms closed, they need to open them to accept the invitation"
867865
)
868866

869-
embed = discord.Embed.from_dict(
870-
{
871-
"title": f"You were invited to to-do list {todo_list.name} (ID: {todo_list.id})",
872-
"description": f'{ctx.author} invited you to be {role} in their to-do list. To accept, click "confirm", to deny click "cancel". If this invitation was inappropriate, click "report"',
873-
"color": todo_list.color or 0x3E4A78,
874-
"footer": {
875-
"icon_url": str(ctx.author.avatar.url),
876-
"text": f"Requested by {ctx.author}",
877-
},
878-
}
879-
)
867+
# embed = discord.Embed.from_dict(
868+
# {
869+
# "title": f"You were invited to to-do list {todo_list.name} (ID: {todo_list.id})",
870+
# "description": f'{ctx.author} invited you to be {role} in their to-do list. To accept, click "confirm", to deny click "cancel". If this invitation was inappropriate, click "report"',
871+
# "color": todo_list.color or 0x3E4A78,
872+
# "footer": {
873+
# "icon_url": str(ctx.author.avatar.url),
874+
# "text": f"Requested by {ctx.author}",
875+
# },
876+
# }
877+
# )
880878

881879
try:
882-
view = ConfirmButton(user.id, timeout=60 * 60 * 24)
883-
view.add_item(
880+
view = ConfirmButton(
881+
user.id,
882+
f"# You were invited to to-do list {todo_list.name} (ID: {todo_list.id})\n"
883+
+ f'{ctx.author} invited you to be {role} in their to-do list. To accept, click "confirm", to deny click "cancel". If this invitation was inappropriate, click "report"',
884+
timeout=60 * 60 * 24,
885+
)
886+
view._children[0]._children[1].add_item(
884887
Button(
885-
label="Report", custom_id="report", style=discord.ButtonStyle.red
888+
label="report", custom_id="report", style=discord.ButtonStyle.red
886889
)
887890
)
888-
msg = await user.send(embed=embed, view=view)
891+
msg = await user.send(view=view)
889892
await ctx.send(
890893
"Successfully send the invitation to the specified user! They have 24 hours to accept or deny"
891894
)

‎killua/static/cards.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ async def exec(self, member: discord.Member) -> None:
5454
self._has_cards_check(other.fs_cards, " in their free slots", uses_up=True)
5555

5656
async def make_embed(page, *_):
57-
return await Book(
58-
cast(BaseBot, self.ctx.bot)
59-
).create(member, page,True)
57+
return await Book(cast(BaseBot, self.ctx.bot)).create(member, page, True)
6058

6159
await Paginator(
6260
self.ctx,
@@ -87,9 +85,7 @@ async def exec(self, member: discord.Member) -> None:
8785
await self._view_defense_check(self.ctx, other)
8886

8987
async def make_embed(page, *_):
90-
return await Book(
91-
cast(BaseBot, self.ctx.bot)
92-
).create(member, page)
88+
return await Book(cast(BaseBot, self.ctx.bot)).create(member, page)
9389

9490
await Paginator(self.ctx, max_pages=6, func=make_embed, has_file=True).start()
9591

@@ -203,9 +199,7 @@ async def exec(self, member: discord.Member) -> None:
203199
self._has_cards_check(other.all_cards)
204200

205201
async def make_embed(page, embed, pages):
206-
return await Book(
207-
cast(BaseBot, self.ctx.bot)
208-
).create(member, page)
202+
return await Book(cast(BaseBot, self.ctx.bot)).create(member, page)
209203

210204
return await Paginator(
211205
self.ctx,
@@ -339,9 +333,11 @@ async def exec(self) -> None:
339333
raise CheckFailure(
340334
"You don't have another copy of this card to renew the effect"
341335
)
342-
view = ConfirmButton(user_id=self.ctx.author.id)
343-
msg = await self.ctx.send(
336+
view = ConfirmButton(
337+
self.ctx.author.id,
344338
f"You still have {author.has_effect(str(self.id))[1]} protections left. Do you really want to use this card now and overwrite the current protection?",
339+
)
340+
msg = await self.ctx.send(
345341
view=view,
346342
)
347343
await view.wait()

‎killua/utils/interactions.py

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ async def disable(self, msg: discord.Message) -> Union[discord.Message, None]:
4242
try:
4343
await msg.edit(view=self)
4444
except discord.HTTPException:
45-
pass # Idk why but this can be Forbidden
45+
pass # Idk why but this can be Forbidden
4646

4747

4848
class Modal(discord.ui.Modal): # lgtm [py/missing-call-to-init]
@@ -89,18 +89,46 @@ async def callback(self, _: discord.Interaction):
8989
self.view.value = self.custom_id
9090
self.view.stop()
9191

92+
class ConfirmButtonRow(discord.ui.ActionRow):
93+
def __init__(self, view: 'ConfirmButton') -> None:
94+
self.__view = view
95+
super().__init__()
9296

93-
class ConfirmButton(discord.ui.View):
97+
@discord.ui.button(
98+
label="confirm", style=discord.ButtonStyle.green, custom_id="confirm"
99+
)
100+
async def confirm(self, *_):
101+
print("Confirming")
102+
self.__view.value = True
103+
self.__view.timed_out = False
104+
self.__view.stop()
105+
106+
@discord.ui.button(label="cancel", style=discord.ButtonStyle.red, custom_id="cancel")
107+
async def cancel(self, *_):
108+
self.__view.value = False
109+
self.__view.timed_out = False
110+
self.__view.stop()
111+
112+
class ConfirmButton(discord.ui.LayoutView):
94113
"""A button that is used to confirm a certain action or deny it"""
95114

96-
def __init__(self, user_id: int, **kwargs):
115+
def __init__(self, user_id: int, text: str, **kwargs):
97116
super().__init__(**kwargs)
98117
self.user_id = user_id
99118
self.timed_out = (
100119
False # helps subclasses using Button to have set this to False
101120
)
102121
self.interaction = None
103122
self.value = False
123+
self.buttons = ConfirmButtonRow(self)
124+
container = discord.ui.Container(
125+
discord.ui.TextDisplay(
126+
content=text,
127+
),
128+
self.buttons,
129+
accent_colour=discord.Colour.blurple(),
130+
)
131+
self.add_item(container)
104132

105133
async def interaction_check(self, interaction: discord.Interaction) -> bool:
106134
if not (val := interaction.user.id == self.user_id):
@@ -109,37 +137,16 @@ async def interaction_check(self, interaction: discord.Interaction) -> bool:
109137
return val
110138

111139
async def disable(self, msg: discord.Message) -> discord.Message:
112-
if (
113-
len([c for c in self.children if not c.disabled]) == 0
114-
): # if every child is already disabled, we don't need to edit the message again
115-
return
116-
117-
for child in self.children:
140+
for child in self._children[0]._children[1]._children:
141+
# I tried to do this more dynamically but it didn't work
118142
child.disabled = True
119-
120143
if self.interaction and not self.interaction.response.is_done():
121144
await self.interaction.response.edit_message(view=self)
122145
else:
123146
try:
124147
await msg.edit(view=self)
125148
except discord.HTTPException:
126-
pass # Idk why but this can be Forbidden
149+
pass # Idk why but this can be Forbidden
127150

128151
async def on_timeout(self):
129152
self.timed_out = True
130-
131-
@discord.ui.button(
132-
label="confirm", style=discord.ButtonStyle.green, custom_id="confirm"
133-
)
134-
async def confirm(self, *_):
135-
self.value = True
136-
self.timed_out = False
137-
self.stop()
138-
139-
@discord.ui.button(
140-
label="cancel", style=discord.ButtonStyle.red, custom_id="cancel"
141-
)
142-
async def cancel(self, *_):
143-
self.value = False
144-
self.timed_out = False
145-
self.stop()

0 commit comments

Comments
 (0)