GoldyBot.internal_modules.v4.timestamps

  1from __future__ import annotations
  2
  3import GoldyBot
  4from GoldyBot.utility.commands import *
  5import pytz
  6
  7AUTHOR = 'Dev Goldy'
  8AUTHOR_GITHUB = 'https://github.com/THEGOLDENPRO'
  9OPEN_SOURCE_LINK = ''
 10
 11class Timestamps(GoldyBot.Extension):
 12    """Timestamps extension."""
 13    def __init__(self, package_module=None):
 14        super().__init__(self, package_module_name=package_module)
 15
 16        self.default_timezone = "Europe/London"
 17
 18        self.unknown_timezone_embed = GoldyBot.Embed(title="❤ Unknown Time Zone!", description="""
 19*This is how the ``timezone`` parameter should be used.*
 20
 21``Europe/London`` = **Uk Time**
 22``America/New_York`` = **New York Time**
 23``Europe/Stockholm`` = **Sweden Time**
 24
 25*The list goes on... To see the full list click [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).*
 26            """, colour=GoldyBot.utility.goldy.colours.RED
 27        ).set_thumbnail(GoldyBot.assets.ANIME_ONE_PUNCH_CLOCK_GIF)
 28
 29    def loader(self):
 30
 31        @GoldyBot.command(help_des="Sends a discord timestamp of that time and date.",slash_cmd_only=True, slash_options={
 32            "date" : GoldyBot.nextcord.SlashOption(description="The date goes here like example, 13.08.2022 or even 2022/08/22.", required=True),
 33            "time" : GoldyBot.nextcord.SlashOption(description="The time goes here like example, 15:00.", required=True),
 34            "flag" : GoldyBot.nextcord.SlashOption(description="Choose a flag.", required=True, choices={
 35                "08/13/2022": "d",
 36                "August 13, 2022": "D",
 37                "6:00 PM": "t",
 38                "6:00:00 PM": "T",
 39                "August 13, 2022 6:00 PM" : "f",
 40                "Saturday, August 13, 2022 6:00 PM" : "F",
 41                "in 3 hours": "R"
 42            }),
 43            "timezone" : GoldyBot.nextcord.SlashOption(description="The timezone to use. Goldy Bot defaults to Europe/London timezone.", required=False)
 44        })
 45        async def timestamp(self:Timestamps, ctx, date, time, flag, timezone=None):
 46            member = GoldyBot.Member(ctx)
 47            datetime = GoldyBot.utility.datetime.user_input.get_time_and_date(f"{date} {time}")
 48            timezone:str|None
 49
 50            if timezone is None:
 51                member_data = await member.get_member_data()
 52                member_saved_timezone = member_data[member.member_id].get("timezone", None)
 53
 54                if member_saved_timezone is None:
 55                    member_data[member.member_id]["timezone"] = self.default_timezone
 56                    await member.edit_member_data(member_data)
 57                    
 58                    timezone = member_data[member.member_id]["timezone"]
 59                else:
 60                    timezone = member_saved_timezone
 61
 62            if not datetime is None:
 63                try:
 64                    # Convert to chosen timezone.
 65                    chosen_timezone = pytz.timezone(timezone)
 66                    datetime = chosen_timezone.normalize(chosen_timezone.localize(datetime, is_dst=True))
 67
 68                    posix_timestamp = int(datetime.timestamp())
 69
 70                    view = CopyButtonView(posix_timestamp, flag)
 71
 72                    await send(ctx, f"<t:{posix_timestamp}:{flag}>", view=view)
 73
 74                    return True
 75
 76                except pytz.UnknownTimeZoneError as e:
 77                    GoldyBot.log("error", e)
 78                    message = await send(ctx, embed=self.unknown_timezone_embed)
 79                    
 80                except Exception as e:
 81                    GoldyBot.log("error", e)
 82
 83                    message = await send(ctx, embed=GoldyBot.utility.goldy.embed.Embed(
 84                        title="❓ Did you enter it correctly?",
 85                        description="I couldn't read either your time or date properly, please could you try again. Perhaps you mistyped something.",
 86                        colour=GoldyBot.utility.goldy.colours.RED
 87                    ))
 88
 89            else:
 90                message = await send(ctx, embed=GoldyBot.utility.goldy.embed.Embed(
 91                        title="❓ Did you enter it correctly?",
 92                        description="I couldn't read either your time or date properly, please could you try again. Perhaps you mistyped something.",
 93                        colour=GoldyBot.utility.goldy.colours.RED
 94                    )
 95                )
 96
 97            await message.delete(delay=30)
 98            return False
 99
