Skip to content

Commit

Permalink
New command /get-timestamp:
Browse files Browse the repository at this point in the history
- Accepts a date or/and a time in various formats
- Returns a formatted timestamp for displaying it in local TZ of a user
  • Loading branch information
soksanichenko committed Feb 28, 2024
1 parent 190950c commit ba5ad08
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 13 deletions.
102 changes: 89 additions & 13 deletions sources/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import logging
import os
from copy import copy
from dataclasses import dataclass
from typing import Optional
from urllib.parse import urlparse, ParseResult
import dateparser
from tldextract import extract
from tldextract.tldextract import ExtractResult

Expand All @@ -19,21 +18,11 @@
logger = logging.getLogger('discord')


@dataclass
class DomainFixer:
"""
data-class for a domain fixer
"""
fixer: str
second_fixer: Optional[str] = None
second_domain: Optional[str] = None


@bot.tree.command(
name='ping',
description='Test ping command',
)
async def ping(interaction):
async def ping(interaction: discord.Interaction):
"""
Test ping command
:param interaction: the command's interaction
Expand All @@ -42,6 +31,92 @@ async def ping(interaction):
await interaction.response.send_message('pong')


class TimestampFormatView(discord.ui.View):
"""
View class for timestamp formatting
"""

def __init__(self, timestamp: int):
self.timestamp = timestamp
super().__init__()

@discord.ui.select(
placeholder='Select format',
min_values=1,
max_values=1,
options=[
discord.SelectOption(
label='F',
description='Wednesday, 1 January 2021, 23:50'
),
discord.SelectOption(
label='f',
description='1 January 2021, 23:50'
),
discord.SelectOption(
label='D',
description='1 January 2021'
),
discord.SelectOption(
label='d',
description='01.01.2021'
),
discord.SelectOption(
label='t',
description='23:50'
),
discord.SelectOption(
label='T',
description='23:50:55'
),
discord.SelectOption(
label='R',
description='2 hours ago'
),
]
)
async def select_callback(
self,
interaction: discord.Interaction,
select: discord.ui.ChannelSelect,
):
"""
Callback for selecting an option of formatting
:param interaction: an object of interaction with a user
:param select: a selected option
:return: None
"""
await interaction.response.send_message(
f'<t:{self.timestamp}:{select.values[0]}>'
)


@bot.tree.command(
name='get-timestamp',
description='Get formatted timestamp for any date and/or time',
)
@discord.app_commands.describe(time='HH:MM')
@discord.app_commands.describe(date='dd.mm.YYYY')
async def get_timestamp(interaction, time: str = '', date: str = ''):
"""
Send any text by the bot
:param time: an input time for converting
:param date: an input date for converting
:param interaction: the command's interaction
:return: None
"""
time_date = dateparser.parse(f'{time} {date}')
if time_date is None:
await interaction.response.send_message(
'You sent a date/time in incorrect format',
)
return
await interaction.response.send_message(
'Select format',
view=TimestampFormatView(int(time_date.timestamp()))
)


def fix_urls(message: discord.Message) -> str:
"""
Fix the URLs by replacing an original domain by a fixer
Expand All @@ -53,6 +128,7 @@ def fix_urls(message: discord.Message) -> str:
'tiktok.com': 'vxtiktok',
'x.com': 'fixupx',
'twitter.com': 'fxtwitter',
'instagram.com': 'ddinstagram'
}

msg_content_lines = message.content.split()
Expand Down
1 change: 1 addition & 0 deletions sources/req.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
discord==2.3.2
tldextract==5.1.1
dateparser==1.2.0

0 comments on commit ba5ad08

Please sign in to comment.