GoldyBot.objects.channel

 1from __future__ import annotations
 2import nextcord
 3
 4import GoldyBot
 5from GoldyBot.objects.slash import Interaction, InteractionToCtx
 6
 7MODULE_NAME = "CHANNEL"
 8
 9class Channel():
10    """A class representing a discord channel in Goldy Bot."""
11    def __init__(self, ctx, channel_id:str|int=None, mention_str:str=None, channel_object:nextcord.abc.GuildChannel=None):
12
13        self.ctx = ctx
14        self.channel_id_ = channel_id
15        self.mention_str_ = mention_str
16        self.channel_object_ = channel_object
17
18        self.channel:nextcord.abc.GuildChannel|None
19
20        if self.channel_object_ == None:
21            # Find the channel.
22            self.channel = self.find_channel(self.channel_id)
23        else:
24            self.channel = self.channel_object_
25        
26    @property
27    def channel_id(self) -> str:
28        """Returns id of discord channel. Defaults to ctx channel if ``channel_id``, ``mention_str`` and ``channel_object`` are None."""
29        if not self.channel_id_ == None: return str(self.channel_id_)
30        if not self.mention_str_ == None: return self.mention_str_[3:-1]
31        if not self.channel_object_ == None: return str(self.channel_object_.id)
32        else:
33            if isinstance(self.ctx, Interaction):
34                return str(InteractionToCtx(self.ctx).channel.id)
35            else:
36                return str(self.ctx.channel.id)
37                
38    @property
39    def name(self):
40        """Returns the discord channel name."""
41        return self.channel.name
42
43    @property
44    def display_name(self):
45        """Alias of ``Channel().name``"""
46        return self.name
47
48    async def purge(self, amount:int|None):
49        """Deletes specified number of messages in this channel. (Only works if this channel is a text channel.)"""
50        if isinstance(self.channel, nextcord.TextChannel):
51            channel:nextcord.TextChannel = self.channel
52
53            deleted = None
54            try:
55                deleted = await channel.purge(limit=amount)
56                GoldyBot.logging.log("info_5", f"[{MODULE_NAME}] Deleted '{len(deleted)}' messages from the channel '{self.name}'.")
57            except Exception as e:
58                GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Couldn't delete from '{self.name}' because '{e}'.")
59                
60            return deleted
61        else:
62            GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Can't purge messages because this channel isn't a text channel!")
63
64
65    def find_channel(self, channel_id:int|str) -> nextcord.abc.GuildChannel | None:
66        """Finds the damn channel!"""
67
68        if not channel_id == None:
69            return nextcord.utils.get(self.ctx.guild.channels, id=int(channel_id))
70        else:
71            return None
class Channel:
10class Channel():
11    """A class representing a discord channel in Goldy Bot."""
12    def __init__(self, ctx, channel_id:str|int=None, mention_str:str=None, channel_object:nextcord.abc.GuildChannel=None):
13
14        self.ctx = ctx
15        self.channel_id_ = channel_id
16        self.mention_str_ = mention_str
17        self.channel_object_ = channel_object
18
19        self.channel:nextcord.abc.GuildChannel|None
20
21        if self.channel_object_ == None:
22            # Find the channel.
23            self.channel = self.find_channel(self.channel_id)
24        else:
25            self.channel = self.channel_object_
26        
27    @property
28    def channel_id(self) -> str:
29        """Returns id of discord channel. Defaults to ctx channel if ``channel_id``, ``mention_str`` and ``channel_object`` are None."""
30        if not self.channel_id_ == None: return str(self.channel_id_)
31        if not self.mention_str_ == None: return self.mention_str_[3:-1]
32        if not self.channel_object_ == None: return str(self.channel_object_.id)
33        else:
34            if isinstance(self.ctx, Interaction):
35                return str(InteractionToCtx(self.ctx).channel.id)
36            else:
37                return str(self.ctx.channel.id)
38                
39    @property
40    def name(self):
41        """Returns the discord channel name."""
42        return self.channel.name
43
44    @property
45    def display_name(self):
46        """Alias of ``Channel().name``"""
47        return self.name
48
49    async def purge(self, amount:int|None):
50        """Deletes specified number of messages in this channel. (Only works if this channel is a text channel.)"""
51        if isinstance(self.channel, nextcord.TextChannel):
52            channel:nextcord.TextChannel = self.channel
53
54            deleted = None
55            try:
56                deleted = await channel.purge(limit=amount)
57                GoldyBot.logging.log("info_5", f"[{MODULE_NAME}] Deleted '{len(deleted)}' messages from the channel '{self.name}'.")
58            except Exception as e:
59                GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Couldn't delete from '{self.name}' because '{e}'.")
60                
61            return deleted
62        else:
63            GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Can't purge messages because this channel isn't a text channel!")
64
65
66    def find_channel(self, channel_id:int|str) -> nextcord.abc.GuildChannel | None:
67        """Finds the damn channel!"""
68
69        if not channel_id == None:
70            return nextcord.utils.get(self.ctx.guild.channels, id=int(channel_id))
71        else:
72            return None

A class representing a discord channel in Goldy Bot.

Channel( ctx, channel_id: str | int = None, mention_str: str = None, channel_object: nextcord.abc.GuildChannel = None)
12    def __init__(self, ctx, channel_id:str|int=None, mention_str:str=None, channel_object:nextcord.abc.GuildChannel=None):
13
14        self.ctx = ctx
15        self.channel_id_ = channel_id
16        self.mention_str_ = mention_str
17        self.channel_object_ = channel_object
18
19        self.channel:nextcord.abc.GuildChannel|None
20
21        if self.channel_object_ == None:
22            # Find the channel.
23            self.channel = self.find_channel(self.channel_id)
24        else:
25            self.channel = self.channel_object_
channel_id: str

Returns id of discord channel. Defaults to ctx channel if channel_id, mention_str and channel_object are None.

name

Returns the discord channel name.

display_name

Alias of Channel().name

async def purge(self, amount: int | None):
49    async def purge(self, amount:int|None):
50        """Deletes specified number of messages in this channel. (Only works if this channel is a text channel.)"""
51        if isinstance(self.channel, nextcord.TextChannel):
52            channel:nextcord.TextChannel = self.channel
53
54            deleted = None
55            try:
56                deleted = await channel.purge(limit=amount)
57                GoldyBot.logging.log("info_5", f"[{MODULE_NAME}] Deleted '{len(deleted)}' messages from the channel '{self.name}'.")
58            except Exception as e:
59                GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Couldn't delete from '{self.name}' because '{e}'.")
60                
61            return deleted
62        else:
63            GoldyBot.logging.log("warn", f"[{MODULE_NAME}] Can't purge messages because this channel isn't a text channel!")

Deletes specified number of messages in this channel. (Only works if this channel is a text channel.)

def find_channel(self, channel_id: int | str) -> nextcord.abc.GuildChannel | None:
66    def find_channel(self, channel_id:int|str) -> nextcord.abc.GuildChannel | None:
67        """Finds the damn channel!"""
68
69        if not channel_id == None:
70            return nextcord.utils.get(self.ctx.guild.channels, id=int(channel_id))
71        else:
72            return None

Finds the damn channel!