100
101        @GoldyBot.command(help_des="Sets default timezone for /timestamp command.", slash_cmd_only=True, slash_options={
102            "timezone" : GoldyBot.nextcord.SlashOption(description="The time zone. Must be like this --> Europe/London, America/New_York, Europe/Stockholm", required=True)
103        })
104        async def timezone_set(self:Timestamps, ctx, timezone):
105            member = GoldyBot.Member(ctx)
106            member_data = await member.get_member_data()
107
108            try:
109                pytz.timezone(timezone)
110                member_data[member.member_id]["timezone"] = timezone
111                await member.edit_member_data(member_data)
112
113                message = await send(ctx, embed=GoldyBot.Embed(
114                    title="💚 Time Zone Set!",
115                    description="Your default timezone was set **UwU**!",
116                    color=GoldyBot.Colours().GREEN
117                ))
118
119                await message.delete(delay=15)
120
121            except pytz.UnknownTimeZoneError as e:
122                GoldyBot.log("error", e)
123
124                message = await send(ctx, embed=self.unknown_timezone_embed)
125
126                await message.delete(delay=30)
127
128
129class CopyButtonView(GoldyBot.nextcord.ui.View):
130    def __init__(self, posix_timestamp:int, flag:str):
131        super().__init__()
132        self.posix_timestamp = posix_timestamp
133        self.flag = flag
134
135        self.response_message:GoldyBot.nextcord.PartialInteractionMessage = None
136
137    @GoldyBot.nextcord.ui.button(label="📋 Copy", style=GoldyBot.nextcord.ButtonStyle.gray)
138    async def copy(self, button: GoldyBot.nextcord.ui.Button, interaction: GoldyBot.nextcord.Interaction):
139        self.response_message = await interaction.response.send_message(f"``<t:{self.posix_timestamp}:{self.flag}>``", ephemeral=True)
140        self.value = True
141
142
143def load():
144    Timestamps(__name__)
class Timestamps(GoldyBot.ext.extensions.Extension):
 12class Timestamps(GoldyBot.Extension):
 13    """Timestamps extension."""
 14    def __init__(self, package_module=None):
 15        super().__init__(self, package_module_name=package_module)
 16
 17        self.default_timezone = "Europe/London"
 18
 19        self.unknown_timezone_embed = GoldyBot.Embed(title="❤ Unknown Time Zone!", description="""
 20*This is how the ``timezone`` parameter should be used.*
 21
 22``Europe/London`` = **Uk Time**
 23``America/New_York`` = **New York Time**
 24``Europe/Stockholm`` = **Sweden Time**
 25
 26*The list goes on... To see the full list click [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).*
 27            """, colour=GoldyBot.utility.goldy.colours.RED
 28        ).set_thumbnail(GoldyBot.assets.ANIME_ONE_PUNCH_CLOCK_GIF)
 29
 30    def loader(self):
 31
 32        @GoldyBot.command(help_des="Sends a discord timestamp of that time and date.",slash_cmd_only=True, slash_options={
 33            "date" : GoldyBot.nextcord.SlashOption(description="The date goes here like example, 13.08.2022 or even 2022/08/22.", required=True),
 34            "time" : GoldyBot.nextcord.SlashOption(description="The time goes here like example, 15:00.", required=True),
 35            "flag" : GoldyBot.nextcord.SlashOption(description="Choose a flag.", required=True, choices={
 36                "08/13/2022": "d",
 37                "August 13, 2022": "D",
 38                "6:00 PM": "t",
 39                "6:00:00 PM": "T",
 40                "August 13, 2022 6:00 PM" : "f",
 41                "Saturday, August 13, 2022 6:00 PM" : "F",
 42                "in 3 hours": "R"
 43            }),
 44            "timezone" : GoldyBot.nextcord.SlashOption(description="The timezone to use. Goldy Bot defaults to Europe/London timezone.", required=False)
 45        })
 46        async def timestamp(self:Timestamps, ctx, date, time, flag, timezone=None):
 47            member = GoldyBot.Member(ctx)
 48            datetime = GoldyBot.utility.datetime.user_input.get_time_and_date(f"{date} {time}")
 49            timezone:str|None
 50
 51            if timezone is None:
 52                member_data = await member.get_member_data()
 53                member_saved_timezone = member_data[member.member_id].get("timezone", None)
 54
 55                if member_saved_timezone is None:
 56                    member_data[member.member_id]["timezone"] = self.default_timezone
 57                    await member.edit_member_data(member_data)
 58                    
 59                    timezone = member_data[member.member_id]["timezone"]
 60                else:
 61                    timezone = member_saved_timezone
 62
 63            if not datetime is None:
 64                try:
 65                    # Convert to chosen timezone.
 66                    chosen_timezone = pytz.timezone(timezone)
 67                    datetime = chosen_timezone.normalize(chosen_timezone.localize(datetime, is_dst=True))
 68
 69                    posix_timestamp = int(datetime.timestamp())
 70
 71                    view = CopyButtonView(posix_timestamp, flag)
 72
 73                    await send(ctx, f"<t:{posix_timestamp}:{flag}>", view=view)
 74
 75                    return True
 76
 77                except pytz.UnknownTimeZoneError as e:
 78                    GoldyBot.log("error", e)
 79                    message = await send(ctx, embed=self.unknown_timezone_embed)
 80                    
 81                except Exception as e:
 82                    GoldyBot.log("error", e)
 83
 84                    message = await send(ctx, embed=GoldyBot.utility.goldy.embed.Embed(
 85                        title="❓ Did you enter it correctly?",
 86                        description="I couldn't read either your time or date properly, please could you try again. Perhaps you mistyped something.",
 87                        colour=GoldyBot.utility.goldy.colours.RED
 88                    ))
 89
 90            else:
 91                message = await send(ctx, embed=GoldyBot.utility.goldy.embed.Embed(
 92                        title="❓ Did you enter it correctly?",
 93                        description="I couldn't read either your time or date properly, please could you try again. Perhaps you mistyped something.",
 94                        colour=GoldyBot.utility.goldy.colours.RED
 95                    )
 96                )
 97
 98            await message.delete(delay=30)
 99            return False
