【问题标题】:Sending Dm to a user command in discord.py-rewrite在 discord.py-rewrite 中将 Dm 发送给用户命令
【发布时间】:2020-10-10 14:49:10
【问题描述】:

所以,我试图制作一个 DM 命令,询问我们要将 DM 消息(值)发送给哪个用户,例如 - !vx dm THIS IS THE CONTENT OF THE MESSAGE 将向消息的作者发送一条消息,询问 - “”谁你要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'

任何帮助将不胜感激!

【问题讨论】:

  • ctx.author 不是ctx.member。我强烈建议查看文档
  • 这个discord.Member if not discord.Member else discord.Member也没有意义。
  • 我不想将消息发送给作者,我希望将其发送给他提到的用户。

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


【解决方案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背后的逻辑

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    • 2021-04-16
    • 1970-01-01
    • 2021-12-24
    • 2021-07-15
    • 2019-08-22
    • 2021-08-07
    相关资源
    最近更新 更多