2024-08-19 17:57:26 -04:00
|
|
|
from discord import ChannelType, Forbidden, Guild, HTTPException, InvalidData, NotFound, Role, User
|
|
|
|
from discord.abc import Messageable
|
2024-05-06 17:23:59 -04:00
|
|
|
from redbot.core.bot import Red
|
|
|
|
|
2024-05-09 21:27:26 -04:00
|
|
|
from .base import AuroraBaseModel, AuroraGuildModel
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PartialUser(AuroraBaseModel):
|
|
|
|
id: int
|
|
|
|
username: str
|
|
|
|
discriminator: int
|
2024-08-19 17:57:26 -04:00
|
|
|
_obj: User | None
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
return f"{self.username}#{self.discriminator}" if self.discriminator != 0 else self.username
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
async def from_id(cls, bot: Red, user_id: int) -> "PartialUser":
|
|
|
|
user = bot.get_user(user_id)
|
|
|
|
if not user:
|
|
|
|
try:
|
|
|
|
user = await bot.fetch_user(user_id)
|
2024-08-19 17:57:26 -04:00
|
|
|
return cls(bot=bot, id=user.id, username=user.name, discriminator=user.discriminator, _obj=user)
|
2024-05-06 17:23:59 -04:00
|
|
|
except NotFound:
|
2024-08-19 17:57:26 -04:00
|
|
|
return cls(bot=bot, id=user_id, username="Deleted User", discriminator=0, _obj=None)
|
|
|
|
return cls(bot=bot, id=user.id, username=user.name, discriminator=user.discriminator, _obj=user)
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
|
|
|
|
class PartialChannel(AuroraGuildModel):
|
|
|
|
id: int
|
|
|
|
name: str
|
2024-06-05 23:13:23 -04:00
|
|
|
type: ChannelType
|
2024-08-19 17:57:26 -04:00
|
|
|
_obj: Messageable | None
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def mention(self):
|
|
|
|
if self.name in ["Deleted Channel", "Forbidden Channel"]:
|
|
|
|
return self.name
|
|
|
|
return f"<#{self.id}>"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.mention
|
|
|
|
|
|
|
|
@classmethod
|
2024-06-05 23:13:23 -04:00
|
|
|
async def from_id(cls, bot: Red, channel_id: int, guild: Guild) -> "PartialChannel":
|
2024-05-06 17:23:59 -04:00
|
|
|
channel = bot.get_channel(channel_id)
|
|
|
|
if not channel:
|
|
|
|
try:
|
|
|
|
channel = await bot.fetch_channel(channel_id)
|
2024-08-19 17:57:26 -04:00
|
|
|
return cls(bot=bot, guild_id=channel.guild.id, guild=guild, id=channel.id, name=channel.name, type=channel.type, _obj=channel)
|
2024-05-06 17:23:59 -04:00
|
|
|
except (NotFound, InvalidData, HTTPException, Forbidden) as e:
|
|
|
|
if e == Forbidden:
|
|
|
|
return cls(bot=bot, guild_id=0, id=channel_id, name="Forbidden Channel")
|
2024-08-19 17:57:26 -04:00
|
|
|
return cls(bot=bot, guild_id=0, id=channel_id, name="Deleted Channel", type=ChannelType.text, _obj=None)
|
|
|
|
return cls(bot=bot, guild_id=channel.guild.id, guild=guild, id=channel.id, name=channel.name, type=channel.type, _obj=channel)
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
class PartialRole(AuroraGuildModel):
|
|
|
|
id: int
|
|
|
|
name: str
|
2024-08-19 17:57:26 -04:00
|
|
|
_obj: Role | None
|
2024-05-06 17:23:59 -04:00
|
|
|
|
|
|
|
@property
|
|
|
|
def mention(self):
|
|
|
|
if self.name in ["Deleted Role", "Forbidden Role"]:
|
|
|
|
return self.name
|
|
|
|
return f"<@&{self.id}>"
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.mention
|
|
|
|
|
|
|
|
@classmethod
|
2024-06-05 23:13:23 -04:00
|
|
|
async def from_id(cls, bot: Red, guild: Guild, role_id: int) -> "PartialRole":
|
2024-05-06 17:23:59 -04:00
|
|
|
role = guild.get_role(role_id)
|
|
|
|
if not role:
|
2024-08-19 17:57:26 -04:00
|
|
|
return cls(bot=bot, guild_id=guild.id, id=role_id, name="Deleted Role", _obj=None)
|
|
|
|
return cls(bot=bot, guild_id=guild.id, id=role.id, name=role.name, _obj=role)
|