100
101
102        @GoldyBot.command(help_des="Sets default timezone for /timestamp command.", slash_cmd_only=True, slash_options={
103            "timezone" : GoldyBot.nextcord.SlashOption(description="The time zone. Must be like this --> Europe/London, America/New_York, Europe/Stockholm", required=True)
104        })
105        async def timezone_set(self:Timestamps, ctx, timezone):
106            member = GoldyBot.Member(ctx)
107            member_data = await member.get_member_data()
108
109            try:
110                pytz.timezone(timezone)
111                member_data[member.member_id]["timezone"] = timezone
112                await member.edit_member_data(member_data)
113
114                message = await send(ctx, embed=GoldyBot.Embed(
115                    title="💚 Time Zone Set!",
116                    description="Your default timezone was set **UwU**!",
117                    color=GoldyBot.Colours().GREEN
118                ))
119
120                await message.delete(delay=15)
121
122            except pytz.UnknownTimeZoneError as e:
123                GoldyBot.log("error", e)
124
125                message = await send(ctx, embed=self.unknown_timezone_embed)
126
127                await message.delete(delay=30)

Timestamps extension.

Timestamps(package_module=None)
14    def __init__(self, package_module=None):
15        super().__init__(self, package_module_name=package_module)
16
17        self.default_timezone = "Europe/London"
18
19        self.unknown_timezone_embed = GoldyBot.Embed(title="❤ Unknown Time Zone!", description="""
20*This is how the ``timezone`` parameter should be used.*
21
22``Europe/London`` = **Uk Time**
23``America/New_York`` = **New York Time**
24``Europe/Stockholm`` = **Sweden Time**
25
26*The list goes on... To see the full list click [here](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones).*
27            """, colour=GoldyBot.utility.goldy.colours.RED
28        ).set_thumbnail(GoldyBot.assets.ANIME_ONE_PUNCH_CLOCK_GIF)

