3

I've come up with a way to DM people, but I want to know what they say back to the bot through DMs, as if the bot "reads" the DM, and then forwards it to some channel in a discord server of mine, or, even better, DM it to me.

Here is my starting code:

if message.content.startswith("!dm"):
    if message.author.id == "[YOUR ID HERE]":
        memberID = "ID OF RECIPIENT"
        server = message.server
        person = discord.Server.get_member(server, memberID)
        await client.delete_message(message)
        await client.send_message(destination = person, content = "WHAT I'D LIKE TO SAY TO THEM")

I do it a different way contrary to how people do it with defining functions, I use a more basic way of making commands.

Any help is appreciated!

Value_Error
  • 53
  • 1
  • 1
  • 6
  • Private `Message` objects will have a `message.server` attribute of `None`. I'm a little confused about your requirement. Do you want a command that sends a private message, or to "forward" private messages to some other channel? DO you have the id of that channel? – Patrick Haugh Jun 19 '18 at 22:29
  • The command that I posted is code for sending a message that is predetermined to the string that I wrote in the `await client.send_message(destination = person, content = "Content of DM")` line of code, which sends the DM through the bot account whenever I write "`!dm`". – Value_Error Jun 19 '18 at 23:35
  • I want to "forward" private messages that I receive FROM THE BOT ACCOUNT (this ain't a selfbot) to some text channel in a server or mine, or DM'd to me, whichever you prefer. – Value_Error Jun 19 '18 at 23:37
  • The channel that I may want to have the log forwarded to is 458778457539870742, My discord ID (If you want to go the DM route) is 267043073635254273. – Value_Error Jun 19 '18 at 23:44

1 Answers1

4

Here's a quick example. I've moved your existing command into an actual Command object, so the forwarding logic is the only thing in on_message

from discord.ext import commands

bot = commands.bot('!')

# I've moved the command out of on_message so it doesn't get cluttered
@bot.event
async def on_message(message):
    channel = bot.get_channel('458778457539870742')
    if message.server is None and message.author != bot.user:
        await bot.send_message(channel, message.content)
    await bot.process_commands(message)

# This always sends the same message to the same person.  Is that what you want?
@bot.command(pass_context=True)
@commands.is_owner()  # The account that owns the bot
async def dm(ctx):
    memberID = "ID OF RECIPIENT"
    person = await bot.get_user_info(memberID)
    await bot.send_message(person, "WHAT I'D LIKE TO SAY TO THEM")
    await bot.delete_message(ctx.message)
Patrick Haugh
  • 59,226
  • 13
  • 88
  • 96
  • Yes, I just change the ID I want to send the DM to. Thank you for answering. – Value_Error Jun 20 '18 at 00:02
  • Maybe it might be the lack of the rewrite version of discord.py, but I get a type error: `await client.process_commands` can't be used in an await expression. Any thoughts? – Value_Error Jun 20 '18 at 00:10
  • My mistake. You have to pass it `message`. – Patrick Haugh Jun 20 '18 at 00:11
  • `message.author` will be the `User` object of the person who wrote the message. – Patrick Haugh Jun 20 '18 at 00:46
  • Also, `InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received generator`. Maybe one of the parameters are listed wrong? Line of code where error was recieved: await client.send_message(person, "WHAT I'D LIKE TO SAY TO THEM") – Value_Error Jun 20 '18 at 00:48
  • Looks like you have to `await bot.get_user_info(memberID)`. I'll make that correction – Patrick Haugh Jun 20 '18 at 00:52
  • I apologize, I do not have much information on defining functions as commands, but how do you delete the command written, and restrict the command to be used only by my account (by my ID)? I typically use `await client.delete_message(message)` and `if message.author.id == "267043073635254273":` in order to delete the command written and restrict the command only to me respectively, but since this is not using the attribute message, it is not defined, therefore my bot not being able to actually run the commands properly without a message. – Value_Error Jun 20 '18 at 01:08
  • You can use the `commands.is_owner` deocrator to determine if the person invoking the command is the owner of the bot. You can use `ctx.message` to resolve the message object so you can delete it. – Patrick Haugh Jun 20 '18 at 01:12
  • How do you establish the owner of the bot? – Value_Error Jun 20 '18 at 01:14
  • To register a bot with discord and get a token, you had to go to their web site and log in. Whatever account was logged in when you generated the bot owns it. If you're not the owner, you can use this solution instead: https://stackoverflow.com/questions/50667963/admins-only-command/50668067#50668067 – Patrick Haugh Jun 20 '18 at 01:16
  • Alright, thank you. This is my owned bot, so no problem. Thank you for putting up with my.. Let's say, lack of knowledge of the subject. Hopefully I see you again in the python 3.X stack overflow. – Value_Error Jun 20 '18 at 01:19
  • Uh oh, one more problem, the attribute "is_owner" doesn't exist in discord.ext.commands? – Value_Error Jun 20 '18 at 01:34
  • Looks like that might only be in the experimental branch. You can see the implementation of a simialar decorator here in the source: https://github.com/Rapptz/discord.py/blob/async/discord/ext/commands/core.py#L743 – Patrick Haugh Jun 20 '18 at 02:39