首页 文章

你如何将Discord bot读取DM发送给它? (discord.py)

提问于
浏览
0

我想方设法给DM的人,但是我想通过DM知道他们对机器人说什么,好像机器人“读取”DM,然后将它转发到我的不和谐服务器中的某个 Channels . 或者,甚至更好,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")

我这样做的方式不同于人们如何使用定义函数,我使用更基本的方法来制作命令 .

任何帮助表示赞赏!

1 回答

  • 1

    这里's a quick example. I'已将您现有的命令移动到实际的 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)
    

相关问题