你如何让 Discord 机器人读取发送给它的 DM?(discord.py) [英] How do you have a Discord bot read DMs sent to it? (discord.py)

查看:35
本文介绍了你如何让 Discord 机器人读取发送给它的 DM?(discord.py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've come up with a way to DM people, but I want to know what they say back to the bot through DMs, as if the bot "reads" the DM, and then forwards it to some channel in a discord server of mine, or, even better, DM it to me.

这是我的起始代码:

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

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

I do it a different way contrary to how people do it with defining functions, I use a more basic way of making commands.

感谢任何帮助!

推荐答案

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

Here's a quick example. I've moved your existing command into an actual Command object, so the forwarding logic is the only thing in 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)

这篇关于你如何让 Discord 机器人读取发送给它的 DM?(discord.py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