简体   繁体   中英

Support command discord.py

I have created support command. But I want it to be more advanced. Everything works in my code but I want to add a function that my bot will dm me with a message content someone dmed bot. Well that's confusing I know but let me explain it with my code:

import discord
from discord.ext import commands
import DiscordUtils


intents = discord.Intents.default()

intents.members = True
client = commands.Bot(command_prefix = "=", intents = intents)


@client.command(pass_context=True)
async def support(ctx):
    author = ctx.author
    await ctx.message.add_reaction('<a:check12:810961073746345985>')
    embed = discord.Embed(
        title = 'What do you want to do?',
        description = '',
        color = 0
    )

    embed.set_footer(text='007 support')
    embed.add_field(name='Report a bug, react:', value="1️⃣", inline=False)
    embed.add_field(name='Submit suggestion, react:', value="2️⃣", inline=False)
    embed.add_field(name='Talk to bot owner, react:', value="3️⃣", inline=False)

    msg = await ctx.author.send(embed=embed)

    await msg.add_reaction("1️⃣")
    await msg.add_reaction("2️⃣")
    await msg.add_reaction("3️⃣")

    
    def check(reaction, user):
        return user == ctx.author and str(reaction.emoji) in ["1️⃣", "2️⃣", "3️⃣"]

    while True:
        try:
            reaction, user = await client.wait_for("reaction_add", timeout=60, check=check)

            if str(reaction.emoji) == "1️⃣":
                embed = discord.Embed(color=0)
                embed.set_author(name=f"007", icon_url="")
                embed.add_field(name='Report a bug', value="Describe a bug here. 007 will try to fix it.")
                embed.set_footer(text="007 support")
                await ctx.author.send(embed=embed)

                user = client.get_user(int(MY_ID))
                await user.send(embed=embed)

            if str(reaction.emoji) == "2️⃣":
                embed = discord.Embed(color=0)
                embed.set_author(name=f"007", icon_url="")
                embed.add_field(name='Submit suggestion', value="Submit your suggestion here. 007 owner will review it as fast as possible")
                embed.set_footer(text="007 support")
                await ctx.author.send(embed=embed)

                
                user = client.get_user(int(MY_ID))
                await user.send(embed=embed)


            if str(reaction.emoji) == "3️⃣":
                embed = discord.Embed(color=0)
                embed.set_author(name=f"007", icon_url="")
                embed.add_field(name='Talk to a owner', value='Your code is 240348. Type this in any channel: ```=talk <your code>``` then follow instructions')
                embed.set_footer(text="007 support")
                await ctx.author.send(embed=embed)
    
    
        except asyncio.TimeoutError:
            await msg.delete()

As you see in options(1, 2) bot send embed to ctx.author and to me. But I want bot to send embed message to me and author's answer with author name. For example:

if str(reaction.emoji) == "2️⃣":
                embed = discord.Embed(color=0)
                embed.set_author(name=f"007", icon_url="")
                embed.add_field(name='Submit suggestion', value="Submit your suggestion here. 007 owner will review it as fast as possible")
                embed.set_footer(text="007 support")
                await ctx.author.send(embed=embed)

                
                user = client.get_user(int(MY_ID))
                await user.send(embed=embed)
                await user.send(f'{answer} {author.name}

In last line there is author's answer/message. Is this possible to do this? If yes let me know how. Thanks

To make it so that your bot forwards the message it got to you, you simply need to put code for the on_message event . This bit of code will make that every time the bot receives a direct message, it will send to the user with id MY_ID the content of the received message, followed by the name of the author of the message

@client.event
async def on_message(self, message: Message):
    if not isinstance(message.channel, DMChannel):  # This is to check if the message was received via DM
        return
    user = client.get_user(int(MY_ID))
    await user.send(content=f"{message.content} received from {message.author.name}")

If you want to also forward files and images this might require more checks such as checking if Message has attachements, etc.

You can find more documentation about the Message class on https://discordpy.readthedocs.io/en/latest/api.html?highlight=message#discord.Message

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM