在 discord.py-rewrite 中将 Dm 发送到用户命令

Sending Dm to a user command in discord.py-rewrite

所以,我试图制作一个 DM 命令,询问我们要将 DM 消息(值)发送给哪个用户,例如 - !vx dm THIS IS THE CONTENT OF THE MESSAGE 将向消息的作者发送消息询问- ““你想给谁发私信?” 作者会通过提及他想将私信发送给谁来回复,在这样做之后,它会将私信发送给用户,然后发送一条消息说“我已将消息发送给 X”。这就是我想要的命令。

@client.command()
async def dm(ctx, value):
    member = discord.Member if not discord.Member else discord.Member
    await ctx.send(f"{ctx.author.mention}, Whom do you want to send the message to?")
    def check(m):
        return m.content == member.mention == member
    await ctx.member.send(f"**{value}**")
    await ctx.member.send(f"||Sent by {ctx.author.mention} via VX Helper.||")
    e = discord.Embed(title=f"Message sent to {member.display_name}.", description=f"Message Content - {value}.", colour=0x40cc88)
    e.set_footer(text=f"Sent by {ctx.author.display_name}", icon_url=ctx.author.avatar_url)
    await ctx.channel.purge(limit=3)
    await ctx.send(embed=e)

此代码在 heroku 中打印出此错误 -

2020-06-20T12:03:12.632899+00:00 app[worker.1]: Ignoring exception in command dm:
2020-06-20T12:03:12.635752+00:00 app[worker.1]: Traceback (most recent call last):
2020-06-20T12:03:12.635825+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/discord/ext/commands/core.py", line 85, in wrapped
2020-06-20T12:03:12.635826+00:00 app[worker.1]:     ret = await coro(*args, **kwargs)
2020-06-20T12:03:12.635861+00:00 app[worker.1]:   File "run.py", line 336, in dm
2020-06-20T12:03:12.635862+00:00 app[worker.1]:     await ctx.member.send(f"**{value}**")
2020-06-20T12:03:12.635920+00:00 app[worker.1]: AttributeError: 'Context' object has no attribute 'member'
2020-06-20T12:03:12.635960+00:00 app[worker.1]: 
2020-06-20T12:03:12.635963+00:00 app[worker.1]: The above exception was the direct cause of the following exception:
2020-06-20T12:03:12.635963+00:00 app[worker.1]: 
2020-06-20T12:03:12.635999+00:00 app[worker.1]: Traceback (most recent call last):
2020-06-20T12:03:12.636064+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/discord/ext/commands/bot.py", line 892, in invoke
2020-06-20T12:03:12.636065+00:00 app[worker.1]:     await ctx.command.invoke(ctx)
2020-06-20T12:03:12.636097+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/discord/ext/commands/core.py", line 824, in invoke
2020-06-20T12:03:12.636098+00:00 app[worker.1]:     await injected(*ctx.args, **ctx.kwargs)
2020-06-20T12:03:12.636131+00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.7/site-packages/discord/ext/commands/core.py", line 94, in wrapped
2020-06-20T12:03:12.636132+00:00 app[worker.1]:     raise CommandInvokeError(exc) from exc
2020-06-20T12:03:12.636184+00:00 app[worker.1]: discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'Context' object has no attribute 'member'

如有任何帮助,我们将不胜感激!

因此,理想情况下,您应该在命令中获取用户输入。使用 wait_for 可以让您省去一些麻烦,尽管您似乎已经尝试这样做了。这是一种简单的方法,您可以使用一些代码向提到的任何人发送消息。

@bot.command()
async def dm(ctx, user: discord.User, *, value):
    # Send a message to the mentioned user!
    await user.send(f"**{value}**")
    await user.send(f"||Sent by {ctx.author.display_name} via VX Helper.||")

从本质上讲,这里有几件事需要理解。首先,我们正在解析一个 user: discord.User 参数,这是将我们的消息发送给的人。使用 : discord.User 只是意味着 D.py 会自动将其转换为有效用户,因此我们可以直接发送到该对象。 这里要理解的第二件事是*, value的用法。简而言之,这意味着您提及用户之后出现的任何内容都将放入 value 变量的字符串中。您可以阅读更多关于 here

背后的逻辑

在此之后,您可以根据需要简单地完成您的代码。希望对您有所帮助!