From 9a1064c81d4e5f182c744645470f640de172d2e7 Mon Sep 17 00:00:00 2001 From: Twentysix Date: Thu, 10 Aug 2017 03:48:50 +0200 Subject: [PATCH] [Core] Added [p]contact and [p]dm (#875) * [Core] Added [p]contact and a way to DM users through Red * Turned reply feature into [p]dm command * Better docstring --- core/core_commands.py | 86 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/core/core_commands.py b/core/core_commands.py index 3c99fab1f..f549e2728 100644 --- a/core/core_commands.py +++ b/core/core_commands.py @@ -2,6 +2,7 @@ from discord.ext import commands from core import checks from string import ascii_letters, digits from random import SystemRandom +from collections import namedtuple import logging import importlib import os @@ -281,3 +282,88 @@ class Core: await ctx.send("You have been set as owner.") else: await ctx.send("Invalid token.") + + @commands.command() + @commands.cooldown(1, 60, commands.BucketType.user) + async def contact(self, ctx, *, message: str): + """Sends a message to the owner""" + guild = ctx.message.guild + owner = discord.utils.get(ctx.bot.get_all_members(), + id=ctx.bot.owner_id) + author = ctx.message.author + footer = "User ID: %s" % author.id + + if ctx.guild is None: + source = "through DM" + else: + source = "from {}".format(guild) + footer += " | Server ID: %s" % guild.id + + # We need to grab the DM command prefix (global) + # Since it can also be set through cli flags, bot.db is not a reliable + # source. So we'll just mock a DM message instead. + fake_message = namedtuple('Message', 'guild') + prefix = ctx.bot.command_prefix(ctx.bot, fake_message(guild=None))[0] + + content = ("Use `{}dm {} ` to reply to this user" + "".format(prefix, author.id)) + + if isinstance(author, discord.Member): + colour = author.colour + else: + colour = discord.Colour.red() + + description = "Sent by {} {}".format(author, source) + + e = discord.Embed(colour=colour, description=message) + if author.avatar_url: + e.set_author(name=description, icon_url=author.avatar_url) + else: + e.set_author(name=description) + e.set_footer(text=footer) + + try: + await owner.send(content, embed=e) + except discord.InvalidArgument: + await ctx.send("I cannot send your message, I'm unable to find " + "my owner... *sigh*") + except: + await ctx.send("I'm unable to deliver your message. Sorry.") + else: + await ctx.send("Your message has been sent.") + + @commands.command() + @checks.is_owner() + async def dm(self, ctx, user_id: int, *, message: str): + """Sends a DM to a user + + This command needs a user id to work. + To get a user id enable 'developer mode' in Discord's + settings, 'appearance' tab. Then right click a user + and copy their id""" + destination = discord.utils.get(ctx.bot.get_all_members(), + id=user_id) + if destination is None: + await ctx.send("Invalid ID or user not found. You can only " + "send messages to people I share a server " + "with.") + return + + e = discord.Embed(colour=discord.Colour.red(), description=message) + description = "Owner of %s" % ctx.bot.user + fake_message = namedtuple('Message', 'guild') + prefix = ctx.bot.command_prefix(ctx.bot, fake_message(guild=None))[0] + e.set_footer(text=("You can reply to this message with %scontact" + "" % prefix)) + if ctx.bot.user.avatar_url: + e.set_author(name=description, icon_url=ctx.bot.user.avatar_url) + else: + e.set_author(name=description) + + try: + await destination.send(embed=e) + except: + await ctx.send("Sorry, I couldn't deliver your message " + "to %s" % destination) + else: + await ctx.send("Message delivered to %s" % destination)