-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix views timing out issue * Implement deletion process * Add error handler * Implement subclassed `commands.Context` (`RoboContext`)" * Include docs deps and align to best practices
- Loading branch information
Showing
11 changed files
with
840 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Optional | ||
|
||
import discord | ||
from discord.ext import commands | ||
|
||
from .views import RoboView | ||
|
||
if TYPE_CHECKING: | ||
from bot.rodhaj import Rodhaj | ||
|
||
|
||
class ConfirmationView(RoboView): | ||
def __init__(self, ctx, timeout: float, delete_after: bool): | ||
super().__init__(ctx, timeout) | ||
self.value: Optional[bool] = None | ||
self.delete_after = delete_after | ||
self.message: Optional[discord.Message] = None | ||
|
||
async def on_timeout(self) -> None: | ||
if self.delete_after and self.message: | ||
await self.message.delete() | ||
elif self.message: | ||
await self.message.edit(view=None) | ||
|
||
async def delete_response(self, interaction: discord.Interaction): | ||
await interaction.response.defer() | ||
if self.delete_after: | ||
await interaction.delete_original_response() | ||
|
||
self.stop() | ||
|
||
@discord.ui.button( | ||
label="Confirm", | ||
style=discord.ButtonStyle.green, | ||
emoji="<:greenTick:596576670815879169>", | ||
) | ||
async def confirm( | ||
self, interaction: discord.Interaction, button: discord.ui.Button | ||
) -> None: | ||
self.value = True | ||
await self.delete_response(interaction) | ||
|
||
@discord.ui.button( | ||
label="Cancel", | ||
style=discord.ButtonStyle.red, | ||
emoji="<:redTick:596576672149667840>", | ||
) | ||
async def cancel( | ||
self, interaction: discord.Interaction, button: discord.ui.Button | ||
) -> None: | ||
self.value = False | ||
await interaction.response.defer() | ||
await interaction.delete_original_response() | ||
self.stop() | ||
|
||
|
||
class RoboContext(commands.Context): | ||
bot: Rodhaj | ||
|
||
def __init__(self, **kwargs): | ||
super().__init__(**kwargs) | ||
|
||
async def prompt( | ||
self, message: str, *, timeout: float = 60.0, delete_after: bool = False | ||
) -> Optional[bool]: | ||
view = ConfirmationView(ctx=self, timeout=timeout, delete_after=delete_after) | ||
view.message = await self.send(message, view=view, ephemeral=delete_after) | ||
await view.wait() | ||
return view.value |
Oops, something went wrong.