143 lines
5.4 KiB
Python
143 lines
5.4 KiB
Python
import discord
|
|
from typing import Literal
|
|
|
|
class Confirm(discord.ui.View):
|
|
def __init__(self, responders: list, timeout: float = 300.0, label_type: Literal['yes', 'confirm'] = 'confirm'):
|
|
super().__init__(timeout=timeout)
|
|
if not isinstance(responders, list):
|
|
raise TypeError('responders must be a list')
|
|
self.value = None
|
|
self.responders = responders
|
|
if label_type == 'yes':
|
|
self.confirm.label = 'Yes'
|
|
self.cancel.label = 'No'
|
|
|
|
# When the confirm button is pressed, set the inner value to `True` and
|
|
# stop the View from listening to more input.
|
|
# We also send the user an ephemeral message that we're confirming their choice.
|
|
@discord.ui.button(label='Confirm', style=discord.ButtonStyle.green)
|
|
async def confirm(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = True
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
# This one is similar to the confirmation button except sets the inner value to `False`
|
|
@discord.ui.button(label='Cancel', style=discord.ButtonStyle.grey)
|
|
async def cancel(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = False
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
|
|
class ButtonOptions(discord.ui.View):
|
|
def __init__(self, responders: list, timeout: float = 300.0, labels=None):
|
|
super().__init__(timeout=timeout)
|
|
if not isinstance(responders, list):
|
|
raise TypeError('responders must be a list')
|
|
self.value = None
|
|
self.responders = responders
|
|
self.options = labels
|
|
for count, x in enumerate(labels):
|
|
if count == 0:
|
|
self.option1.label = x
|
|
if x is None or x.lower() == 'na' or x == 'N/A':
|
|
self.remove_item(self.option1)
|
|
if count == 1:
|
|
self.option2.label = x
|
|
if x is None or x.lower() == 'na' or x == 'N/A':
|
|
self.remove_item(self.option2)
|
|
if count == 2:
|
|
self.option3.label = x
|
|
if x is None or x.lower() == 'na' or x == 'N/A':
|
|
self.remove_item(self.option3)
|
|
if count == 3:
|
|
self.option4.label = x
|
|
if x is None or x.lower() == 'na' or x == 'N/A':
|
|
self.remove_item(self.option4)
|
|
if count == 4:
|
|
self.option5.label = x
|
|
if x is None or x.lower() == 'na' or x == 'N/A':
|
|
self.remove_item(self.option5)
|
|
|
|
@discord.ui.button(label='Option 1', style=discord.ButtonStyle.primary)
|
|
async def option1(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = self.options[0]
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
@discord.ui.button(label='Option 2', style=discord.ButtonStyle.primary)
|
|
async def option2(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = self.options[1]
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
@discord.ui.button(label='Option 3', style=discord.ButtonStyle.primary)
|
|
async def option3(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = self.options[2]
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
@discord.ui.button(label='Option 4', style=discord.ButtonStyle.primary)
|
|
async def option4(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = self.options[3]
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
@discord.ui.button(label='Option 5', style=discord.ButtonStyle.primary)
|
|
async def option5(self, interaction: discord.Interaction, button: discord.ui.Button):
|
|
if interaction.user not in self.responders:
|
|
return
|
|
|
|
self.value = self.options[4]
|
|
self.clear_items()
|
|
self.stop()
|
|
|
|
|
|
async def ask_confirm(interaction: discord.Interaction, question: str, label_type: Literal['yes', 'confirm'] = 'confirm', timeout: int = 60, delete_question: bool = True, custom_confirm_label: str = None, custom_cancel_label: str = None, embed: discord.Embed = None, delete_embed: bool = False) -> bool:
|
|
"""
|
|
button_callbacks: keys are button values, values are async functions
|
|
"""
|
|
try:
|
|
view = Confirm(responders=[interaction.user], timeout=timeout, label_type=label_type)
|
|
except AttributeError:
|
|
view = Confirm(responders=[interaction.author], timeout=timeout, label_type=label_type)
|
|
if custom_confirm_label:
|
|
view.confirm.label = custom_confirm_label
|
|
if custom_cancel_label:
|
|
view.cancel.label = custom_cancel_label
|
|
|
|
question = await interaction.channel.send(question, view=view)
|
|
await view.wait()
|
|
|
|
if view.value:
|
|
if delete_question:
|
|
await question.delete()
|
|
else:
|
|
await question.edit(content=question, view=None)
|
|
return True
|
|
|
|
else:
|
|
if delete_question:
|
|
await question.delete()
|
|
else:
|
|
await question.edit(content=question, view=None)
|
|
return False
|