initial commit for moderation cog

This commit is contained in:
SeaswimmerTheFsh 2023-06-20 12:28:40 -04:00
parent 1d0d08ba47
commit d51c26c9ca
2 changed files with 47 additions and 0 deletions

View file

@ -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())

45
moderation.py Normal file
View file

@ -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))