Source code for discord.ext.forms.forms

import discord
from discord.ext import commands
from typing import List
[docs]class Form(object): """The basic form object. Parameters: ----------- ctx (discord.ext.commands.Context): The context of the form. title (str): The title of the form. """ def __init__(self, ctx:commands.Context, title): self._ctx = ctx self._bot = ctx.bot self._questions = [] self.title = title self.timeout = 120 self.editanddelete = False self.color = None
[docs] def set_timeout(self,timeout:int) -> None: """Sets the timeout for the form. Args: ----- timeout (int): The timeout to be used. """ self.timeout = timeout
[docs] def add_question(self,question,qtype=None) -> List[dict]: """Adds a question to the form. Parameters ---------- question : str The question as a string that should be added. qtype : str, optional The input validation to be used (incomplete), by default None Returns ------- List[dict] A list of all of the questions, stored as dictionaries. Raises ------ InvalidFormType Is raised when the input validation type is invalid. """ dictionary = {'question':question} if qtype: if qtype.lower() == 'invite': pass elif qtype.lower() == 'channel': pass elif qtype.lower() == 'user': pass else: raise InvalidFormType(f"Type '{qtype}' is invalid!") dictionary['type'] = qtype self._questions.append(dictionary) return self._questions
[docs] def edit_and_delete(self,choice:bool=None) -> bool: """Set's whether the bot should edit the prompt and delete the input messages. """ if choice == None: if self.editanddelete == True: self.editanddelete = False return False else: self.editanddelete = True return True else: self.editanddelete = choice
[docs] async def set_color(self,color:discord.Color) -> None: """Sets the color of the form embeds.""" try: color = await commands.ColorConverter().convert(self._ctx,color) except Exception as e: raise InvalidColor(e) self.color = color
[docs] async def start(self,channel=None) -> List[dict]: """Starts the form in the specified channel. If none is specified, the channel will be fetched from the `context` parameter of the form's initialization.""" elist = [] answers = [] if not channel: channel = self._ctx.channel for q in self._questions: embed=discord.Embed(description=q['question']) embed.set_author(name=f"{self.title}: {self._questions.index(q)+1}/{len(self._questions)}",icon_url=self._bot.user.avatar_url) if self.color: embed.color=self.color elist.append(embed) prompt = None for embed in elist: if self.editanddelete: if not prompt: prompt = await self._ctx.channel.send(embed=embed) else: await prompt.edit(embed=embed) else: prompt = await self._ctx.channel.send(embed=embed) def check(m): return m.channel == prompt.channel and m.author == self._ctx.author question = [x for x in self._questions if x['question'] == embed.description] question = question[0] msg = await self._bot.wait_for('message',check=check,timeout=self.timeout) question['answer'] = msg.content self._questions[self._questions.index(question)] = question if 'type' in question.keys(): # TODO: Add input validation #if question['type'] == 'invite' pass return self._questions
[docs]class InvalidFormType(Exception): """The exception raised when a form type is invalid.""" pass
[docs]class InvalidColor(Exception): """The exception raised when the color type is invalid.""" pass