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.
384 lines
16 KiB
Python
384 lines
16 KiB
Python
5 years ago
|
import asyncio
|
||
5 years ago
|
import datetime
|
||
5 years ago
|
import random
|
||
|
|
||
5 years ago
|
import mariadb
|
||
5 years ago
|
from discord import DMChannel, Embed
|
||
5 years ago
|
from discord.ext import commands
|
||
|
|
||
5 years ago
|
import db
|
||
5 years ago
|
from settings import blank_space, enso_embedmod_colours, enso_guild_ID, enso_modmail_ID, hammyMention, \
|
||
5 years ago
|
ensoMention, hammyID
|
||
5 years ago
|
|
||
5 years ago
|
|
||
5 years ago
|
# Method to send the prompt/embed to start sending modmail to the user
|
||
|
def startModMail(author):
|
||
|
# Set up embed to let the user how to start sending modmail
|
||
|
startModMailEmbed = Embed(title="**Welcome to Modmail!**",
|
||
5 years ago
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
startModMailEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
startModMailEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
5 years ago
|
fields = [
|
||
|
(blank_space, "**React to this message if you want to send a message to the Staff Team!**", False),
|
||
|
(blank_space, "**Use :white_check_mark: for** `Yes`", True),
|
||
|
(blank_space, "**Use :x: for** `No`", True),
|
||
|
(blank_space, blank_space, True),
|
||
|
(blank_space,
|
||
|
"We encourage all suggestions/thoughts and opinions on the server! As long as it is **valid** criticism. "
|
||
|
"Purely negative feedback will not be considered.", True)]
|
||
5 years ago
|
|
||
|
for name, value, inline in fields:
|
||
|
startModMailEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return startModMailEmbed
|
||
|
|
||
|
|
||
|
# Method to ask the user if they want to be anonymous or not
|
||
|
def AnonOrNot(author):
|
||
|
# Set up embed to let the user how to start sending modmail
|
||
|
AnonModMailEmbed = Embed(title="**Want to send it Anonymously?**",
|
||
5 years ago
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
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."
|
||
|
"\nFeel free to use the reactions below to choose!**", False),
|
||
|
(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
|
"This will make sure that Staff do not know who is sending the mail."
|
||
|
"\nAgain, purely negative feedback will not be considered.", True)]
|
||
|
|
||
|
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!**",
|
||
5 years ago
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
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 characters!**",
|
||
|
"**Include as much detail as possible :P**",
|
||
5 years ago
|
False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
SendModMailEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return SendModMailEmbed
|
||
|
|
||
|
|
||
5 years ago
|
# 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
|
||
|
ErrorHandlingEmbed = Embed(title="**Uh Oh! Please make sure the message is above 50 characters!**",
|
||
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
ErrorHandlingEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
ErrorHandlingEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
|
fields = [("Please enter in a message which is above 50 characters!",
|
||
|
"**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
|
||
|
|
||
|
|
||
5 years ago
|
# 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,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
ConfirmationEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
ConfirmationEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
5 years ago
|
fields = [("Thank you for your input! The staff team appreciate it very much!",
|
||
5 years ago
|
f"\n As mentioned previously, please don't be hesistant to DM {hammyMention} for anything! :P",
|
||
|
False)]
|
||
|
|
||
|
for name, value, inline in fields:
|
||
|
ConfirmationEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return ConfirmationEmbed
|
||
|
|
||
|
|
||
5 years ago
|
# 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",
|
||
5 years ago
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
embed.set_thumbnail(url=random.choice(avatars))
|
||
5 years ago
|
embed.set_footer(text=f"Sent By Anon Member")
|
||
5 years ago
|
|
||
|
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",
|
||
5 years ago
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
embed.set_thumbnail(url=author.avatar_url)
|
||
5 years ago
|
embed.set_footer(text=f"Sent By {author}")
|
||
5 years ago
|
|
||
|
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
|
||
|
|
||
|
|
||
5 years ago
|
# Method to allow modmail to be logged into the database
|
||
5 years ago
|
def logModMail(ctx, anon, msg):
|
||
5 years ago
|
# With the database connection
|
||
|
with db.connection() as conn:
|
||
5 years ago
|
# Make sure that mariaDB errors are handled properly
|
||
|
try:
|
||
|
if anon:
|
||
|
Anon = "True"
|
||
|
else:
|
||
|
Anon = "False"
|
||
|
|
||
|
msg_name = ctx.message.author.name
|
||
|
msg_discrim = ctx.message.author.discriminator
|
||
|
time = ctx.message.created_at
|
||
|
|
||
|
# Get:
|
||
|
msg_time = time.strftime('%Y-%m-%d %H:%M:%S') # Time of the Message
|
||
|
msg_author = f"{msg_name}#{msg_discrim}" # DiscordID
|
||
5 years ago
|
msg_content = msg.content # Content of the message
|
||
5 years ago
|
|
||
|
# Store the variables
|
||
|
val = Anon, msg_time, msg_author, msg_content
|
||
|
|
||
|
# Define the Insert Into Statement inserting into the database
|
||
|
insert_query = """INSERT INTO modmail (Anon, messageTime, discordID, messageContent) VALUES (?, ?, ?, ?)"""
|
||
|
cursor = conn.cursor()
|
||
|
# Execute the SQL Query
|
||
|
cursor.execute(insert_query, val)
|
||
|
conn.commit()
|
||
|
print(cursor.rowcount, "Record inserted successfully into Modmail")
|
||
|
|
||
|
except mariadb.Error as ex:
|
||
|
print("Parameterized Query Failed: {}".format(ex))
|
||
|
|
||
|
|
||
|
# Method to send an embed to let the user know that they have aborted the modmail process
|
||
|
def Abort(author):
|
||
|
# Set up embed to let the user know that they have aborted the modmail
|
||
|
AbortEmbed = Embed(title="**Aborting ModMail!**",
|
||
|
colour=enso_embedmod_colours,
|
||
5 years ago
|
timestamp=datetime.datetime.utcnow())
|
||
5 years ago
|
|
||
|
AbortEmbed.set_thumbnail(url=author.avatar_url)
|
||
|
AbortEmbed.set_footer(text=f"Sent by {author}")
|
||
|
|
||
|
fields = [
|
||
5 years ago
|
("**If you change your mind, you can do `~mm` or `~modmail` at anytime!**",
|
||
|
f"If you want to speak to me personally, you can DM {hammyMention} anytime!", False)]
|
||
5 years ago
|
|
||
|
for name, value, inline in fields:
|
||
|
AbortEmbed.add_field(name=name, value=value, inline=inline)
|
||
|
|
||
|
return AbortEmbed
|
||
|
|
||
|
|
||
5 years ago
|
# Set up the Cog
|
||
5 years ago
|
class Modmail(commands.Cog):
|
||
|
def __init__(self, bot):
|
||
|
self.bot = bot
|
||
5 years ago
|
self.reaction = None
|
||
|
self.anon = None
|
||
5 years ago
|
|
||
|
# Allows for the modmail system
|
||
5 years ago
|
@commands.command(name="modmail", aliases=["mm"])
|
||
|
async def mod_mail(self, ctx):
|
||
5 years ago
|
self.anon = None
|
||
5 years ago
|
|
||
5 years ago
|
# Get the mod-mail channel
|
||
5 years ago
|
channel = self.bot.get_channel(enso_modmail_ID)
|
||
5 years ago
|
# Get the guild Enso
|
||
5 years ago
|
guild = self.bot.get_guild(enso_guild_ID)
|
||
5 years ago
|
# Get Hamothy
|
||
|
member = guild.get_member(hammyID)
|
||
5 years ago
|
|
||
5 years ago
|
# Making sure the user is in a DM channel with the bot
|
||
|
if isinstance(ctx.message.channel, DMChannel):
|
||
5 years ago
|
|
||
5 years ago
|
# Asking if the user wants to send staff mail
|
||
|
modmail = await ctx.send(embed=startModMail(member))
|
||
|
# Add reactions to the message
|
||
|
await modmail.add_reaction('✅')
|
||
|
await modmail.add_reaction('❌')
|
||
|
|
||
|
# Surround with try/except to catch any exceptions that may occur
|
||
|
try:
|
||
5 years ago
|
|
||
5 years ago
|
# Checking if the user reacted with ✅ with response to sending staff a message
|
||
|
def emoji_check(reaction, user):
|
||
|
return user == ctx.author 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
|
||
5 years ago
|
reaction, user = await self.bot.wait_for('reaction_add', timeout=120.0, check=emoji_check)
|
||
5 years ago
|
except Exception as ex:
|
||
|
print(ex)
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
else:
|
||
5 years ago
|
|
||
5 years ago
|
if str(reaction.emoji) == "✅":
|
||
|
|
||
|
# Delete the old embed
|
||
|
await modmail.delete()
|
||
|
|
||
|
# Ask the user if they want the mail to be anonymized
|
||
|
anonornot = await ctx.send(embed=AnonOrNot(member))
|
||
|
# Add reactions to the message
|
||
|
await anonornot.add_reaction('✅')
|
||
|
await anonornot.add_reaction('❌')
|
||
|
|
||
|
# Surround with try/except to catch any exceptions that may occur
|
||
|
try:
|
||
5 years ago
|
|
||
5 years ago
|
# Wait for the user to add a reaction
|
||
5 years ago
|
reaction, user = await self.bot.wait_for('reaction_add', timeout=120.0, check=emoji_check)
|
||
5 years ago
|
except Exception as ex:
|
||
|
print(ex)
|
||
|
return
|
||
5 years ago
|
|
||
5 years ago
|
else:
|
||
|
if str(reaction.emoji) == "✅":
|
||
|
self.anon = True
|
||
|
|
||
|
# Delete the old embed
|
||
|
await anonornot.delete()
|
||
|
|
||
|
# Tell the user to type their mail into the chat
|
||
|
instructions = await ctx.send(embed=SendInstructions(member))
|
||
|
|
||
|
# Making sure that the reply is from the author
|
||
|
def check(m):
|
||
|
return m.author == ctx.author
|
||
|
|
||
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check, timeout=300)
|
||
|
|
||
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 isinstance(msg.channel, DMChannel):
|
||
5 years ago
|
await ctx.send(embed=ErrorHandling(member))
|
||
5 years ago
|
|
||
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check, timeout=300)
|
||
|
|
||
5 years ago
|
# Delete the previous embed
|
||
5 years ago
|
await instructions.delete()
|
||
5 years ago
|
# Send the message to the modmail channel
|
||
5 years ago
|
await channel.send(embed=SendMsgToModMail(self, msg, ctx.author))
|
||
5 years ago
|
|
||
|
# Make sure the user knows that their message has been sent
|
||
5 years ago
|
await ctx.send(embed=MessageSentConfirmation(member))
|
||
5 years ago
|
|
||
|
# Log the message within the database
|
||
5 years ago
|
logModMail(ctx, self.anon, msg)
|
||
5 years ago
|
|
||
|
if str(reaction.emoji) == "❌":
|
||
|
self.anon = False
|
||
|
|
||
|
# Delete the old embed
|
||
|
await anonornot.delete()
|
||
|
|
||
|
# Tell the user to type their mail into the chat
|
||
|
instructions = await ctx.send(embed=SendInstructions(member))
|
||
|
|
||
|
# Making sure that the reply is from the author
|
||
|
def check(m):
|
||
|
return m.author == ctx.author
|
||
|
|
||
|
# Wait for the message from the author
|
||
|
msg = await self.bot.wait_for('message', check=check, timeout=300)
|
||
|
|
||
5 years ago
|
# Making sure that the message is below 50 characters and the message was sent in the channel
|
||
5 years ago
|
while len(msg.content) < 50 and isinstance(msg.channel, DMChannel):
|
||
5 years ago
|
await ctx.send(embed=ErrorHandling(member))
|
||
5 years ago
|
|
||
5 years ago
|
# Wait for the message from the author again
|
||
5 years ago
|
msg = await self.bot.wait_for('message', check=check, timeout=300)
|
||
|
|
||
5 years ago
|
# Delete the previous embed
|
||
5 years ago
|
await instructions.delete()
|
||
5 years ago
|
# Send the message to the modmail channel
|
||
5 years ago
|
await channel.send(embed=SendMsgToModMail(self, msg, ctx.author))
|
||
5 years ago
|
|
||
|
# Make sure the user knows that their message has been sent
|
||
5 years ago
|
await ctx.send(embed=MessageSentConfirmation(member))
|
||
5 years ago
|
|
||
|
# Log the message within the database
|
||
5 years ago
|
logModMail(ctx, self.anon, msg)
|
||
5 years ago
|
|
||
|
if self.anon is None:
|
||
|
if str(reaction.emoji) == "❌":
|
||
|
# Delete the old embed
|
||
|
await modmail.delete()
|
||
|
|
||
|
# Send the Abort embed to the user
|
||
|
await ctx.send(embed=Abort(member))
|
||
5 years ago
|
return
|
||
5 years ago
|
|
||
|
except Exception as ex:
|
||
|
print(ex)
|
||
5 years ago
|
|
||
5 years ago
|
# Send out an error message if the user waited too long
|
||
|
await ctx.send("ModMail Timed Out! Do `~mm` or `~modmail` if you want to use the ModMail system!")
|
||
5 years ago
|
|
||
5 years ago
|
else:
|
||
|
message = await ctx.send(
|
||
|
f"{ctx.author.mention} **ModMail can only be sent through DM's!** "
|
||
|
f"\nSuggestions and Opinions on the server are always appreciated!\n"
|
||
5 years ago
|
f"Make sure you DM {ensoMention} and then use `~modmail` or `~mm`")
|
||
5 years ago
|
|
||
5 years ago
|
# Let the User read the message for 10 seconds
|
||
|
await asyncio.sleep(10.0)
|
||
|
# Delete the message
|
||
|
await message.delete()
|
||
5 years ago
|
|
||
|
|
||
|
def setup(bot):
|
||
|
bot.add_cog(Modmail(bot))
|