簡體   English   中英

如何訪問discord.py中的DM頻道

[英]How to access a DM channel in discord.py

我有一個機器人,我通過讓它向我的帳戶發送 DM 來測試它,但現在我想刪除已經發送的消息。 我到處搜索,但沒有找到任何東西。 簡而言之,我需要知道如何讓我的機器人從命令用戶的 DM 中刪除它自己的消息。

我嘗試了 @Mars Buttfield-Addison 告訴我的方法,但我收到了一個錯誤 ERROR:

Ignoring exception in command clear_dm:
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
    ret = await coro(*args, **kwargs)
  File "main.py", line 50, in clear_dm
    async for message in client.user.dm_channel.history(limit=messages_to_remove):
AttributeError: 'ClientUser' object has no attribute 'dm_channel'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 902, in invoke
    await ctx.command.invoke(ctx)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 864, in invoke
    await injected(*ctx.args, **ctx.kwargs)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
    raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'ClientUser' object has no attribute 'dm_channel'

您可以訪問會員object 上的dm_channel屬性。

現在,我不完全明白你想說什么,但如果你想在 DM 上接收消息並檢查它是否在 dm 上,你可以這樣做:

import discord
import asyncio

TOKEN = ''
intents = discord.Intents(messages=True, guilds=False, members=False)
client = discord.Client(intents=intents)

@client.event
async def on_message(message):
    if isinstance(message.channel, discord.channel.DMChannel):
        print("got a DM")

client.run(TOKEN)

現在如果你想刪除東西,你可以像正常一樣訪問這個頻道,通過message.channel (你可以刪除,發送消息,或者做任何你喜歡的事情,就像你在公會上一樣)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM