1
Fork 0
mirror of https://github.com/wlinator/luminara.git synced 2024-10-02 20:23:12 +00:00

Change misc and birthday outputs

This commit is contained in:
wlinator 2024-03-17 21:44:21 +01:00
parent a332deceea
commit ed2785a1f8
6 changed files with 177 additions and 13 deletions

View file

@ -3,6 +3,7 @@ import os
import discord
from dotenv import load_dotenv
from lib.embeds.error import BdayErrors
from services.Birthday import Birthday
from services.GuildConfig import GuildConfig
@ -17,7 +18,7 @@ async def birthday_module(ctx):
guild_config = GuildConfig(ctx.guild.id)
if not guild_config.birthday_channel_id:
await ctx.respond(f"Birthdays are disabled in this server.", ephemeral=True)
await ctx.respond(embed=BdayErrors.birthdays_disabled(ctx))
return False
return True
@ -42,7 +43,7 @@ async def channel(ctx):
await ctx.respond(f"I don't have sufficient permissions to check "
f"whether this channel allows commands or not. "
f"Please do '/config commands channel <channel>' with a channel "
f"I can see or allow commands everywhere.")
f"I can see or allow commands everywhere.", ephemeral=True)
return False
return True

View file

@ -99,3 +99,19 @@ class EconErrors:
embed.set_footer(text="Try the command again", icon_url=exclam_icon)
return embed
class BdayErrors:
@staticmethod
def birthdays_disabled(ctx):
embed = clean_error_embed(ctx)
embed.description += "birthdays are disabled in this server."
return embed
@staticmethod
def invalid_date(ctx):
embed = clean_error_embed(ctx)
embed.description += "the date you entered is invalid."
return embed

View file

@ -24,3 +24,21 @@ class EconInfo:
icon_url=exclam_icon)
return embed
class MiscInfo:
@staticmethod
def ping(ctx, client):
embed = clean_info_embed(ctx)
embed.description += "I'm online!"
embed.set_footer(text=f"Latency: {round(1000 * client.latency)}ms", icon_url=exclam_icon)
return embed
@staticmethod
def uptime(ctx, client, unix_time):
embed = clean_info_embed(ctx)
embed.description += f"I've been online since <t:{unix_time}:R>"
embed.set_footer(text=f"Latency: {round(1000 * client.latency)}ms", icon_url=exclam_icon)
return embed

View file

@ -160,7 +160,9 @@ reactions = json_loader.load_reactions()
def load_modules():
modules_list = [
"economy"
"birthdays",
"economy",
"misc"
]
loaded_modules = set()

View file

