Skip to content

Commit

Permalink
Trye to user plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Sep 17, 2024
1 parent 6502307 commit b29f5d2
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 191 deletions.
197 changes: 6 additions & 191 deletions worldtimezone/__main__.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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__":
Expand Down
46 changes: 46 additions & 0 deletions worldtimezone/extensions/edit_world_clock.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions worldtimezone/extensions/ping.py
Original file line number Diff line number Diff line change
@@ -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)
Loading

0 comments on commit b29f5d2

Please sign in to comment.