Tells Goldy Bot to Load this class as an extension.

def loader(self):
 30    def loader(self):
 31
 32        @GoldyBot.command(help_des="Sends a discord timestamp of that time and date.",slash_cmd_only=True, slash_options={
 33            "date" : GoldyBot.nextcord.SlashOption(description="The date goes here like example, 13.08.2022 or even 2022/08/22.", required=True),
 34            "time" : GoldyBot.nextcord.SlashOption(description="The time goes here like example, 15:00.", required=True),
 35            "flag" : GoldyBot.nextcord.SlashOption(description="Choose a flag.", required=True, choices={
 36                "08/13/2022": "d",
 37                "August 13, 2022": "D",
 38                "6:00 PM": "t",
 39                "6:00:00 PM": "T",
 40                "August 13, 2022 6:00 PM" : "f",
 41                "Saturday, August 13, 2022 6:00 PM" : "F",
 42                "in 3 hours": "R"
 43            }),
 44            "timezone" : GoldyBot.nextcord.SlashOption(description="The timezone to use. Goldy Bot defaults to Europe/London timezone.", required=False)
 45        })
 46        async def timestamp(self:Timestamps, ctx, date, time, flag, timezone=None):
 47            member = GoldyBot.Member(ctx)
 48            datetime = GoldyBot.utility.datetime.user_input.get_time_and_date(f"{date} {time}")
 49            timezone:str|None
 50
 51            if timezone is None:
 52                member_data = await member.get_member_data()
 53                member_saved_timezone = member_data[member.member_id].get("timezone", None)
 54
 55                if member_saved_timezone is None:
 56                    member_data[member.member_id]["timezone"] = self.default_timezone
 57                    await member.edit_member_data(member_data)
 58                    
 59                    timezone = member_data[member.member_id]["timezone"]
 60                else:
 61                    timezone = member_saved_timezone
 62
 63            if not datetime is None:
 64                try:
 65                    # Convert to chosen timezone.
 66                    chosen_timezone = pytz.timezone(timezone)
 67                    datetime = chosen_timezone.normalize(chosen_timezone.localize(datetime, is_dst=True))
 68
 69                    posix_timestamp = int(datetime.timestamp())
 70
 71                    view = CopyButtonView(posix_timestamp, flag)
 72
 73                    await send(ctx, f"<t:{posix_timestamp}:{flag}>", view=view)
 74
 75                    return True
 76
 77                except pytz.UnknownTimeZoneError as e:
 78                    GoldyBot.log("error", e)
 79                    message = await send(ctx, embed=self.unknown_timezone_embed)
 80                    
 81                except Exception as e:
 82                    GoldyBot.log("error", e)
 83
 84                    message = await send(ctx, embed=GoldyBot.utility.goldy.embed.Embed(
 85                        title="❓ Did you enter it correctly?",
 86                        description="I couldn't read either your time or date properly, please could you try again. Perhaps you mistyped something.",
 87                        colour=GoldyBot.utility.goldy.colours.RED
 88                    ))
 89
 90            else:
 91                message = await send(ctx, embed=GoldyBot.utility.goldy.embed.Embed(
 92                        title="❓ Did you enter it correctly?",
 93                        description="I couldn't read either your time or date properly, please could you try again. Perhaps you mistyped something.",
 94                        colour=GoldyBot.utility.goldy.colours.RED
 95                    )
 96                )
 97
 98            await message.delete(delay=30)
 99            return False
