【问题标题】:How do you have a Discord bot read DMs sent to it? (discord.py)你如何让 Discord 机器人读取发送给它的 DM? (discord.py)
【发布时间】:2018-06-19 22:08:58
【问题描述】:

我想出了一种与 DM 相关的方法,但我想知道他们通过 DM 回复机器人的话,就好像机器人“读取”了 DM,然后将其转发到某个不和谐的频道我的服务器,或者更好的是,直接私信给我。

这是我的起始代码:

if message.content.startswith("!dm"):
    if message.author.id == "[YOUR ID HERE]":
        memberID = "ID OF RECIPIENT"
        server = message.server
        person = discord.Server.get_member(server, memberID)
        await client.delete_message(message)
        await client.send_message(destination = person, content = "WHAT I'D LIKE TO SAY TO THEM")

与人们定义函数的方式相反,我用一种不同的方式来做,我用一种更基本的方式来制作命令。

感谢任何帮助!

【问题讨论】:

  • 私有Message 对象将具有message.server 属性None。我对你的要求有点困惑。你想要一个发送私人消息的命令,还是将私人消息“转发”到其他频道?你有那个频道的ID吗?
  • 我发布的命令是用于发送一条消息的代码,该消息是我在await client.send_message(destination = person, content = "Content of DM") 代码行中编写的字符串,每当我写“@ 987654326@".
  • 我想将我从 BOT 帐户(这不是 selfbot)收到的私人消息“转发”到服务器或我的某个文本频道,或者 DM 给我,无论你是谁喜欢。
  • 我可能希望将日志转发到的频道是 458778457539870742,我的 discord ID(如果你想走 DM 路线)是 267043073635254273。

标签: python-3.6 discord.py dm


【解决方案1】:

这是一个简单的例子。我已将您现有的命令移动到实际的 Command 对象中,因此转发逻辑是 on_message 中唯一的东西

from discord.ext import commands

bot = commands.bot('!')

# I've moved the command out of on_message so it doesn't get cluttered
@bot.event
async def on_message(message):
    channel = bot.get_channel('458778457539870742')
    if message.server is None and message.author != bot.user:
        await bot.send_message(channel, message.content)
    await bot.process_commands(message)

# This always sends the same message to the same person.  Is that what you want?
@bot.command(pass_context=True)
@commands.is_owner()  # The account that owns the bot
async def dm(ctx):
    memberID = "ID OF RECIPIENT"
    person = await bot.get_user_info(memberID)
    await bot.send_message(person, "WHAT I'D LIKE TO SAY TO THEM")
    await bot.delete_message(ctx.message)

【讨论】:

  • 是的,我只是更改了要发送 DM 的 ID。谢谢你的回答。
  • 可能是缺少 discord.py 的重写版本,但我得到一个类型错误:await client.process_commands 不能在等待表达式中使用。有什么想法吗?
  • 我的错误。你必须通过它message
  • message.author 将是写邮件的人的User 对象。
  • 另外,InvalidArgument: Destination must be Channel, PrivateChannel, User, or Object. Received generator。也许其中一个参数列出错误?收到错误的代码行:await client.send_message(person, "WHAT I'D LIKE TO SAY TO THEM")
猜你喜欢
  • 2021-06-13
  • 2020-06-30
  • 2021-03-17
  • 2021-03-11
  • 2020-09-14
  • 2018-05-10
  • 2021-12-03
  • 2022-01-03
  • 2019-01-07
相关资源
最近更新 更多