【问题标题】:Support command discord.py支持命令 discord.py
【发布时间】:2021-05-27 21:19:58
【问题描述】:

我已经创建了支持命令。但我希望它更先进。一切都在我的代码中运行,但我想添加一个功能,我的机器人将使用某人 dmed bot 的消息内容向我发送消息。好吧,我知道这很令人困惑,但让我用我的代码解释一下:

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()

正如您在 options(1, 2) 中看到的,机器人将嵌入发送到 ctx.author 和我。但我希望机器人向我发送嵌入消息,并用作者姓名回复作者的答案。例如:

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}

最后一行是作者的回答/信息。这有可能做到这一点吗?如果是,请告诉我如何。谢谢

【问题讨论】:

  • 我不确定我理解你想要什么。您是否希望当有人向您的机器人发送 DM 时,该机器人会向您发送一条 DM,其中包含向您的机器人发送 DM 的用户的名称以及发送的消息?
  • 是的,我就是这个意思。

标签: python python-3.x discord.py


【解决方案1】:

要使您的机器人将收到的消息转发给您,您只需输入on_messageevent 的代码。这段代码将使bot每次收到直接消息时,都会向ID为MY_ID的用户发送收到的消息内容,后跟消息作者的姓名

@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}")

如果您还想转发文件和图像,这可能需要更多检查,例如检查 Message 是否有附件等。

您可以在 https://discordpy.readthedocs.io/en/latest/api.html?highlight=message#discord.Message 上找到有关 Message 类的更多文档

【讨论】:

    猜你喜欢
    • 2021-03-26
    • 2021-08-28
    • 2021-01-22
    • 2021-03-30
    • 2021-05-17
    • 2021-01-15
    • 2022-12-04
    • 2023-02-25
    • 1970-01-01
    相关资源
    最近更新 更多