100
101
102        @GoldyBot.command(help_des="Sets default timezone for /timestamp command.", slash_cmd_only=True, slash_options={
103            "timezone" : GoldyBot.nextcord.SlashOption(description="The time zone. Must be like this --> Europe/London, America/New_York, Europe/Stockholm", required=True)
104        })
105        async def timezone_set(self:Timestamps, ctx, timezone):
106            member = GoldyBot.Member(ctx)
107            member_data = await member.get_member_data()
108
109            try:
110                pytz.timezone(timezone)
111                member_data[member.member_id]["timezone"] = timezone
112                await member.edit_member_data(member_data)
113
114                message = await send(ctx, embed=GoldyBot.Embed(
115                    title="💚 Time Zone Set!",
116                    description="Your default timezone was set **UwU**!",
117                    color=GoldyBot.Colours().GREEN
118                ))
119
120                await message.delete(delay=15)
121
122            except pytz.UnknownTimeZoneError as e:
123                GoldyBot.log("error", e)
124
125                message = await send(ctx, embed=self.unknown_timezone_embed)
126
127                await message.delete(delay=30)

The extension's command loader. This is what Goldy Bot uses to load your commands in an extension.

class CopyButtonView(nextcord.ui.view.View):
130class CopyButtonView(GoldyBot.nextcord.ui.View):
131    def __init__(self, posix_timestamp:int, flag:str):
132        super().__init__()
133        self.posix_timestamp = posix_timestamp
134        self.flag = flag
135
136        self.response_message:GoldyBot.nextcord.PartialInteractionMessage = None
137
138    @GoldyBot.nextcord.ui.button(label="📋 Copy", style=GoldyBot.nextcord.ButtonStyle.gray)
139    async def copy(self, button: GoldyBot.nextcord.ui.Button, interaction: GoldyBot.nextcord.Interaction):
140        self.response_message = await interaction.response.send_message(f"``<t:{self.posix_timestamp}:{self.flag}>``", ephemeral=True)
141        self.value = True

Represents a UI view.

This object must be inherited to create a UI within Discord.

New in version 2.0.

Parameters

timeout: Optional[float] Timeout in seconds from last interaction with the UI before no longer accepting input. If None then there is no timeout. auto_defer: bool = True Whether or not to automatically defer the component interaction when the callback completes without responding to the interaction. Set this to False if you want to handle view interactions outside of the callback.

Attributes

timeout: Optional[float] Timeout from last interaction with the UI before no longer accepting input. If None then there is no timeout. children: List[Item] The list of children attached to this view. auto_defer: bool = True Whether or not to automatically defer the component interaction when the callback completes without responding to the interaction. Set this to False if you want to handle view interactions outside of the callback.

CopyButtonView(posix_timestamp: int, flag: str)
131    def __init__(self, posix_timestamp:int, flag:str):
132        super().__init__()
133        self.posix_timestamp = posix_timestamp
134        self.flag = flag
135
136        self.response_message:GoldyBot.nextcord.PartialInteractionMessage = None
@GoldyBot.nextcord.ui.button(label='📋 Copy', style=GoldyBot.nextcord.ButtonStyle.gray)
async def copy( self, button: nextcord.ui.button.Button, interaction: nextcord.interactions.Interaction):
138    @GoldyBot.nextcord.ui.button(label="📋 Copy", style=GoldyBot.nextcord.ButtonStyle.gray)
139    async def copy(self, button: GoldyBot.nextcord.ui.Button, interaction: GoldyBot.nextcord.Interaction):
140        self.response_message = await interaction.response.send_message(f"``<t:{self.posix_timestamp}:{self.flag}>``", ephemeral=True)
141        self.value = True
Inherited Members
nextcord.ui.view.View
to_components
from_message
add_item
remove_item
clear_items
interaction_check
on_timeout
on_error
refresh
stop
is_finished
is_dispatching
is_persistent
wait
def load():
144def load():
145    Timestamps(__name__)