From 38d184f38b1962df4b317b5493faf606b226101d Mon Sep 17 00:00:00 2001 From: Myned Date: Tue, 5 Jul 2022 17:19:59 -0500 Subject: [PATCH] Add inactive command --- commands/info.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ config.py | 3 +++ pyproject.toml | 1 + run.py | 2 +- tasks/activity.py | 8 +++---- tools/components.py | 12 ++++++++++ 6 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 commands/info.py create mode 100644 tools/components.py diff --git a/commands/info.py b/commands/info.py new file mode 100644 index 0000000..5f97a7f --- /dev/null +++ b/commands/info.py @@ -0,0 +1,53 @@ +import hikari +import lightbulb +from miru.ext import nav + +import config as c +from tools import components + + +plugin = lightbulb.Plugin("info", default_enabled_guilds=c.config["guild"]) + + +# Get list of inactive members +@plugin.command +@lightbulb.add_checks(lightbulb.has_guild_permissions(hikari.Permissions.MANAGE_GUILD)) +@lightbulb.command("inactive", "List inactive members", ephemeral=True) +@lightbulb.implements(lightbulb.SlashCommand) +async def inactive(context): + def build(index, content): + return hikari.Embed( + title="Inactive", description=content, color=context.get_guild().get_my_member().get_top_role().color + ).set_footer(f"{len(inactive)} members") + + inactive = { + snowflake: member + for snowflake, member in sorted( + context.get_guild().get_members().items(), key=lambda item: item[1].display_name + ) + if snowflake not in c.db + } + + paginator = lightbulb.utils.EmbedPaginator() + paginator.set_embed_factory(build) + for snowflake, member in inactive.items(): + paginator.add_line(f"{member.mention} {snowflake}") + pages = [page for page in paginator.build_pages()] + + if len(pages) > 1: + navigator = nav.NavigatorView( + pages=pages, + buttons=[components.Back(), components.Forward()], + timeout=600, + ) + await navigator.send(context.interaction, ephemeral=True) + else: + await context.respond(pages[0]) + + +def load(bot): + bot.add_plugin(plugin) + + +def unload(bot): + bot.remove_plugin(plugin) diff --git a/config.py b/config.py index 8edb7b5..c262473 100644 --- a/config.py +++ b/config.py @@ -1,4 +1,5 @@ import toml +import sqlitedict import hikari @@ -26,3 +27,5 @@ except FileNotFoundError: f.write(CONFIG) print("config.toml created with default values. Restart when modified") exit() + +db = sqlitedict.SqliteDict(config["db"], tablename=str(config["guild"]), autocommit=True) diff --git a/pyproject.toml b/pyproject.toml index 253ad53..db044af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -12,6 +12,7 @@ uvloop = "*" sqlitedict = "*" hikari = {extras = ["speedups"], version = "*"} hikari-lightbulb = "*" +hikari-miru = "*" [tool.poetry.dev-dependencies] black = "*" diff --git a/run.py b/run.py index 23d6bb1..1000fb5 100644 --- a/run.py +++ b/run.py @@ -28,5 +28,5 @@ async def on_error(event): tasks.load(bot) -bot.load_extensions_from("tasks") +bot.load_extensions_from("commands", "tasks") bot.run(activity=hikari.Activity(name=c.config["activity"], type=c.ACTIVITY) if c.config["activity"] else None) diff --git a/tasks/activity.py b/tasks/activity.py index e06fbc9..881c933 100644 --- a/tasks/activity.py +++ b/tasks/activity.py @@ -1,5 +1,4 @@ import datetime as dt -import sqlitedict import hikari import lightbulb from lightbulb.ext import tasks @@ -8,13 +7,12 @@ import config as c plugin = lightbulb.Plugin("activity") -db = sqlitedict.SqliteDict(c.config["db"], tablename=str(c.config["guild"]), autocommit=True) # Check every minute if inactive @tasks.task(s=60) async def check_activity(): - for author_id, timestamp in db.items(): + for author_id, timestamp in c.db.items(): if dt.datetime.now(dt.timezone.utc) - timestamp >= dt.timedelta(seconds=c.config["duration"]): member = plugin.bot.cache.get_member(c.config["guild"], author_id) or await plugin.bot.rest.fetch_member( c.config["guild"], author_id @@ -38,7 +36,7 @@ async def on_message(event): if event.is_bot or event.guild_id != c.config["guild"] or c.config["exclude"] in event.member.role_ids: return - db[event.author_id] = dt.datetime.now(dt.timezone.utc) # or event.message.timestamp + c.db[event.author_id] = dt.datetime.now(dt.timezone.utc) # or event.message.timestamp if c.config["active"] and c.config["active"] not in event.member.role_ids: await event.member.add_role(c.config["active"]) @@ -56,7 +54,7 @@ async def on_voice(event): ): return - db[event.state.user_id] = dt.datetime.now(dt.timezone.utc) + c.db[event.state.user_id] = dt.datetime.now(dt.timezone.utc) if c.config["active"] and c.config["active"] not in event.state.member.role_ids: await event.state.member.add_role(c.config["active"]) diff --git a/tools/components.py b/tools/components.py new file mode 100644 index 0000000..89bea81 --- /dev/null +++ b/tools/components.py @@ -0,0 +1,12 @@ +import hikari +from miru.ext import nav + + +class Back(nav.PrevButton): + def __init__(self): + super().__init__(style=hikari.ButtonStyle.SECONDARY, label="⟵", emoji=None) + + +class Forward(nav.NextButton): + def __init__(self): + super().__init__(style=hikari.ButtonStyle.SECONDARY, label="⟶", emoji=None)