Ruby-Cogs/referrals/common/checks.py
2025-05-23 02:30:00 -04:00

24 lines
617 B
Python

from discord.ext.commands.core import check
from redbot.core import commands
def ensure_db_connection():
"""Decorator to ensure a database connection is active.
Example:
```python
@ensure_db_connection()
@commands.command()
async def mycommand(self, ctx):
await ctx.send("Database connection is active")
```
"""
async def predicate(ctx: commands.Context) -> bool:
if not ctx.cog.db:
txt = "Database connection is not active, try again later"
raise commands.UserFeedbackCheckFailure(txt)
return True
return check(predicate)