diff --git a/main.py b/main.py index 0e02e9b..5565361 100644 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ import revolt from revolt.ext import commands import dotenv from dotenv import load_dotenv +from moderation import Moderation # This code reads the variables set in the bot's '.env' file. env = dotenv.find_dotenv() @@ -77,6 +78,7 @@ async def main(): # This function logs into the bot user. async with aiohttp.ClientSession() as session: client = Client(session, token, api_url=api_url) + client.add_cog(Moderation(client)) await client.start() asyncio.run(main()) diff --git a/moderation.py b/moderation.py new file mode 100644 index 0000000..d06fd43 --- /dev/null +++ b/moderation.py @@ -0,0 +1,45 @@ +import asyncio +import os +import aiohttp +import revolt +from revolt.ext import commands +from datetime import timedelta + +class Moderation(commands.Cog): + def __init__(self, bot): + self.bot = bot + + def parse_timedelta(input_str): + # Split the string into its components (e.g., "1 day 3 hours" becomes ["1", "day", "3", "hours"]) + components = input_str.split() + + # Define a dictionary to map time units to their corresponding `timedelta` attribute + units = {"days": "days", "hours": "hours", "minutes": "minutes", "seconds": "seconds"} + + # Iterate over the components, taking pairs of values and units + values_units = zip(components[::2], components[1::2]) + + # Initialize a dictionary to store the values for each unit + values = {} + + # Parse the values and units into the dictionary + for value, unit in values_units: + # Convert the value to an integer + value = int(value) + # Map the unit to the corresponding `timedelta` attribute and store the value + values[units[unit]] = value + + # Create and return the `timedelta` object + return timedelta(**values) + + + @commands.command(name="mute", aliases="timeout") + async def mute(self, ctx, target: revolt.Member, duration: str = "1 hour"): + parsed_time = Moderation.parse_timedelta(duration) + await target.timeout(parsed_time) + await ctx.message.reply(f"{target.mention} has been timed out for {str(parsed_time)}!") + + @commands.command() + async def timedeltaconvert(self, ctx, *, duration: str = "1 hour"): + parsed_time = Moderation.parse_timedelta(duration) + await ctx.send(str(parsed_time))