mirror of https://github.com/sgoudham/Enso-Bot.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
741 lines
32 KiB
Python
741 lines
32 KiB
Python
5 years ago
|
import asyncio
|
||
|
import datetime
|
||
5 years ago
|
import os
|
||
5 years ago
|
import random
|
||
|
|
||
|
import discord
|
||
4 years ago
|
import mariadb
|
||
5 years ago
|
from discord import Embed
|
||
5 years ago
|
from discord import File
|
||
4 years ago
|
from discord.ext.commands import has_permissions, Cog, group, bot_has_permissions
|
||
5 years ago
|
|
||
|
import db
|
||
4 years ago
|
from db import connection
|
||
|
from settings import enso_embedmod_colours, blank_space, storage_modlog_for_guild, remove_modlog_channel
|
||
5 years ago
|
|
||
|
|
||
5 years ago
|
# Method to ask the user if they want to be anonymous or not
|
||
5 years ago
|
def AnonOrNot(author):
|
||
|
# Set up embed to let the user how to start sending modmail
|
||
|
AnonModMailEmbed = Embed(title="**Want to send it Anonymously?**",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
|
||
|
AnonModMailEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
AnonModMailEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
5 years ago
|
fields = [(blank_space, "**We understand that for some things,"
|
||
|
"you may want to remain Anonymous."
|
||
|
"\nUse the reactions below to choose!**", False),
|
||
5 years ago
|
(blank_space, "**Use :white_check_mark: for** `Yes`", True),
|
||
|
(blank_space, "**Use :x: for** `No`", True),
|
||
|
(blank_space, blank_space, True),
|
||
|
(blank_space,
|
||
5 years ago
|
"The Staff will not know who is sending this"
|
||
|
"\nPurely negative feedback will not be considered.", True)]
|
||
5 years ago
|
|
||
|
for name, value, inline in fields:
|
||
|
AnonModMailEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return AnonModMailEmbed
|
||
|
|
||
|
|
||
|
# Method to send an embed to to let the user know to type into chat
|
||
|
def SendInstructions(author):
|
||
|
# Set up embed to let the user know that they have aborted the modmail
|
||
|
SendModMailEmbed = Embed(title="**Please enter a message for it to be sent to the staff!**",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
|
||
|
SendModMailEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
SendModMailEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
5 years ago
|
fields = [("**Make sure that the message is above **50** and below **1024** characters!**",
|
||
5 years ago
|
"**Include as much detail as possible :P**",
|
||
|
False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
SendModMailEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return SendModMailEmbed
|
||
|
|
||
|
|
||
|
# Method to let the user know that the message must be above 50 characters
|
||
|
def ErrorHandling(author):
|
||
|
# Set up embed to let the user know that the message must be above 50 characters
|
||
5 years ago
|
ErrorHandlingEmbed = Embed(
|
||
|
title="Uh Oh! Please make sure the message is above **50** and below **1024** characters!",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
ErrorHandlingEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
ErrorHandlingEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
5 years ago
|
fields = [("Please enter in a message which is above **50** and below **1024** characters!",
|
||
5 years ago
|
"**This helps us reduce spam and allows you to include more detail in your mail!**",
|
||
|
False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
ErrorHandlingEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return ErrorHandlingEmbed
|
||
|
|
||
|
|
||
|
# Method to send an embed into chat to let the user know that their mail has been sent successfully
|
||
|
def MessageSentConfirmation(author):
|
||
|
# Set up embed to let the user know that they have sent the mail
|
||
|
ConfirmationEmbed = Embed(title="**Message relayed to Staff!!**",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
|
||
|
ConfirmationEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
ConfirmationEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
|
fields = [("Thank you for your input! The staff team appreciate it very much!",
|
||
4 years ago
|
f"\n As mentioned previously, please don't be hesistant to DM the Staff for anything! :P",
|
||
5 years ago
|
False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
ConfirmationEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return ConfirmationEmbed
|
||
|
|
||
|
|
||
|
# Method to actually allow the message to be sent to #mod-mail
|
||
|
def SendMsgToModMail(self, msg, author):
|
||
|
if self.anon:
|
||
|
|
||
|
avatars = ["https://cdn.discordapp.com/embed/avatars/0.png",
|
||
|
"https://cdn.discordapp.com/embed/avatars/1.png",
|
||
|
"https://cdn.discordapp.com/embed/avatars/2.png",
|
||
|
"https://cdn.discordapp.com/embed/avatars/3.png",
|
||
|
"https://cdn.discordapp.com/embed/avatars/4.png"]
|
||
|
|
||
|
embed = Embed(title="Modmail",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
|
||
|
embed.set_thumbnail(url=random.choice(avatars))
|
||
|
embed.set_footer(text=f"Sent By Anon Member")
|
||
|
|
||
|
fields = [("Member", "Anon Member", False),
|
||
|
("Message", msg.content, False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
embed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return embed
|
||
|
|
||
|
else:
|
||
|
embed = Embed(title="Modmail",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
|
||
|
embed.set_thumbnail(url=author.avatar_url)
|
||
|
embed.set_footer(text=f"Sent By {author}")
|
||
|
|
||
|
fields = [("Member", author, False),
|
||
|
("Message", msg.content, False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
embed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return embed
|
||
|
|
||
|
|
||
|
# Set up the Cog
|
||
4 years ago
|
class Guild(Cog):
|
||
4 years ago
|
"""Modmail System!"""
|
||
4 years ago
|
|
||
5 years ago
|
def __init__(self, bot):
|
||
|
self.bot = bot
|
||
|
self.anon = None
|
||
|
|
||
4 years ago
|
@Cog.listener()
|
||
4 years ago
|
async def on_ready(self):
|
||
|
"""Printing out that Cog is ready on startup"""
|
||
|
print(f"{self.__class__.__name__} Cog has been loaded\n-----")
|
||
|
|
||
4 years ago
|
@group(invoke_without_command=True, usage="`[argument...]`")
|
||
|
@has_permissions(manage_guild=True)
|
||
|
@bot_has_permissions(administrator=True)
|
||
|
async def modlogs(self, ctx):
|
||
|
"""Setup/Update/Delete Modlogs System"""
|
||
|
pass
|
||
|
|
||
|
@modlogs.command()
|
||
|
@has_permissions(manage_guild=True)
|
||
|
@bot_has_permissions(administrator=True)
|
||
|
async def setup(self, ctx, channelID: int):
|
||
|
"""Setup a Channel for the Kick/Ban/Mute Actions to be Logged In"""
|
||
|
|
||
|
# Retrieve a list of channel id's in the guild
|
||
|
channels = [channel.id for channel in ctx.guild.channels]
|
||
|
|
||
|
# Setup pool
|
||
|
pool = await connection(db.loop)
|
||
|
|
||
|
# Setup pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the row of the guild
|
||
|
select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
|
||
|
val = ctx.guild.id,
|
||
|
|
||
|
# Execute the SQL Query
|
||
|
await cur.execute(select_query, val)
|
||
|
result = await cur.fetchone()
|
||
|
|
||
|
# Throw error if the modlog channel already exists and then stop the function
|
||
|
if result[2] is not None:
|
||
|
await ctx.send("Looks like this guild already has a **Modlogs Channel** set up!" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
|
return
|
||
|
|
||
|
# Abort the process if the channel does not exist within the guild
|
||
|
if channelID not in channels:
|
||
|
await ctx.send("**Invalid ChannelID Detected... Aborting Process**")
|
||
|
|
||
|
else:
|
||
|
# Set up the modlogs channel within the guild
|
||
|
mod_log_setup = True
|
||
|
await storage_modlog_for_guild(ctx, channelID, mod_log_setup)
|
||
|
|
||
|
@modlogs.command()
|
||
|
@has_permissions(manage_guild=True)
|
||
|
@bot_has_permissions(administrator=True)
|
||
|
async def update(self, ctx, channelID: int):
|
||
|
"""Change the Channel that your Modlogs are Sent to"""
|
||
|
|
||
|
# Retrieve a list of channel id's in the guild
|
||
|
channels = [channel.id for channel in ctx.guild.channels]
|
||
|
|
||
|
# Setup pool
|
||
|
pool = await connection(db.loop)
|
||
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the guilds row from the guilds table
|
||
|
select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
|
||
|
vals = ctx.guild.id,
|
||
|
|
||
|
# Execute the SQL Query
|
||
|
await cur.execute(select_query, vals)
|
||
|
result = await cur.fetchone()
|
||
|
|
||
|
# Throw error if the modlog channel already exists and then stop the function
|
||
|
if result[2] is None:
|
||
|
await ctx.send("Looks like this guild has not setup a **Modlogs Channel**" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
|
return
|
||
|
|
||
|
# Abort the process if the channel does not exist within the guild
|
||
|
if channelID not in channels:
|
||
|
await ctx.send("**Invalid ChannelID Detected... Aborting Process**")
|
||
|
|
||
|
else:
|
||
|
# Update the modlog channel within the database and cache
|
||
|
mod_log_setup = False
|
||
|
await storage_modlog_for_guild(ctx, channelID, mod_log_setup)
|
||
|
|
||
|
@modlogs.command()
|
||
|
@has_permissions(manage_guild=True)
|
||
|
@bot_has_permissions(administrator=True)
|
||
|
async def delete(self, ctx):
|
||
|
"""Delete the Existing Modlogs System"""
|
||
|
|
||
|
# Setup pool
|
||
|
pool = await connection(db.loop)
|
||
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the guilds row from the guilds table
|
||
|
select_query = """SELECT * FROM guilds WHERE guildID = (%s)"""
|
||
|
vals = ctx.guild.id,
|
||
|
|
||
|
# Execute the SQL Query
|
||
|
await cur.execute(select_query, vals)
|
||
|
result = await cur.fetchone()
|
||
|
|
||
|
# Throw error if the modlog channel already exists and then stop the function
|
||
|
if result[2] is None:
|
||
|
await ctx.send("Looks like this guild has not setup a **Modlogs Channel**" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
|
return
|
||
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Update the existing prefix within the database
|
||
|
update_query = """UPDATE guilds SET modlogs = NULL WHERE guildID = (%s)"""
|
||
|
update_vals = ctx.guild.id,
|
||
|
|
||
|
# Execute the query
|
||
|
await cur.execute(update_query, update_vals)
|
||
|
await conn.commit()
|
||
|
|
||
|
# Delete channel from cache
|
||
|
remove_modlog_channel(str(ctx.guild.id))
|
||
|
|
||
|
# Sending confirmation message that the modmail system has been deleted
|
||
|
await ctx.send("**Modlogs System** successfully deleted!" +
|
||
|
f"\nPlease do **{ctx.prefix}help** to find out how to set Modmail again!")
|
||
|
|
||
4 years ago
|
@group(invoke_without_command=True, usage="`[argument...]`")
|
||
4 years ago
|
@has_permissions(administrator=True)
|
||
4 years ago
|
@bot_has_permissions(administrator=True)
|
||
4 years ago
|
async def modmail(self, ctx):
|
||
4 years ago
|
"""Setup/Update/Delete Modmail System"""
|
||
4 years ago
|
pass
|
||
|
|
||
4 years ago
|
@modmail.command()
|
||
4 years ago
|
@has_permissions(administrator=True)
|
||
4 years ago
|
@bot_has_permissions(embed_links=True, read_messages=True, manage_messages=True,
|
||
|
manage_channels=True, read_message_history=True,
|
||
|
send_messages=True, attach_files=True)
|
||
4 years ago
|
async def setup(self, ctx, channelID: int):
|
||
4 years ago
|
"""
|
||
|
Setup Modmail System
|
||
|
Input the ID of the Channel where the Modmail will be sent
|
||
|
"""
|
||
4 years ago
|
|
||
|
# Retrieve a list of channel id's in the guild
|
||
|
channels = [channel.id for channel in ctx.guild.channels]
|
||
|
|
||
4 years ago
|
# Setup pool
|
||
4 years ago
|
pool = await connection(db.loop)
|
||
4 years ago
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the author's row from the Members Table
|
||
|
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
|
||
|
val = ctx.author.guild.id,
|
||
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(select_query, val)
|
||
|
result = await cur.fetchone()
|
||
4 years ago
|
|
||
4 years ago
|
# Throw error if the guild already exists and then stop the function
|
||
|
if result is not None:
|
||
|
await ctx.send("Looks like this guild already has a **Modmail System** set up!" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
|
return
|
||
4 years ago
|
|
||
|
# As long as the channel exists within the guild
|
||
|
if channelID in channels:
|
||
|
|
||
|
# Ask for the channel ID that the modmail should be logged to
|
||
|
await ctx.send("**Please enter the ID of the channel you want your modmail to be sent**")
|
||
|
|
||
|
# Check the response is from the author and from the same channel as the previous message
|
||
|
def check(m):
|
||
|
return m.author == ctx.author and m.channel == ctx.channel
|
||
|
|
||
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check)
|
||
|
|
||
|
# As long as the channel exists within the guild
|
||
|
if int(msg.content) in channels:
|
||
|
|
||
|
# Set up embed to let the user how to start sending modmail
|
||
|
ModMail = Embed(title="**Welcome to Modmail!**",
|
||
|
colour=enso_embedmod_colours,
|
||
|
timestamp=datetime.datetime.utcnow())
|
||
|
|
||
|
ModMail.set_thumbnail(url=self.bot.user.avatar_url)
|
||
|
|
||
|
# Define fields to be inserted into the embed
|
||
|
fields = [
|
||
|
("React to this message if you want to send a message to the Staff Team!",
|
||
|
"**React with ✅**", False),
|
||
|
("We encourage all suggestions/thoughts and opinions on the server!" +
|
||
|
"\nAs long as it is **valid** criticism.",
|
||
|
"**Purely negative feedback will not be considered**", False)]
|
||
|
|
||
|
# Add the fields to the embed
|
||
|
for name, value, inline in fields:
|
||
|
ModMail.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
try:
|
||
|
# Get the channel object from the channelID input by the user
|
||
|
channel = ctx.author.guild.get_channel(channelID)
|
||
|
modmailchannelID = await channel.send(embed=ModMail)
|
||
|
# Auto add the ✅ reaction
|
||
|
await modmailchannelID.add_reaction('✅')
|
||
|
|
||
4 years ago
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Define the insert statement that will insert information about the modmail channel
|
||
|
insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (%s, %s, %s, %s)"""
|
||
|
vals = ctx.author.guild.id, channelID, modmailchannelID.id, int(msg.content),
|
||
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(insert_query, vals)
|
||
|
await conn.commit()
|
||
4 years ago
|
|
||
4 years ago
|
await ctx.send("Your **Modmail System** is now successfully set up!" +
|
||
|
f"\nPlease refer to **{ctx.prefix}help** for any information")
|
||
|
return
|
||
4 years ago
|
|
||
|
except mariadb.IntegrityError as err:
|
||
|
print(err)
|
||
4 years ago
|
await ctx.send("Looks like this guild already has a **Modmail System** set up!" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
4 years ago
|
else:
|
||
|
# Send error message if the channel ID is not recognised
|
||
|
await ctx.send("**Invalid Channel ID. Aborting Process...**")
|
||
|
return
|
||
|
else:
|
||
|
# Send error message if the channel ID is not recognised
|
||
|
await ctx.send("**Invalid Channel ID. Aborting Process...**")
|
||
|
return
|
||
|
|
||
4 years ago
|
@modmail.command()
|
||
4 years ago
|
@has_permissions(administrator=True)
|
||
4 years ago
|
@bot_has_permissions(administrator=True)
|
||
4 years ago
|
async def update(self, ctx, channelID: int):
|
||
4 years ago
|
"""
|
||
|
Update the Channel that the Modmail is logged to
|
||
|
Input the ID of the New Channel
|
||
|
"""
|
||
4 years ago
|
|
||
|
# Retrieve a list of channel id's in the guild
|
||
|
channels = [channel.id for channel in ctx.guild.channels]
|
||
|
|
||
4 years ago
|
# Setup pool
|
||
4 years ago
|
pool = await connection(db.loop)
|
||
4 years ago
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the author's row from the Members Table
|
||
|
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
|
||
|
vals = ctx.author.guild.id,
|
||
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(select_query, vals)
|
||
|
result = await cur.fetchone()
|
||
4 years ago
|
|
||
4 years ago
|
# Throw error if the guild already exists and then stop the function
|
||
|
if result is None:
|
||
|
await ctx.send("Looks like this guild does not have a **Modmail System** setup!" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
|
return
|
||
4 years ago
|
|
||
|
# As long as the channel exists within the guild
|
||
|
if channelID in channels:
|
||
|
|
||
|
try:
|
||
4 years ago
|
|
||
4 years ago
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Define the update statement that will insert information about the modmail channel
|
||
|
update_query = """UPDATE moderatormail SET modmailChannelID = (%s) WHERE guildID = (%s)"""
|
||
|
vals = channelID, ctx.author.guild.id
|
||
4 years ago
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(update_query, vals)
|
||
|
await conn.commit()
|
||
4 years ago
|
|
||
|
except mariadb.Error as err:
|
||
|
print(err)
|
||
4 years ago
|
await ctx.send("**Looks like something went wrong during the update!**")
|
||
4 years ago
|
|
||
4 years ago
|
# Send confirmation that the channel has been updated
|
||
4 years ago
|
channel = ctx.author.guild.get_channel(channelID)
|
||
|
await ctx.send(
|
||
|
f"**The channel has been updated! Your new modmail will be sent to** {channel.mention}")
|
||
|
|
||
|
else:
|
||
|
# Send error message if the channel ID is not recognised
|
||
|
await ctx.send("**Invalid Channel ID. Aborting Process...**")
|
||
|
return
|
||
|
|
||
4 years ago
|
@modmail.command()
|
||
4 years ago
|
@has_permissions(administrator=True)
|
||
4 years ago
|
@bot_has_permissions(administrator=True)
|
||
4 years ago
|
async def delete(self, ctx):
|
||
4 years ago
|
"""Delete the Entire Modmail System from the Guild"""
|
||
4 years ago
|
|
||
4 years ago
|
# Setup pool
|
||
4 years ago
|
pool = await connection(db.loop)
|
||
4 years ago
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the author's row from the Members Table
|
||
|
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
|
||
|
vals = ctx.author.guild.id,
|
||
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(select_query, vals)
|
||
|
result = await cur.fetchone()
|
||
4 years ago
|
|
||
|
# Throw error if the guild already exists and then stop the function
|
||
|
if result is None:
|
||
4 years ago
|
await ctx.send("Looks like this guild does not have a **Modmail System** setup!" +
|
||
|
f"\nPlease check **{ctx.prefix}help** for information on how to update/delete existing information")
|
||
4 years ago
|
return
|
||
|
|
||
|
try:
|
||
4 years ago
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Define the delete statement to remove all information about the guild
|
||
|
delete_query = """DELETE FROM moderatormail WHERE guildID = (%s)"""
|
||
|
vals = ctx.author.guild.id,
|
||
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(delete_query, vals)
|
||
|
await conn.commit()
|
||
4 years ago
|
|
||
|
except mariadb.Error as err:
|
||
|
print(err)
|
||
4 years ago
|
await ctx.send("**Looks like this guild has not set up the Modmail System yet!" +
|
||
4 years ago
|
f"\nPlease do `{ctx.prefix}help` to find out how to set it up!**")
|
||
|
|
||
|
# Sending confirmation message that the modmail system has been deleted
|
||
4 years ago
|
await ctx.send("**Modmail System** successfully deleted!" +
|
||
|
f"\nPlease do **{ctx.prefix}help** to find out how to set Modmail again!")
|
||
4 years ago
|
|
||
5 years ago
|
# Setting up Listener to listen for reactions within the modmail channel created
|
||
4 years ago
|
@Cog.listener()
|
||
5 years ago
|
async def on_raw_reaction_add(self, payload):
|
||
5 years ago
|
# Don't count reactions that are made by the bot
|
||
|
if payload.user_id == self.bot.user.id:
|
||
5 years ago
|
return
|
||
4 years ago
|
|
||
4 years ago
|
# Don't count other reactions other than ✅ and ❌
|
||
4 years ago
|
elif payload.user_id:
|
||
|
if str(payload.emoji) not in ['✅', '❌']:
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
# Find a role corresponding to the Emoji name.
|
||
|
guildid = payload.guild_id
|
||
|
|
||
4 years ago
|
# Setup pool
|
||
4 years ago
|
pool = await connection(db.loop)
|
||
4 years ago
|
|
||
|
# Setup up pool connection and cursor
|
||
|
async with pool.acquire() as conn:
|
||
|
async with conn.cursor() as cur:
|
||
|
# Get the author's row from the Members Table
|
||
|
select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)"""
|
||
|
val = guildid,
|
||
5 years ago
|
|
||
4 years ago
|
# Execute the SQL Query
|
||
4 years ago
|
await cur.execute(select_query, val)
|
||
|
result = await cur.fetchone()
|
||
|
|
||
|
# Adding error handling
|
||
|
if result is None:
|
||
|
return
|
||
|
|
||
|
# Define variables
|
||
|
guild_id = int(result[0])
|
||
|
channel_id = int(result[1])
|
||
|
message_id = int(result[2])
|
||
|
modmail_channel_id = int(result[3])
|
||
5 years ago
|
|
||
|
# Bunch of checks to make sure it has the right guild, channel, message and reaction
|
||
|
if payload.guild_id == guild_id and payload.channel_id == channel_id and payload.message_id == message_id and payload.emoji.name == "✅":
|
||
|
|
||
|
# Get the guild
|
||
|
guild = self.bot.get_guild(payload.guild_id)
|
||
|
# Get the member
|
||
|
member = guild.get_member(payload.user_id)
|
||
|
# Get the setup modmail channel
|
||
|
channel = guild.get_channel(payload.channel_id)
|
||
|
# Get the modmail logging channel
|
||
|
modmail_channel = guild.get_channel(modmail_channel_id)
|
||
|
|
||
|
# Fetch the message and remove the reaction
|
||
|
reaction = await channel.fetch_message(message_id)
|
||
|
await reaction.remove_reaction('✅', member)
|
||
|
|
||
|
# Setting up the channel permissions for the new channel that will be created
|
||
|
overwrites = {
|
||
4 years ago
|
guild.default_role: discord.PermissionOverwrite(read_messages=False, send_messages=False),
|
||
4 years ago
|
guild.me: discord.PermissionOverwrite(embed_links=True, read_messages=True, manage_messages=True,
|
||
4 years ago
|
manage_channels=True, read_message_history=True,
|
||
4 years ago
|
send_messages=True, attach_files=True),
|
||
5 years ago
|
member: discord.PermissionOverwrite(read_messages=True, send_messages=True)
|
||
|
}
|
||
|
|
||
|
# Saving this for later within when discord.py 1.4 comes out
|
||
4 years ago
|
# cat = await guild.create_category_channel(member.name, overwrites=overwrites, position=0)
|
||
5 years ago
|
|
||
|
# Create the text channel
|
||
4 years ago
|
user_channel = await guild.create_text_channel(member.name, overwrites=overwrites,
|
||
5 years ago
|
position=0)
|
||
|
|
||
|
# Mention the user to make sure that they get pinged
|
||
|
mention = await user_channel.send(member.mention)
|
||
|
await mention.delete()
|
||
|
|
||
|
try:
|
||
|
|
||
|
# Send the embed if they want to remain anonymous or not
|
||
|
Anon_or_Not = await user_channel.send(embed=AnonOrNot(member))
|
||
|
# Add reactions to the message
|
||
|
await Anon_or_Not.add_reaction('✅')
|
||
|
await Anon_or_Not.add_reaction('❌')
|
||
|
|
||
|
# Checking if the user reacted with ✅ with response to sending staff a message
|
||
|
def emoji_check(reaction, user):
|
||
|
return user == member and str(reaction.emoji) in ['✅', '❌']
|
||
|
|
||
|
# Surround with try/except to catch any exceptions that may occur
|
||
5 years ago
|
try:
|
||
5 years ago
|
# Wait for the user to add a reaction
|
||
|
reaction, user = await self.bot.wait_for('reaction_add', check=emoji_check)
|
||
|
except Exception as ex:
|
||
|
print(ex)
|
||
|
return
|
||
|
else:
|
||
|
if str(reaction.emoji) == "✅":
|
||
|
self.anon = True
|
||
5 years ago
|
|
||
5 years ago
|
# Delete the old embed
|
||
|
await Anon_or_Not.delete()
|
||
5 years ago
|
|
||
5 years ago
|
# Tell the user to type their mail into the chat
|
||
|
instructions = await user_channel.send(embed=SendInstructions(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Making sure that the reply is from the author
|
||
|
def check(m):
|
||
|
return m.author == payload.member and user_channel.id == instructions.channel.id
|
||
5 years ago
|
|
||
5 years ago
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check)
|
||
5 years ago
|
|
||
5 years ago
|
# Making sure that the message is below 50 characters and the message was sent in the channel
|
||
|
while len(msg.content) < 50 and msg.channel == user_channel:
|
||
|
await user_channel.send(embed=ErrorHandling(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check)
|
||
5 years ago
|
|
||
5 years ago
|
# As long as the message is above 50 characters and in the correct channel
|
||
|
if len(msg.content) > 50 and msg.channel == user_channel:
|
||
|
# Delete the previous embed
|
||
|
await instructions.delete()
|
||
|
|
||
|
# Determine a path for the message logs to be stored
|
||
4 years ago
|
path = "cogs/Anon.txt"
|
||
5 years ago
|
with open(path, 'a+') as f:
|
||
|
# Store the date and content of every message in the text file
|
||
|
async for message in user_channel.history(limit=300):
|
||
|
print(f"{message.created_at} : {message.content}", file=f)
|
||
|
|
||
|
# Send the message to the modmail channel
|
||
|
await modmail_channel.send(embed=SendMsgToModMail(self, msg, member),
|
||
|
file=File(fp=path))
|
||
|
|
||
|
# Removing file from the directory after it has been sent
|
||
|
if os.path.exists(path):
|
||
|
os.remove(path)
|
||
|
else:
|
||
|
print("The file does not exist")
|
||
5 years ago
|
|
||
5 years ago
|
# Make sure the user knows that their message has been sent
|
||
|
await user_channel.send(embed=MessageSentConfirmation(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Let the user read the message for 5 seconds
|
||
|
await asyncio.sleep(5)
|
||
5 years ago
|
|
||
5 years ago
|
# Delete the channel and then stop the function
|
||
|
await user_channel.delete()
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
# If the user types anywhere else, delete the channel
|
||
|
else:
|
||
|
await user_channel.delete()
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
if str(reaction.emoji) == "❌":
|
||
|
self.anon = False
|
||
5 years ago
|
|
||
5 years ago
|
# Delete the old embed
|
||
|
await Anon_or_Not.delete()
|
||
5 years ago
|
|
||
5 years ago
|
# Tell the user to type their mail into the chat
|
||
|
instructions = await user_channel.send(embed=SendInstructions(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Making sure that the reply is from the author
|
||
|
def check(m):
|
||
|
return m.author == payload.member and user_channel.id == instructions.channel.id
|
||
5 years ago
|
|
||
5 years ago
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check, timeout=300)
|
||
5 years ago
|
|
||
5 years ago
|
# Making sure that the message is below 50 characters and the message was sent in the channel
|
||
|
while len(msg.content) < 50 and msg.channel == user_channel:
|
||
|
await user_channel.send(embed=ErrorHandling(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Wait for the message from the author again
|
||
|
msg = await self.bot.wait_for('message', check=check, timeout=300)
|
||
5 years ago
|
|
||
5 years ago
|
if len(msg.content) > 50 and msg.channel == user_channel:
|
||
|
# Delete the previous embed
|
||
|
await instructions.delete()
|
||
5 years ago
|
|
||
5 years ago
|
# Determine a path for the message logs to be stored
|
||
4 years ago
|
path = "cogs/{}.txt".format(payload.member.name)
|
||
5 years ago
|
with open(path, 'a+') as f:
|
||
|
# Store the date and content of every message in the text file
|
||
|
async for message in user_channel.history(limit=300):
|
||
|
print(f"{message.created_at} : {message.content}", file=f)
|
||
5 years ago
|
|
||
5 years ago
|
# Send the message to the modmail channel
|
||
|
await modmail_channel.send(embed=SendMsgToModMail(self, msg, member),
|
||
|
file=File(fp=path))
|
||
5 years ago
|
|
||
5 years ago
|
# Removing file from the directory after it has been sent
|
||
|
if os.path.exists(path):
|
||
|
os.remove(path)
|
||
|
else:
|
||
|
print("The file does not exist")
|
||
5 years ago
|
|
||
5 years ago
|
# Make sure the user knows that their message has been sent
|
||
|
await user_channel.send(embed=MessageSentConfirmation(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Let the user read the message for 5 seconds
|
||
|
await asyncio.sleep(5)
|
||
5 years ago
|
|
||
5 years ago
|
# Delete the channel and then stop the function
|
||
|
await user_channel.delete()
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
# If the user types anywhere else, delete the channel
|
||
|
else:
|
||
|
await user_channel.delete()
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
except Exception as ex:
|
||
|
print(ex)
|
||
5 years ago
|
|
||
5 years ago
|
# Removing file from the directory after it has been sent
|
||
|
if os.path.exists(path):
|
||
|
os.remove(path)
|
||
|
else:
|
||
|
print("The file does not exist")
|
||
5 years ago
|
|
||
5 years ago
|
# Send out an error message if the user waited too long
|
||
|
await user_channel.send(
|
||
|
"Sorry! Something seems to have gone wrong and the modmail will be aborting."
|
||
|
"\nRemember to make sure it's under **1024** characters!!")
|
||
5 years ago
|
|
||
5 years ago
|
await asyncio.sleep(5)
|
||
|
await user_channel.delete()
|
||
5 years ago
|
|
||
5 years ago
|
|
||
|
def setup(bot):
|
||
4 years ago
|
bot.add_cog(Guild(bot))
|