wdiscordbotserver/cogs/world_time.py
ザカリアス・ウィリアム・ポージー c889e9be0f Edit world_time.py
2025-06-05 02:41:42 +09:00

46 lines
1.9 KiB
Python

import discord
from discord import app_commands
from discord.ext import commands
import pytz
import random
import datetime
class WorldTime(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command(name="world_time", description="Display timezones")
@app_commands.describe(timezone="The timezone to display (optional)")
async def worldtime(self, interaction: discord.Interaction, timezone: str = None):
now = datetime.datetime.now(datetime.timezone.utc)
if timezone:
try:
tz = pytz.timezone(timezone)
local_time = now.astimezone(tz)
await interaction.response.send_message(f"**Time in {timezone}**: {local_time.strftime('%Y-%m-%d %H:%M:%S')}")
except pytz.exceptions.UnknownTimeZoneError:
# Handle invalid timezone
await interaction.response.send_message(f"Unknown timezone: `{timezone}`\nTry using a timezone from the IANA Time Zone Database (e.g., 'America/New_York', 'Europe/London')", ephemeral=True)
else:
# no selected zone so displays fuckass zones :3
all_timezones = list(pytz.all_timezones)
random_timezones = random.sample(all_timezones, 5)
embed = discord.Embed(title="World Time", color=discord.Color.blue())
embed.description = "Timezones"
for tz_name in random_timezones:
tz = pytz.timezone(tz_name)
local_time = now.astimezone(tz)
embed.add_field(
name=tz_name,
value=local_time.strftime('%Y-%m-%d %H:%M:%S'),
inline=False
)
await interaction.response.send_message(embed=embed)
async def setup(bot):
await bot.add_cog(WorldTime(bot))