discord.py - 没有向用户发送 DM

discord.py - No DM sent to the user

我正在制作 discord.Client. I have a DM command that sends a DM to a specific user, but no message is sent to the user when the command is run, but a message is sent on the Context.channel

这是我的代码:

import discord, asyncio

app = discord.Client()

@app.event
async def on_message(message):
    if message.content.startswith('!DM'):
        usernotsending = []
        content = message.content
        msg = await message.channel.send('메시지를 보내고 있습니다!')
        i = app.user

        # 봇의 모든 유저를 for문으로 적용
        for i in app.user:
            try:
                if i == app.user:
                     return

                # 해당 유저의 DM을 염
                await i.create_dm()
                # 내용전송

                await app.dmchannel.send(content)
                # DiscordAPI 에서 오류가 발생했을경우

            except discord.HTTPException:
                # DM을 보내지 못한 유저 태그 list에 저장
                usernotsending.append(f'{i.name}#{i.discriminator}')

                messageing = """
                아래 유저들에게 메시지를 전송하지 못했습니다!
                직접 보내주시거나, 따로 전달을 해드려야됩니다!
                """

                for msg in usernotsending:
                    # message 에 유저 태그 추가
                    messageing += msg

            # 메시지 전송
            await msg.edit(content=messageing)

Context is only part of a commands.Bot 实例。您的代码和您的解释似乎不匹配。假设您想私信作者:

import discord

app = discord.Client()

@app.event
async def on_message(message):
    if message.content.startswith('!DM'):
        try:
            await message.author.send(...)
        except discord.HTTPException:
            ...

如果您想私信机器人可以看到的每个人:

import discord

app = discord.Client()

@app.event
async def on_message(message):
    if message.content.startswith('!DM'):
        for user in app.users:
            try:
                await user.send(...)
            except discord.HTTPException:
                ...