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

时间:2020-06-20 12:13:42

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

因此,我试图创建一个DM命令,询问我们要将DM消息(值)发送给哪个用户,例如!vx dm THIS IS THE CONTENT OF THE MESSAGE会向该消息的作者发送一条消息,询问- “您要向谁发送dm?”,作者将通过提及要向其发送dm的人进行回复,然后将其发送给用户,然后发送一条消息,说“我已经发送了dm”。消息发送到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'

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

因此,理想情况下,您应该尝试在命令内获取用户输入。尽管看起来似乎有些尝试,但这会为您节省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

背后的逻辑

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