From b29f5d2968f750e8278cbae6fc739a53229d668b Mon Sep 17 00:00:00 2001 From: Xavier Mitault Date: Wed, 18 Sep 2024 00:16:31 +0100 Subject: [PATCH] Trye to user plugin --- worldtimezone/__main__.py | 197 +------------------ worldtimezone/extensions/edit_world_clock.py | 46 +++++ worldtimezone/extensions/ping.py | 19 ++ worldtimezone/extensions/world_clock.py | 156 +++++++++++++++ 4 files changed, 227 insertions(+), 191 deletions(-) create mode 100644 worldtimezone/extensions/edit_world_clock.py create mode 100644 worldtimezone/extensions/ping.py create mode 100644 worldtimezone/extensions/world_clock.py diff --git a/worldtimezone/__main__.py b/worldtimezone/__main__.py index dd9cb29..f3e2524 100644 --- a/worldtimezone/__main__.py +++ b/worldtimezone/__main__.py @@ -1,17 +1,10 @@ import os -from typing import Optional -import datetime - -from data import Data import hikari import lightbulb +from data import Data from hikari import Intents - -import pytz - -from apscheduler.schedulers.asyncio import AsyncIOScheduler -from apscheduler.triggers.cron import CronTrigger +from lightbulb.ext import tasks INTENTS = Intents.GUILD_MEMBERS | Intents.GUILDS @@ -20,195 +13,17 @@ intents=INTENTS, banner=None, ) - - -async def edit_world_clock() -> None: - - def create_embed(guild_id, u, tz): - user_ = bot.cache.get_member(guild_id, int(u)) - embed = ( - hikari.Embed( - title=f"WorldTimeClock - {user_.display_name}", - description=f"Timezone: {tz}", - ) - .set_author(name=f"{user_.display_name}") - .set_thumbnail(user_.avatar_url) - ) - now = datetime.datetime.now() - new_tz = pytz.timezone(tz) - new_now = now.astimezone(new_tz) - embed.add_field("Time", f"{new_now}", inline=False) - return embed - - for guild_id in bot.d.data.get_guilds_list(): - embeds = [] - guild_world_clock = bot.d.data.get_world_clock(guild_id) - channel_world_clock = guild_world_clock["channel_id"] - message_world_clock = guild_world_clock["message_id"] - - for u in bot.d.data.get_members_list(guild_id): - member = bot.d.data.get_member(guild_id, u) - if "tz" in member: - embeds.append(create_embed(guild_id, u, member["tz"])) - await bot.rest.edit_message( - channel_world_clock, message_world_clock, None, embeds=embeds - ) +tasks.load(bot) @bot.listen(hikari.StartingEvent) async def on_starting(_: hikari.StartingEvent) -> None: bot.d.data = Data() - bot.d.sched = AsyncIOScheduler() - bot.d.sched.start() - bot.d.sched.add_job(edit_world_clock, CronTrigger(minute="*/5")) - - -@bot.command -@lightbulb.add_cooldown(5, 1, lightbulb.GuildBucket) -@lightbulb.command("ping", description="The bot's ping.") -@lightbulb.implements(lightbulb.SlashCommand) -async def pingIt(ctx: lightbulb.SlashContext) -> None: - await ctx.respond(f"Pong! Latency: {bot.heartbeat_latency * 1000:.2f}ms.") - - -@bot.command -@lightbulb.add_cooldown(5, 1, lightbulb.UserBucket) -@lightbulb.option("timezone", "Your TimeZone", type=str, required=False) -@lightbulb.command("set", description="Set your TimeZone", pass_options=True) -@lightbulb.implements(lightbulb.SlashCommand) -async def setIt(ctx: lightbulb.SlashContext, timezone: Optional[str] = None) -> None: - res = bot.d.data.set_member_tz(ctx.guild_id, ctx.user.id, timezone) - if res is False: - await ctx.respond( - f"Failed, please provide a working timezone ('{timezone}' is unknow)" - ) - return - await ctx.respond("Your TimeZone as been set!") - -@bot.command -@lightbulb.add_cooldown(2, 1, lightbulb.GuildBucket) -@lightbulb.option( - "user", "to see only his/her time", type=hikari.OptionType.USER, required=False -) -@lightbulb.command( - "list", description="see what time is it for others", pass_options=True -) -@lightbulb.implements(lightbulb.SlashCommand) -async def listIt( - ctx: lightbulb.SlashContext, user: Optional[hikari.OptionType.USER] -) -> None: - assert ctx.guild_id is not None - - embed = hikari.Embed( - title="WorldTimeZone Clock", - description="", - ).set_thumbnail(ctx.user.avatar_url) - - def add_field(u, tz): - user_ = ctx.bot.cache.get_member(ctx.guild_id, int(u)) - now = datetime.datetime.now() - new_tz = pytz.timezone(tz) - new_now = now.astimezone(new_tz) - embed.add_field(f"{user_.display_name}", f"{new_now}", inline=False) - if user is None: - for u in bot.d.data.get_members_list(ctx.guild_id): - member = bot.d.data.get_member(ctx.guild_id, u) - if "tz" in member: - add_field(u, member["tz"]) - else: - member = bot.d.data.get_member(ctx.guild_id, user.id) - if "tz" in member: - add_field(f"{user.id}", member["tz"]) - else: - ctx.respond("This user has no timezone set.") - return - await ctx.respond(embed) - - -@bot.command -@lightbulb.option("hour", "hour in your timezone", type=int, required=True) -@lightbulb.option( - "minute", "minute in your timezone", type=int, required=False, default=0 -) -@lightbulb.option( - "day", - "day in your timezone", - type=int, - required=False, - default=datetime.datetime.now().day, -) -@lightbulb.option( - "month", - "month in your timezone", - type=int, - required=False, - default=datetime.datetime.now().month, -) -@lightbulb.option( - "year", - "year in your timezone", - type=int, - required=False, - default=datetime.datetime.now().year, -) -@lightbulb.command( - "convert", - description="convert a specific time to others timezone", - pass_options=True, -) -@lightbulb.implements(lightbulb.SlashCommand) -async def convertIt( - ctx: lightbulb.SlashContext, hour: int, minute: int, day: int, month: int, year: int -) -> None: - message = "" - user_info = bot.d.data.get_member(ctx.guild_id, ctx.user.id) - if "tz" not in user_info: - await ctx.respond("Please set your timezone first") - return - for u in bot.d.data.get_members_list(ctx.guild_id): - member_info = bot.d.data.get_member(ctx.guild_id, u) - if "tz" in member_info: - user_ = ctx.bot.cache.get_member(ctx.guild_id, int(u)) - now = datetime.datetime( - year=year, month=month, day=day, hour=hour, minute=minute - ) - now = pytz.timezone(user_info["tz"]).localize(now) - new_tz = pytz.timezone(member_info["tz"]) - new_now = now.astimezone(new_tz) - message += f"**{user_.display_name}**: {new_now}\n" - await ctx.respond(message) - - -@bot.command -@lightbulb.add_cooldown(10, 1, lightbulb.GuildBucket) -@lightbulb.option( - "channel", - "where the bot will edit the same message", - type=hikari.OptionType.CHANNEL, - required=True, -) -@lightbulb.command( - "setupchannel", - description="edit the same message every 5 minutes", - pass_options=True, -) -@lightbulb.implements(lightbulb.SlashCommand) -async def setupIt( - ctx: lightbulb.SlashContext, channel: hikari.OptionType.CHANNEL -) -> None: - message = await bot.rest.create_message( - channel, "This message will be edited in no time... Please wait..." - ) - status = bot.d.data.set_world_clock(ctx.guild_id, channel.id, message.id) - if status is False: - await message.delete() - ctx.respond("Sorry, an error occured.") - return - await ctx.respond( - "All Set! A message has been sent to the channel, it will be edited soon." - ) +bot.load_extensions("extensions.ping") +bot.load_extensions("extensions.world_clock") +bot.load_extensions("extensions.edit_world_clock") if __name__ == "__main__": diff --git a/worldtimezone/extensions/edit_world_clock.py b/worldtimezone/extensions/edit_world_clock.py new file mode 100644 index 0000000..4bb342e --- /dev/null +++ b/worldtimezone/extensions/edit_world_clock.py @@ -0,0 +1,46 @@ +import datetime + +import hikari +import lightbulb +import pytz +from lightbulb.ext import tasks + +plugin = lightbulb.Plugin("EditWorldClock") + + +def load(bot: lightbulb.BotApp): + @tasks.task(m=5, auto_start=True) + async def edit_world_clock() -> None: + def create_embed(guild_id, u, tz): + user_ = bot.cache.get_member(guild_id, int(u)) + embed = ( + hikari.Embed( + title=f"WorldTimeClock - {user_.display_name}", + description=f"Timezone: {tz}", + ) + .set_author(name=f"{user_.display_name}") + .set_thumbnail(user_.avatar_url) + ) + now = datetime.datetime.now() + new_tz = pytz.timezone(tz) + new_now = now.astimezone(new_tz) + embed.add_field("Time", f"{new_now}", inline=False) + return embed + + for guild_id in bot.d.data.get_guilds_list(): + embeds = [] + guild_world_clock = bot.d.data.get_world_clock(guild_id) + channel_world_clock = guild_world_clock["channel_id"] + message_world_clock = guild_world_clock["message_id"] + + for u in bot.d.data.get_members_list(guild_id): + member = bot.d.data.get_member(guild_id, u) + if "tz" in member: + embeds.append(create_embed(guild_id, u, member["tz"])) + await bot.rest.edit_message( + channel_world_clock, message_world_clock, None, embeds=embeds + ) + + +def unload(bot: lightbulb.BotApp): + pass diff --git a/worldtimezone/extensions/ping.py b/worldtimezone/extensions/ping.py new file mode 100644 index 0000000..17940ce --- /dev/null +++ b/worldtimezone/extensions/ping.py @@ -0,0 +1,19 @@ +import lightbulb + +plugin = lightbulb.Plugin("Ping") + + +@plugin.command +@lightbulb.add_cooldown(5, 1, lightbulb.GuildBucket) +@lightbulb.command("ping", description="The bot's ping.") +@lightbulb.implements(lightbulb.SlashCommand) +async def pingIt(ctx: lightbulb.SlashContext) -> None: + await ctx.respond(f"Pong! Latency: {ctx.bot.heartbeat_latency * 1000:.2f}ms.") + + +def load(bot: lightbulb.BotApp): + bot.add_plugin(plugin) + + +def unload(bot: lightbulb.BotApp): + bot.remove_plugin(plugin) diff --git a/worldtimezone/extensions/world_clock.py b/worldtimezone/extensions/world_clock.py new file mode 100644 index 0000000..dd5b05f --- /dev/null +++ b/worldtimezone/extensions/world_clock.py @@ -0,0 +1,156 @@ +import datetime +from typing import Optional + +import hikari +import lightbulb +import pytz + +plugin = lightbulb.Plugin("WorldClock") + + +@plugin.command +@lightbulb.add_cooldown(5, 1, lightbulb.UserBucket) +@lightbulb.option("timezone", "Your TimeZone", type=str, required=False) +@lightbulb.command("set", description="Set your TimeZone", pass_options=True) +@lightbulb.implements(lightbulb.SlashCommand) +async def setIt(ctx: lightbulb.SlashContext, timezone: Optional[str] = None) -> None: + res = ctx.bot.d.data.set_member_tz(ctx.guild_id, ctx.user.id, timezone) + if res is False: + await ctx.respond( + f"Failed, please provide a working timezone ('{timezone}' is unknow)" + ) + return + await ctx.respond("Your TimeZone as been set!") + + +@plugin.command +@lightbulb.add_cooldown(2, 1, lightbulb.GuildBucket) +@lightbulb.option( + "user", "to see only his/her time", type=hikari.OptionType.USER, required=False +) +@lightbulb.command( + "list", description="see what time is it for others", pass_options=True +) +@lightbulb.implements(lightbulb.SlashCommand) +async def listIt( + ctx: lightbulb.SlashContext, user: Optional[hikari.OptionType.USER] +) -> None: + assert ctx.guild_id is not None + + embed = hikari.Embed( + title="WorldTimeZone Clock", + description="", + ).set_thumbnail(ctx.user.avatar_url) + + def add_field(u, tz): + user_ = ctx.bot.cache.get_member(ctx.guild_id, int(u)) + now = datetime.datetime.now() + new_tz = pytz.timezone(tz) + new_now = now.astimezone(new_tz) + embed.add_field(f"{user_.display_name}", f"{new_now}", inline=False) + + if user is None: + for u in ctx.bot.d.data.get_members_list(ctx.guild_id): + member = ctx.bot.d.data.get_member(ctx.guild_id, u) + if "tz" in member: + add_field(u, member["tz"]) + else: + member = ctx.bot.d.data.get_member(ctx.guild_id, user.id) + if "tz" in member: + add_field(f"{user.id}", member["tz"]) + else: + ctx.respond("This user has no timezone set.") + return + await ctx.respond(embed) + + +@plugin.command +@lightbulb.option("hour", "hour in your timezone", type=int, required=True) +@lightbulb.option( + "minute", "minute in your timezone", type=int, required=False, default=0 +) +@lightbulb.option( + "day", + "day in your timezone", + type=int, + required=False, + default=datetime.datetime.now().day, +) +@lightbulb.option( + "month", + "month in your timezone", + type=int, + required=False, + default=datetime.datetime.now().month, +) +@lightbulb.option( + "year", + "year in your timezone", + type=int, + required=False, + default=datetime.datetime.now().year, +) +@lightbulb.command( + "convert", + description="convert a specific time to others timezone", + pass_options=True, +) +@lightbulb.implements(lightbulb.SlashCommand) +async def convertIt( + ctx: lightbulb.SlashContext, hour: int, minute: int, day: int, month: int, year: int +) -> None: + message = "" + user_info = ctx.bot.d.data.get_member(ctx.guild_id, ctx.user.id) + if "tz" not in user_info: + await ctx.respond("Please set your timezone first") + return + for u in ctx.bot.d.data.get_members_list(ctx.guild_id): + member_info = ctx.bot.d.data.get_member(ctx.guild_id, u) + if "tz" in member_info: + user_ = ctx.bot.cache.get_member(ctx.guild_id, int(u)) + now = datetime.datetime( + year=year, month=month, day=day, hour=hour, minute=minute + ) + now = pytz.timezone(user_info["tz"]).localize(now) + new_tz = pytz.timezone(member_info["tz"]) + new_now = now.astimezone(new_tz) + message += f"**{user_.display_name}**: {new_now}\n" + await ctx.respond(message) + + +@plugin.command +@lightbulb.add_cooldown(10, 1, lightbulb.GuildBucket) +@lightbulb.option( + "channel", + "where the bot will edit the same message", + type=hikari.OptionType.CHANNEL, + required=True, +) +@lightbulb.command( + "setupchannel", + description="edit the same message every 5 minutes", + pass_options=True, +) +@lightbulb.implements(lightbulb.SlashCommand) +async def setupIt( + ctx: lightbulb.SlashContext, channel: hikari.OptionType.CHANNEL +) -> None: + message = await ctx.bot.rest.create_message( + channel, "This message will be edited in no time... Please wait..." + ) + status = ctx.bot.d.data.set_world_clock(ctx.guild_id, channel.id, message.id) + if status is False: + await message.delete() + ctx.respond("Sorry, an error occured.") + return + await ctx.respond( + "All Set! A message has been sent to the channel, it will be edited soon." + ) + + +def load(bot: lightbulb.BotApp): + bot.add_plugin(plugin) + + +def unload(bot: lightbulb.BotApp): + bot.remove_plugin(plugin)