@ -0,0 +1,127 @@
import asyncio
import calendar
import datetime
import logging
import random
import discord
from discord import default_permissions
from discord.commands import SlashCommandGroup
from discord.ext import commands, tasks
from config import json_loader
from services.Birthday import Birthday
from services.GuildConfig import GuildConfig
from main import strings
from lib import time, checks
logs = logging.getLogger('Racu.Core')
data = json_loader.load_birthday()
months = data["months"]
messages = data["birthday_messages"]
class Birthdays(commands.Cog):
def __init__(self, client):
self.client = client
self.daily_birthday_check.start()
birthday = SlashCommandGroup("birthday", "various birthday commands.", guild_only=True)
@birthday.command(
name="set",
description="Set your birthday."
)
@commands.cooldown(1, 10, commands.BucketType.user)
@commands.check(checks.birthday_module)
async def set_birthday(self, ctx, *,
month: discord.Option(choices=months),
day: discord.Option(int)):
leap_year = 2020
month_index = months.index(month) + 1
max_days = calendar.monthrange(leap_year, month_index)[1]
if not (1 <= day <= max_days):
return await ctx.respond(strings["birthday_invalid_date"].format(ctx.author.name), ephemeral=True)
date_str = f"{leap_year}-{month_index:02d}-{day:02d}"
date_obj = datetime.datetime.strptime(date_str, '%Y-%m-%d')
birthday = Birthday(ctx.author.id, ctx.guild.id)
birthday.set(date_obj)
await ctx.respond(strings["birthday_set"].format(ctx.author.name, month, day), ephemeral=True)
@birthday.command(
name="upcoming",
description="See upcoming birthdays!"
)
@commands.cooldown(1, 10, commands.BucketType.user)
@commands.check(checks.birthday_module)
async def upcoming_birthdays(self, ctx):
upcoming_birthdays = Birthday.get_upcoming_birthdays(ctx.guild.id)
icon = ctx.guild.icon if ctx.guild.icon else "https://i.imgur.com/79XfsbS.png"
embed = discord.Embed(
color=discord.Color.embed_background()
)
embed.set_author(name="Upcoming Birthdays!", icon_url=icon)
embed.set_thumbnail(url="https://i.imgur.com/79XfsbS.png")
for i, (user_id, birthday) in enumerate(upcoming_birthdays, start=1):
try:
member = await ctx.guild.fetch_member(user_id)
name = member.name
except:
name = "Unknown User"
try:
birthday_date = datetime.datetime.strptime(birthday, "%m-%d")
formatted_birthday = birthday_date.strftime("%B %-d")
except ValueError:
# leap year error
formatted_birthday = "February 29"
embed.add_field(
name=f"{name}",
value=f"🎂 {formatted_birthday}",
inline=False
)
await ctx.respond(embed=embed)
@tasks.loop(hours=23, minutes=55)
async def daily_birthday_check(self):
wait_time = time.seconds_until(7, 0)
logs.info(f"[BirthdayHandler] Waiting until 7 AM Eastern for daily check: {round(wait_time)}s")
await asyncio.sleep(wait_time)
embed = discord.Embed(color=discord.Color.embed_background())
embed.set_image(url="https://media1.tenor.com/m/NXvU9jbBUGMAAAAC/fireworks.gif")
for user_id, guild_id in Birthday.get_birthdays_today():
try:
guild = await self.client.fetch_guild(guild_id)
member = await guild.fetch_member(user_id)
guild_config = GuildConfig(guild.id)
if not guild_config.birthday_channel_id:
logs.info(f"[BirthdayHandler] Guild with ID {guild.id} skipped: no birthday channel defined.")
return
message = random.choice(messages)
embed.description = message.format(member.name)
channel = await guild.fetch_channel(guild_config.birthday_channel_id)
await channel.send(embed=embed, content=member.mention)
logs.info(f"[BirthdayHandler] Success! user/guild/channel ID: {member.id}/{guild.id}/{channel.id}")
except Exception as error:
logs.info(f"[BirthdayHandler] Skipped processing user/guild {user_id}/{guild_id}")
# wait one second to avoid rate limits
await asyncio.sleep(1)
def setup(client):
client.add_cog(Birthdays(client))

View file

@ -1,18 +1,21 @@
import logging
import discord
from discord.ext import commands, bridge
import datetime, time
from lib.embeds.info import MiscInfo
from main import strings
from lib import checks
from main import strings
logs = logging.getLogger('Racu.Core')
class Miscellaneous(commands.Cog):
class Misc(commands.Cog):
def __init__(self, client):
self.client = client
self.start_time = time.time()
self.start_time = datetime.datetime.now()
@bridge.bridge_command(
name="ping",
@ -24,7 +27,7 @@ class Miscellaneous(commands.Cog):
)
@commands.check(checks.channel)
async def ping(self, ctx):
await ctx.respond(content=strings["ping"].format(ctx.author.name))
return await ctx.respond(embed=MiscInfo.ping(ctx, self.client))
@bridge.bridge_command(
name="uptime",
@ -34,13 +37,10 @@ class Miscellaneous(commands.Cog):
)
@commands.check(checks.channel)
async def uptime(self, ctx):
current_time = time.time()
difference = int(round(current_time - self.start_time))
text = str(datetime.timedelta(seconds=difference))
await ctx.respond(content=strings["uptime"].format(ctx.author.name, text))
unix_timestamp = int(round(self.start_time.timestamp()))
return await ctx.respond(embed=MiscInfo.uptime(ctx, self.client, unix_timestamp))
def setup(client):
client.add_cog(Miscellaneous(client))
client.add_cog(Misc(client))