Skip to content

Instantly share code, notes, and snippets.

@an-dyy
Created December 21, 2021 00:06
Show Gist options
  • Save an-dyy/b8c81ce7f332d64e97519b07f95e76f0 to your computer and use it in GitHub Desktop.
Save an-dyy/b8c81ce7f332d64e97519b07f95e76f0 to your computer and use it in GitHub Desktop.
A modern mute command using the newly added `communication_disabled_until` property of member. Newly added to the Lefi DAPI wrapper
from __future__ import annotations
import datetime
from typing import TYPE_CHECKING
import lefi
from lefi.exts import commands
if TYPE_CHECKING:
from ..core import Bot
class Moderation(commands.Plugin):
VALID_UNITS: dict[str, str] = {"s": "seconds", "m": "minutes", "h": "hours"}
def __init__(self, bot: Bot) -> None:
self.bot = bot
@staticmethod
def check_role(ctx: commands.Context) -> bool:
if not isinstance(ctx.author, lefi.Member):
return False
if lefi.utils.get(ctx.author.roles, id=907106592938418276):
return True
return False
@commands.command()
@commands.check(lambda ctx: Moderation.check_role(ctx))
async def mute(
self, ctx: commands.Context, target: lefi.Member, timeout: str
) -> None | lefi.Message:
amount: str = timeout[:-1]
unit: str = timeout[-1]
if unit not in Moderation.VALID_UNITS or not amount.isdigit():
return await ctx.send(f"{unit!r} is not a valid time.")
name = Moderation.VALID_UNITS[unit]
actual_amount = int(amount)
time = datetime.timedelta(
seconds=actual_amount if name == "seconds" else 0,
minutes=actual_amount if name == "minutes" else 0,
hours=actual_amount if name == "hours" else 0,
)
embed = lefi.Embed(color=self.bot.config["BOT"]["COLOR"])
embed.description = f"Muted {target.username} for {timeout!r}"
await target.edit(timeout=datetime.datetime.utcnow() + time)
await ctx.send(embed=embed)
@an-dyy
Copy link
Author

an-dyy commented Dec 21, 2021

Very much liking this new feature; This new feature means that mutes persist even during downtime, and mutes also don't require a database anymore. 10/10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment