Skip to content

Commit

Permalink
feat: commands and tasks cog-split
Browse files Browse the repository at this point in the history
  • Loading branch information
Vyvy-vi committed Jul 15, 2021
1 parent 5b5019f commit 64eea80
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/commands/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from discord import Embed
from discord.ext import commands
from discord.ext.commands import Cog, Context

from src.consts import META, Colo


def HelpEmbed(title="Help", description="\u200b"):
return Embed(title=title, description=description, color=Colo.purple)


class CustomHelpCommand(commands.HelpCommand):
def get_cmd_usage(self, command):
cmd = "%s%s %s" % (self.clean_prefix, command.qualified_name, command.signature)
return f"`{cmd.strip()}`"

def get_help_text(self, command):
usage = self.get_cmd_usage(command)
return usage, command.short_doc

async def send_bot_help(self, mapping):
embed = HelpEmbed()
for cog, cmds in mapping.items():
filtered_cmds = await self.filter_commands(cmds, sort=True)
cmd_data = [self.get_cmd_usage(cmd) for cmd in filtered_cmds]
if cmd_data:
cog_name = getattr(cog, "qualified_name", "No Category")
cmd_data = "\n".join(cmd_data)
embed.add_field(
name=cog_name, value=f"{cog.description}\n{cmd_data}", inline=False
)
await self.context.send(embed=embed)

async def send_command_help(self, command):
embed = HelpEmbed(title=self.get_cmd_usage(command))
embed.add_field(name="Help for `{command.name}`", value=command.short_doc)
aliases = command.aliases
if aliases:
embed.add_field(name="Aliases", value=", ".join(aliases), inline=False)
await self.context.send(embed=embed)

async def send_cog_help(self, cog):
cog_name = getattr(cog, "qualified_name", "No Category")
embed = HelpEmbed(
f"Help for Category: `{cog_name}`",
f"Use `^help command` for more info on a command\n\n{cog.description}",
)
cmds = [
self.get_help_text(c)
for c in await self.filter_commands(cog.walk_commands(), sort=True)
]
if cmds:
for cmd in cmds:
embed.add_field(
name=cmd[0], value=f"{cog.description}{cmd[1]}", inline=False
)
await self.context.send(embed=embed)


class Helpers(Cog):
"""Help command and some other helper commands"""

def __init__(self, bot):
bot._default_help_command = bot.help_command
bot.help_command = CustomHelpCommand()
bot.help_command.cog = self
self.bot = bot

@Cog.listener()
async def on_ready(self):
print("Bot is online! Currently running version - v%s" % META["version"])

@commands.command()
async def ping(self, ctx: Context):
await ctx.send("Pong! Running version - v%s" % META["version"])


def setup(bot):
bot.add_cog(Helpers(bot))
62 changes: 62 additions & 0 deletions src/tasks/reminder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from datetime import datetime

from discord import Color, Embed
from discord import Member, User
from discord import Forbidden

from discord.ext import tasks
from discord.ext.commands import Cog
from motor.motor_asyncio import AsyncIOMotorClient as MotorClient

from src.consts import MONGO_URI, GUILD_ID
from typing import Union

reminder_text = "Don't forget to report your weekly progress in `#champion-ring`.\n\
You can do so by using the `^standup` command. Usage example-\n\
```\n\
^standup\n\
I did something. It accomplished that.\n\
I also did this, and it helped us do that.```"


def is_saturday():
return datetime.utcnow().date().weekday() == 5


# helper function to render Embed object for reminder
def reminder_embed(member: Union[Member, User]) -> Embed:
return Embed(
title="Reminder for weekly standup",
description=f"Hey, {member.mention}\n{reminder_text}",
color=Color.gold(),
timestamp=datetime.utcnow()
).set_footer(text="To opt out of these alerts, use the `^standup alerts` command")


class Reminder(Cog):
def __init__(self, bot):
self.bot = bot
self.DB = MotorClient(MONGO_URI).players.tasks
self.weekly_reminder.start()

@tasks.loop(hours=24.0)
async def weekly_reminder(self):
"""Sends people a weekly reminder on every Saturday, to submit standups"""
if is_saturday():
print('Saturday: Sending members reminder alerts')
guild = self.bot.get_guild(GUILD_ID)
async for member in self.DB.find({"alerts": True}, {"data": 0}):
try:
member = guild.get_member(int(member['_id']))
await member.send(embed=reminder_embed(member))
except Forbidden:
print(f"Can't DM member: {member}")

@weekly_reminder.before_loop
async def before_weekly_reminder(self):
print("Setting up Weekly Reminder Task")
await self.bot.wait_until_ready()


def setup(bot):
bot.add_cog(Reminder(bot))

0 comments on commit 64eea80

Please sign in to comment.