Refactor: Implement Pastebin sharing command and enhance server time display with timezone information

This commit is contained in:
pancakes-proxy 2025-05-21 03:34:15 +09:00
parent a06ea83507
commit a8f695d07e
3 changed files with 103 additions and 0 deletions

0
cogs/notebook.py Normal file
View File

77
cogs/pastebinmessage.py Normal file
View File

@ -0,0 +1,77 @@
import discord
from discord import app_commands
from discord.ext import commands
import aiohttp
import re
PASTEBIN_API_URL = "http://pastebin.internettools.org:3000/api/paste"
MESSAGE_LINK_RE = re.compile(
r"https://(?:canary\.|ptb\.)?discord(?:app)?\.com/channels/(\d+)/(\d+)/(\d+)"
)
class PastebinMessageCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command(
name="share",
description="Share a Discord message as a Pastebin link."
)
@app_commands.describe(message_link="The link to the Discord message to share")
async def share(self, interaction: discord.Interaction, message_link: str):
await interaction.response.defer(thinking=True)
match = MESSAGE_LINK_RE.match(message_link)
if not match:
await interaction.followup.send("Invalid message link format.", ephemeral=True)
return
guild_id, channel_id, message_id = map(int, match.groups())
channel = self.bot.get_channel(channel_id)
if channel is None:
# Try fetching the channel if not cached
try:
channel = await self.bot.fetch_channel(channel_id)
except Exception:
await interaction.followup.send("Could not find the channel.", ephemeral=True)
return
try:
message = await channel.fetch_message(message_id)
except Exception:
await interaction.followup.send("Could not fetch the message.", ephemeral=True)
return
content = message.content
if not content:
await interaction.followup.send("The message has no text content to share.", ephemeral=True)
return
# Create paste
async with aiohttp.ClientSession() as session:
try:
async with session.post(
PASTEBIN_API_URL,
json={"content": content},
headers={"Content-Type": "application/json"}
) as resp:
if resp.status == 201:
data = await resp.json()
paste_url = data.get("url")
await interaction.followup.send(
f"Pastebin link: {paste_url}"
)
else:
error = await resp.json()
await interaction.followup.send(
f"Failed to create paste: {error.get('error', 'Unknown error')}",
ephemeral=True
)
except Exception as e:
await interaction.followup.send(
f"Error communicating with Pastebin API: {e}",
ephemeral=True
)
async def setup(bot):
await bot.add_cog(PastebinMessageCog(bot))

26
cogs/servertime.py Normal file
View File

@ -0,0 +1,26 @@
import discord
from discord import app_commands
from discord.ext import commands
from datetime import datetime
import pytz
import time
class ServerTime(commands.Cog):
def __init__(self, bot):
self.bot = bot
@app_commands.command(name="timeis", description="Shows the server's current time and date with timezone.")
async def timeis(self, interaction: discord.Interaction):
# Get local time with timezone
local_time = datetime.now().astimezone()
timezone_name = time.tzname[local_time.dst() != 0]
formatted_time = local_time.strftime("%Y-%m-%d %H:%M:%S")
embed = discord.Embed(
title="Server Time",
description=f"**{formatted_time}**\nTimezone: `{timezone_name}`",
color=discord.Color.blue()
)
await interaction.response.send_message(embed=embed)
async def setup(bot):
await bot.add_cog(ServerTime(bot))