Browse Source

Added user feedback, rate limiting, and spam prevention.

main
Nichole Mattera 9 months ago
parent
commit
d6a0922dc5
2 changed files with 53 additions and 10 deletions
  1. +34
    -4
      cogs/mod_mail.py
  2. +19
    -6
      helpers/userlogs.py

+ 34
- 4
cogs/mod_mail.py View File

@ -1,23 +1,53 @@
import config
import discord
from discord.ext import commands
from discord.ext.commands import Cog
from helpers.userlogs import get_userlog, userlog
import time
class ModMail(Cog):
def __init__(self, bot):
self.bot = bot
@commands.command(aliases=['creport'])
@commands.command(aliases=["creport"])
async def modmail(self, ctx, *, body: str = ""):
"""Sends a modmail"""
logs = get_userlog()
uid = str(ctx.author.id)
# Get the timeout from the config and default it to 15 seconds.
timeout = getattr(config, "modmail_timeout", 15)
if uid in logs and "mail" in logs[uid] and len(logs[uid]["mail"]) != 0:
last_message = logs[uid]["mail"][-1]
# Prevents sending the same message.
if last_message["body"].strip() == body.strip():
await ctx.send("Unable to send message.")
return
# Rate limit messages.
delta_time = int(time.time()) - last_message["timestamp"]
if delta_time < timeout:
await ctx.send(
f"Please wait {timeout - delta_time} seconds before sending another message."
)
return
# Log messages to the userlog.
userlog(uid, None, body, "mail", ctx.author.name)
modmail_channel = self.bot.get_channel(config.modmail_channel)
embed = discord.Embed(title=f"New modmail from {ctx.author.name}!",
description=f"**Content:** {body}")
embed = discord.Embed(
title=f"New modmail from {ctx.author.name}!",
description=f"**Content:** {body}",
)
embed.set_thumbnail(url=ctx.author.avatar_url)
await modmail_channel.send(embed=embed)
await ctx.send("Message sent.")
def setup(bot):


+ 19
- 6
helpers/userlogs.py View File

@ -5,7 +5,8 @@ userlog_event_types = {"warns": "Warn",
"bans": "Ban",
"kicks": "Kick",
"mutes": "Mute",
"notes": "Note"}
"notes": "Note",
"mail": "Mail"}
def get_userlog():
@ -27,15 +28,25 @@ def userlog(uid, issuer, reason, event_type, uname: str = ""):
"kicks": [],
"bans": [],
"notes": [],
"mail": [],
"mail_blocked": False,
"watch": False,
"name": "n/a"}
if uname:
userlogs[uid]["name"] = uname
timestamp = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
log_data = {"issuer_id": issuer.id,
"issuer_name": f"{issuer}",
"reason": reason,
"timestamp": timestamp}
log_data = {}
if event_type == "mail":
log_data = {"body": reason,
"timestamp": int(time.time())
"resolved": False,
"replier_id": 0,
"replier_name": ""}
else:
log_data = {"issuer_id": issuer.id,
"issuer_name": f"{issuer}",
"reason": reason,
"timestamp": time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())}
if event_type not in userlogs[uid]:
userlogs[uid][event_type] = []
userlogs[uid][event_type].append(log_data)
@ -53,6 +64,8 @@ def setwatch(uid, issuer, watch_state, uname: str = ""):
"kicks": [],
"bans": [],
"notes": [],
"mail": [],
"mail_blocked": False,
"watch": False,
"name": "n/a"}
if uname:


Loading…
Cancel
Save