Browse Source

Added a way for users to mark that they no longer need help.

main
Nichole Mattera 9 months ago
parent
commit
09ed3b4833
2 changed files with 105 additions and 28 deletions
  1. +91
    -10
      cogs/mod_mail.py
  2. +14
    -18
      helpers/userlogs.py

+ 91
- 10
cogs/mod_mail.py View File

@ -2,7 +2,8 @@ import config
import discord
from discord.ext import commands
from discord.ext.commands import Cog
from helpers.userlogs import get_userlog, userlog
from helpers.userlogs import get_blank_userlog, get_userlog, set_userlog
import json
import time
@ -10,15 +11,59 @@ class ModMail(Cog):
def __init__(self, bot):
self.bot = bot
def add_mail_log(self, uid, message):
userlogs = get_userlog()
uid = str(uid)
if uid not in userlogs:
userlogs[uid] = get_blank_userlog()
userlogs[uid]["mail"].append(message)
set_userlog(json.dumps(userlogs))
def build_embed(self, author, message):
embed = discord.Embed(description=message["body"],)
embed.set_author(
name=f"{author.name}#{author.discriminator}", icon_url=author.avatar_url
)
embed.colour = self.get_message_color(message)
embed.set_footer(text=f"{self.get_message_icon(message)} - {author.id}")
return embed
def get_message_color(self, message):
if message["resolved"] == True:
return 0x4CAF50
elif message["replier_id"] != 0:
return 0x2196F3
else:
return 0xE91E63
def get_message_icon(self, message):
if message["resolved"] == True:
return "✔️"
elif message["replier_id"] != 0:
return "↩️"
else:
return "✉️"
# Commands
@commands.command(aliases=["creport"])
async def modmail(self, ctx, *, body: str = ""):
"""Sends a modmail"""
"""Sends a mod mail message"""
# We should probably delete the message for privacy.
await ctx.message.delete()
# Prevent sending of blank messages.
if len(body.strip()) == 0:
await ctx.send("A message can not be empty.")
return
if len(body.strip()) > 2048:
await ctx.send("A message can not be longer than 2048 characters.")
return
logs = get_userlog()
uid = str(ctx.author.id)
@ -42,18 +87,54 @@ class ModMail(Cog):
)
return
message = {
"body": body.strip(),
"timestamp": int(time.time()),
"resolved": False,
"replier_id": 0,
"replier_name": "",
"message_id": 0,
}
# Send message
modmail_channel = self.bot.get_channel(config.modmail_channel)
message["message_id"] = (
await modmail_channel.send(embed=self.build_embed(ctx.author, message))
).id
# Log messages to the userlog.
userlog(uid, None, body, "mail", ctx.author.name)
self.add_mail_log(uid, message)
await ctx.send(f"{ctx.author.mention} - Message sent.")
@commands.command(aliases=["solved", "completed"])
async def resolved(self, ctx):
"""Marks your last mod mail message as resolved"""
# We should probably delete the message for privacy.
await ctx.message.delete()
logs = get_userlog()
uid = str(ctx.author.id)
if uid not in logs or "mail" not in logs[uid] or len(logs[uid]["mail"]) == 0:
await ctx.send("No mod mail message to mark as resolved.")
return
message = logs[uid]["mail"][-1]
if message["resolved"]:
await ctx.send("Last mod mail message is already marked as resolved.")
return
message["resolved"] = True
set_userlog(logs)
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.set_thumbnail(url=ctx.author.avatar_url)
modmail_message = await modmail_channel.fetch_message(message["message_id"])
await modmail_message.edit(embed=self.build_embed(ctx.author, message))
await modmail_channel.send(embed=embed)
await ctx.send("Message sent.")
await ctx.send(f"{ctx.author.mention} - Message marked as resolved.")
def setup(bot):


+ 14
- 18
helpers/userlogs.py View File

@ -9,6 +9,18 @@ userlog_event_types = {"warns": "Warn",
"mail": "Mail"}
def get_blank_userlog():
return {"warns": [],
"mutes": [],
"kicks": [],
"bans": [],
"notes": [],
"mail": [],
"mail_blocked": False,
"watch": False,
"name": "n/a"}
def get_userlog():
with open("data/userlog.json", "r") as f:
return json.load(f)
@ -23,15 +35,7 @@ def userlog(uid, issuer, reason, event_type, uname: str = ""):
userlogs = get_userlog()
uid = str(uid)
if uid not in userlogs:
userlogs[uid] = {"warns": [],
"mutes": [],
"kicks": [],
"bans": [],
"notes": [],
"mail": [],
"mail_blocked": False,
"watch": False,
"name": "n/a"}
userlogs[uid] = self.get_blank_userlog()
if uname:
userlogs[uid]["name"] = uname
@ -59,15 +63,7 @@ def setwatch(uid, issuer, watch_state, uname: str = ""):
uid = str(uid)
# Can we reduce code repetition here?
if uid not in userlogs:
userlogs[uid] = {"warns": [],
"mutes": [],
"kicks": [],
"bans": [],
"notes": [],
"mail": [],
"mail_blocked": False,
"watch": False,
"name": "n/a"}
userlogs[uid] = self.get_blank_userlog()
if uname:
userlogs[uid]["name"] = uname


Loading…
Cancel
Save