Getting better check

Removing indentation level
pull/8/head
sgoudham 4 years ago
parent c006b17330
commit 1ef87c257b

@ -142,7 +142,7 @@ def SendMsgToModMail(self, msg, author):
# Set up the Cog # Set up the Cog
class Modmail(commands.Cog): class modmail(commands.Cog):
def __init__(self, bot): def __init__(self, bot):
self.bot = bot self.bot = bot
self.anon = None self.anon = None
@ -150,227 +150,220 @@ class Modmail(commands.Cog):
# Setting up Listener to listen for reactions within the modmail channel created # Setting up Listener to listen for reactions within the modmail channel created
@commands.Cog.listener() @commands.Cog.listener()
async def on_raw_reaction_add(self, payload): async def on_raw_reaction_add(self, payload):
self.anon = None # Don't count reactions that are made by the bot
m = payload.member if payload.user_id == self.bot.user.id:
# A check that makes sure that the reaction is done by the bot
def check(m):
return m == self.bot.user
# If the member is not a user, do nothing
if check(m):
return return
else:
# Find a role corresponding to the Emoji name.
guildid = payload.guild_id
# Use database connection
with db.connection() as conn:
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
val = guildid,
cursor = conn.cursor()
# Execute the SQL Query
cursor.execute(select_query, val)
result = cursor.fetchone()
guild_id = int(result[0])
channel_id = int(result[1])
message_id = int(result[2])
modmail_channel_id = int(result[3])
# 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 = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
guild.me: discord.PermissionOverwrite(read_messages=True),
member: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
# Saving this for later within when discord.py 1.4 comes out
# cat = await guild.create_category_channel(member.name, overwrites=overwrites)
# Create the text channel
user_channel = await guild.create_text_channel(member.name, overwrites=overwrites,
position=0)
# Mention the user to make sure that they get pinged
mention = await user_channel.send(member.mention)
await mention.delete()
# Find a role corresponding to the Emoji name.
guildid = payload.guild_id
# Use database connection
with db.connection() as conn:
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
val = guildid,
cursor = conn.cursor()
# Execute the SQL Query
cursor.execute(select_query, val)
result = cursor.fetchone()
guild_id = int(result[0])
channel_id = int(result[1])
message_id = int(result[2])
modmail_channel_id = int(result[3])
# 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 = {
guild.default_role: discord.PermissionOverwrite(read_messages=False),
guild.me: discord.PermissionOverwrite(read_messages=True),
member: discord.PermissionOverwrite(read_messages=True, send_messages=True)
}
# Saving this for later within when discord.py 1.4 comes out
# cat = await guild.create_category_channel(member.name, overwrites=overwrites)
# Create the text channel
user_channel = await guild.create_text_channel(member.name, overwrites=overwrites,
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
try: try:
# 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
# Send the embed if they want to remain anonymous or not # Delete the old embed
Anon_or_Not = await user_channel.send(embed=AnonOrNot(member)) await Anon_or_Not.delete()
# 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
try:
# 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
# Delete the old embed
await Anon_or_Not.delete()
# Tell the user to type their mail into the chat
instructions = await user_channel.send(embed=SendInstructions(member))
# Making sure that the reply is from the author
def check(m):
return m.author == payload.member and user_channel.id == instructions.channel.id
# Wait for the message from the author
msg = await self.bot.wait_for('message', check=check)
# Making sure that the message is below 50 characters and the message was sent in the channel # Tell the user to type their mail into the chat
while len(msg.content) < 50 and msg.channel == user_channel: instructions = await user_channel.send(embed=SendInstructions(member))
await user_channel.send(embed=ErrorHandling(member))
# Wait for the message from the author # Making sure that the reply is from the author
msg = await self.bot.wait_for('message', check=check) def check(m):
return m.author == payload.member and user_channel.id == instructions.channel.id
# As long as the message is above 50 characters and in the correct channel # Wait for the message from the author
if len(msg.content) > 50 and msg.channel == user_channel: msg = await self.bot.wait_for('message', check=check)
# Delete the previous embed
await instructions.delete()
# Determine a path for the message logs to be stored # Making sure that the message is below 50 characters and the message was sent in the channel
path = "cogs/modmail/Anon.txt" while len(msg.content) < 50 and msg.channel == user_channel:
with open(path, 'a+') as f: await user_channel.send(embed=ErrorHandling(member))
# 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 # Wait for the message from the author
await modmail_channel.send(embed=SendMsgToModMail(self, msg, member), msg = await self.bot.wait_for('message', check=check)
file=File(fp=path))
# Removing file from the directory after it has been sent # As long as the message is above 50 characters and in the correct channel
if os.path.exists(path): if len(msg.content) > 50 and msg.channel == user_channel:
os.remove(path) # Delete the previous embed
else: await instructions.delete()
print("The file does not exist")
# Determine a path for the message logs to be stored
path = "cogs/modmail/Anon.txt"
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")
# Make sure the user knows that their message has been sent # Make sure the user knows that their message has been sent
await user_channel.send(embed=MessageSentConfirmation(member)) await user_channel.send(embed=MessageSentConfirmation(member))
# Let the user read the message for 5 seconds # Let the user read the message for 5 seconds
await asyncio.sleep(5) await asyncio.sleep(5)
# Delete the channel and then stop the function # Delete the channel and then stop the function
await user_channel.delete() await user_channel.delete()
return return
# If the user types anywhere else, delete the channel # If the user types anywhere else, delete the channel
else: else:
await user_channel.delete() await user_channel.delete()
return return
if str(reaction.emoji) == "": if str(reaction.emoji) == "":
self.anon = False self.anon = False
# Delete the old embed # Delete the old embed
await Anon_or_Not.delete() await Anon_or_Not.delete()
# Tell the user to type their mail into the chat # Tell the user to type their mail into the chat
instructions = await user_channel.send(embed=SendInstructions(member)) instructions = await user_channel.send(embed=SendInstructions(member))
# Making sure that the reply is from the author # Making sure that the reply is from the author
def check(m): def check(m):
return m.author == payload.member and user_channel.id == instructions.channel.id return m.author == payload.member and user_channel.id == instructions.channel.id
# Wait for the message from the author # Wait for the message from the author
msg = await self.bot.wait_for('message', check=check, timeout=300) msg = await self.bot.wait_for('message', check=check, timeout=300)
# Making sure that the message is below 50 characters and the message was sent in the channel # 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: while len(msg.content) < 50 and msg.channel == user_channel:
await user_channel.send(embed=ErrorHandling(member)) await user_channel.send(embed=ErrorHandling(member))
# Wait for the message from the author again # Wait for the message from the author again
msg = await self.bot.wait_for('message', check=check, timeout=300) msg = await self.bot.wait_for('message', check=check, timeout=300)
if len(msg.content) > 50 and msg.channel == user_channel: if len(msg.content) > 50 and msg.channel == user_channel:
# Delete the previous embed # Delete the previous embed
await instructions.delete() await instructions.delete()
# Determine a path for the message logs to be stored # Determine a path for the message logs to be stored
path = "cogs/modmail/{}.txt".format(payload.member.name) path = "cogs/modmail/{}.txt".format(payload.member.name)
with open(path, 'a+') as f: with open(path, 'a+') as f:
# Store the date and content of every message in the text file # Store the date and content of every message in the text file
async for message in user_channel.history(limit=300): async for message in user_channel.history(limit=300):
print(f"{message.created_at} : {message.content}", file=f) print(f"{message.created_at} : {message.content}", file=f)
# Send the message to the modmail channel # Send the message to the modmail channel
await modmail_channel.send(embed=SendMsgToModMail(self, msg, member), await modmail_channel.send(embed=SendMsgToModMail(self, msg, member),
file=File(fp=path)) file=File(fp=path))
# Removing file from the directory after it has been sent # Removing file from the directory after it has been sent
if os.path.exists(path): if os.path.exists(path):
os.remove(path) os.remove(path)
else: else:
print("The file does not exist") print("The file does not exist")
# Make sure the user knows that their message has been sent # Make sure the user knows that their message has been sent
await user_channel.send(embed=MessageSentConfirmation(member)) await user_channel.send(embed=MessageSentConfirmation(member))
# Let the user read the message for 5 seconds # Let the user read the message for 5 seconds
await asyncio.sleep(5) await asyncio.sleep(5)
# Delete the channel and then stop the function # Delete the channel and then stop the function
await user_channel.delete() await user_channel.delete()
return return
# If the user types anywhere else, delete the channel # If the user types anywhere else, delete the channel
else: else:
await user_channel.delete() await user_channel.delete()
return return
except Exception as ex: except Exception as ex:
print(ex) print(ex)
# Removing file from the directory after it has been sent # Removing file from the directory after it has been sent
if os.path.exists(path): if os.path.exists(path):
os.remove(path) os.remove(path)
else: else:
print("The file does not exist") print("The file does not exist")
# Send out an error message if the user waited too long # Send out an error message if the user waited too long
await user_channel.send( await user_channel.send(
"Sorry! Something seems to have gone wrong and the modmail will be aborting." "Sorry! Something seems to have gone wrong and the modmail will be aborting."
"\nRemember to make sure it's under **1024** characters!!") "\nRemember to make sure it's under **1024** characters!!")
await asyncio.sleep(5) await asyncio.sleep(5)
await user_channel.delete() await user_channel.delete()
def setup(bot): def setup(bot):
bot.add_cog(Modmail(bot)) bot.add_cog(modmail(bot))

Loading…
Cancel
Save