from redbot.core import Config, commands from collections.abc import Iterable import validators as vals import logging import discord log = logging.getLogger("red.rpg") class Check: def __init__(self, ctx_or_interaction, custom: Iterable = None, length: int = None): self.ctx_or_interaction = ctx_or_interaction # Store the context or interaction object self.custom = custom self.length = length def _get_author(self): """ Helper function to get the author from either ctx or interaction. """ if isinstance(self.ctx_or_interaction, commands.Context): return self.ctx_or_interaction.author elif isinstance(self.ctx_or_interaction, discord.Interaction): return self.ctx_or_interaction.user else: log.error(f"Unexpected object type in Check._get_author: {type(self.ctx_or_interaction)}") return None def same(self, m): author = self._get_author() if author is None: return False # Handle the case where author couldn't be determined if isinstance(m, discord.Message): return author == m.author elif isinstance(m, discord.Interaction): return author == m.user else: log.error(f"Unexpected object type in Check.same: {type(m)}") return False def comfirm(self, m): return self.same(m) and m.content.lower() in ("yes", "no") def valid_int(self, m): return self.same and m.content.isdigit() def valid_float(self, m): try: return self.same(m) and float(m.content) >= 1 except ValueError: return False def positive(self, m): return self.same(m) and m.content.isdigit() and int(m.content) > 0 def role(self, m): roles = [r.name for r in self.ctx.guild.roles if r.name != "Bot"] return self.same(m) and m.content in roles def member(self, m): return self.same(m) and m.content in [x.name for x in self.ctx.guild.members] def length_under(self, m): try: if isinstance(m, discord.Message): content = m.content elif isinstance(m, discord.Interaction): content = m.data['components'][0]['components'][0]['value'] # Access modal input value else: raise ValueError("Unsupported object type for length_under check") return self.same(m) and len(content) <= self.length except TypeError: raise ValueError("Length was not specified in Check") def valid_image_url(self, m): url = m.content.strip() if not vals.url(url): return False valid_extensions = (".jpg", ".jpeg", ".png", ".gif") if not any(url.lower().endswith(ext) for ext in valid_extensions): return False return True def content(self, m): try: return self.same(m) and m.content in self.custom except TypeError: raise ValueError("A custom iterable was not set in Check")