add activitylog cog, removed uneeded code in punish

This commit is contained in:
brandons209 2020-01-26 21:41:06 -05:00
parent 5e63f0ada4
commit d27a8d0af0
5 changed files with 1739 additions and 6 deletions

6
activitylog/__init__.py Normal file
View file

@ -0,0 +1,6 @@
from .activitylog import ActivityLogger
async def setup(bot):
n = ActivityLogger(bot)
await n.initialize()
bot.add_cog(n)

1652
activitylog/activitylog.py Normal file

File diff suppressed because it is too large Load diff

22
activitylog/info.json Normal file
View file

@ -0,0 +1,22 @@
{
"author": [
"Brandons209",
"calebj"
],
"bot_version": [
3,
0,
0
],
"description": "Log messages, dms, attachments, guild updates (roles, channels, bans, kicks etc) and be able to retrieve these logs for review. Saves these right to disk and customize then to rotate log files. Tracks user's stats; how many messages they send, how many are bot commands, time spent in VC, and moderator actions against them. Also provides an upgraded userinfo command that gives these stats alongside the information Red's userinfo command gives. This is rewritten from @calebj's V2 cog.",
"hidden": false,
"install_msg": "Thank you for using this cog! Make sure to set bot prefixes using [p]logset prefixes for proper stat logging.",
"requirements": ["python-dateutil", "pytz"],
"short": "Log messages, audit actions, and track user statistics.",
"tags": [
"brandons209",
"logging",
"log",
"activity"
]
}

57
activitylog/time_utils.py Normal file
View file

@ -0,0 +1,57 @@
# thanks to @Sinbad for time parsing!
from __future__ import annotations
import re
from datetime import datetime as dt, timedelta
from typing import Optional
import pytz
from dateutil import parser
from dateutil.tz import gettz
TIME_RE_STRING = r"\s?".join(
[
r"((?P<weeks>\d+?)\s?(weeks?|w))?",
r"((?P<days>\d+?)\s?(days?|d))?",
r"((?P<hours>\d+?)\s?(hours?|hrs|hr?))?",
r"((?P<minutes>\d+?)\s?(minutes?|mins?|m(?!o)))?", # prevent matching "months"
r"((?P<seconds>\d+?)\s?(seconds?|secs?|s))?",
]
)
TIME_RE = re.compile(TIME_RE_STRING, re.I)
def gen_tzinfos():
for zone in pytz.common_timezones:
try:
tzdate = pytz.timezone(zone).localize(dt.utcnow(), is_dst=None)
except pytz.NonExistentTimeError:
pass
else:
tzinfo = gettz(zone)
if tzinfo:
yield tzdate.tzname(), tzinfo
def parse_time(datetimestring: str):
tzinfo = dict(gen_tzinfos())
ret = parser.parse(datetimestring, tzinfos=tzinfo)
if ret.tzinfo is not None:
ret = ret.astimezone(pytz.utc)
return ret
def parse_time_naive(datetimestring: str):
return parser.parse(datetimestring)
def parse_timedelta(argument: str) -> Optional[timedelta]:
matches = TIME_RE.match(argument)
if matches:
params = {k: int(v) for k, v in matches.groupdict().items() if v}
if params:
return timedelta(**params)
return None

View file

@ -79,7 +79,6 @@ class Punish(commands.Cog):
"default_setting": True,
"image": "\N{HOURGLASS WITH FLOWING SAND}\N{SPEAKER WITH CANCELLATION STROKE}",
"case_str": "Timed Mute",
"audit_type": "member_update",
}
try:
await modlog.register_casetype(**punish_case)
@ -1345,9 +1344,6 @@ class Punish(commands.Cog):
@commands.Cog.listener()
async def on_guild_channel_create(self, channel):
"""Run when new channels are created and set up role permissions"""
if channel.is_private:
return
role = await self.get_role(channel.guild, quiet=True)
if not role:
return
@ -1356,11 +1352,11 @@ class Punish(commands.Cog):
@commands.Cog.listener()
async def on_member_update(self, before, after):
"""Remove scheduled unpunish when manually removed"""
"""Remove scheduled unpunish when manually removed role"""
try:
assert before.roles != after.roles
guild_data = await self.config.guild(before.guild).PUNISHED()
member_data = guild_data[str(before.id)]
assert before.roles != after.roles
role = await self.get_role(before.guild, quiet=True)
assert role
except (KeyError, AssertionError):