diff --git a/.github/dependabot.yml b/.github/dependabot.yml index 78fb9dd..f4952ba 100644 --- a/.github/dependabot.yml +++ b/.github/dependabot.yml @@ -6,4 +6,3 @@ updates: interval: daily time: "10:00" open-pull-requests-limit: 10 - diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c24ecd1..23b136a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -20,12 +20,12 @@ jobs: uses: actions/setup-python@v2 with: python-version: 3.8 - + - name: Install dev dependencies run: | python -m pip install --upgrade pip pip install -r requirements.dev.txt - + - name: Check code-formatting with Black run: | python -m black . --check --diff @@ -35,18 +35,18 @@ jobs: - name: Test code with Pytest run: | echo "No tests found..." - + cog_loader_test: # loads cogs, to check if they have any issues runs-on: ubuntu-latest - + steps: - uses: actions/checkout@v2 - name: Set up Python 3.8 uses: actions/setup-python@v2 with: python-version: 3.8 - + - name: Install dependencies run: | python -m pip install --upgrade pip diff --git a/CHANGELOG.md b/CHANGELOG.md index 04ce08c..15c7d0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,3 @@ * Standup command for logging goals ([97b5182](https://github.com/MetaFam/shepherd-bot/commit/97b5182bef0de5f88c078acb09bb543f6487601a)) * versioning ([6f9b865](https://github.com/MetaFam/shepherd-bot/commit/6f9b86570fb45f77f8a6eef38ba79e82dbef61a7)) * versioning ([8061d6e](https://github.com/MetaFam/shepherd-bot/commit/8061d6e5565c5a8440e83ee194e6662b2bf6053c)) - - - diff --git a/meta.json b/meta.json index d140708..9f8c07f 100644 --- a/meta.json +++ b/meta.json @@ -9,4 +9,4 @@ "privacy_policy": "", "terms_and_conditions": "", "latest_image": "" -} \ No newline at end of file +} diff --git a/src/commands/necromancy.py b/src/commands/necromancy.py new file mode 100644 index 0000000..a14b4ac --- /dev/null +++ b/src/commands/necromancy.py @@ -0,0 +1,148 @@ +from datetime import datetime +from typing import List, Optional, Union + +from discord import Color, Embed, Member, User +from discord.ext import commands +from discord.ext.commands import Cog, Context +from motor.motor_asyncio import AsyncIOMotorClient as MotorClient + +from src.consts import ACTIVE_ROLE_ID, MONGO_URI + +# GUILD_ID +# import csv + + +def get_last_active(last_active): + date = datetime.fromtimestamp(last_active).strftime("%b-%d-%Y") + days = (datetime.now() - last_active).days + return f"Last active on {date} ({days} days ago...)" + + +def get_xp(self, member: Union[Member, User]): + return None + + +class Necromancy(Cog): + """ + Cog to parse user activity stats from the server. + And implements tooling for the Nacromancers + (This is a temporary implementation, and may be shifted to Jergal) + """ + + def __init__(self, bot): + self.bot = bot + self.DB = MotorClient(MONGO_URI).players.logs + + @commands.command() + async def necro( + self, ctx: Context, member: Union[Member, User], action: Optional[str] + ): + """Command for Necromancy Utilities""" + if not action: + await self.get_activity(ctx, member) + elif action == "active": + await self.get_last_active(ctx, member) + elif action == "xp": + await ctx.send( + embed=Embed( + description="That action is currently not available", + colour=Color.red(), + ) + ) + else: + await ctx.send( + embed=Embed( + description="That action does not exist(as of _yet_).", + color=Color.red(), + ) + ) + + async def get_activity(self, ctx: Context, member: Union[Member, User]): + record = await self.DB_find_one({"_id": str(member.id)}) + if record: + stats = f"**Last active**: {get_last_active(record['last_active_at'])}\n\ +**Total Message Count**: {record['message_count']}\n\ +**XP since last purge**: {get_xp(member)}\n" + await ctx.send( + embed=Embed( + description=f"Activity stats for {member.mention}\n{stats}", + colour=Color.green(), + ) + ) + else: + await ctx.send( + embed=Embed( + description="There appears to be no instance of this user in the DB.", + colour=Color.red(), + ) + ) + + async def get_last_active(self, ctx: Context, member: Union[Member, User]): + record = await self.DB.find_one({"_id": str(member.id)}) + if record: + await ctx.send( + f"{member.mention} was {get_last_active(record['last_active'])}" + ) + else: + await ctx.send( + embed=Embed( + description="There appears to be no instance of this user in the DB.", + colour=Color.red(), + ) + ) + + async def update_status( + self, member_id: str, created_at: datetime, status: List[str] + ): + try: + record = await self.DB.find_one({"_id": member_id}) + if record: + await self.DB.update_one(record, {"$set": {"status": status}}) + else: + await self.DB.insert_one( + { + "_id": member_id, + "status": status, + "message_count": 0, + "xp": 0, + "last_active": created_at, + } + ) + except Exception as e: + print(f"ERROR - {e}") + + @commands.has_role("Necromancer") + @commands.command(aliases=["fallen"]) + async def slay( + self, ctx: Context, member: Union[Member, User], *, reason: Optional[str] + ): + try: + active_role = ctx.guild.get_role(ACTIVE_ROLE_ID) + await member.remove_roles(active_role, reason=reason, atomic=True) + + await self.update_status( + member.id, ctx.guild.created_at, ["fallen", f"Reason- {reason}"] + ) + except Exception as e: + print(e) + await ctx.send(f"Some error occured\n{e}") + + @commands.has_role("Necromancer") + @commands.command(aliases=["chosen"]) + async def promote( + self, ctx: Context, member: Union[Member, User], *, reason: Optional[str] + ): + try: + active_role = ctx.guild.get_role(ACTIVE_ROLE_ID) + await member.add_roles(active_role, reason=reason, atomic=True) + + await self.update_status( + member.id, ctx.guild.created_at, ["fallen", f"Reason- {reason}"] + ) + except Exception as e: + print(e) + await ctx.send(f"Some error occured\n{e}") + + +def setup(bot): + bot.add_cog(Necromancy(bot))