簡體   English   中英

使用 discord.py DM 好友

[英]DM friends with discord.py

我寫了這段代碼,無論如何都應該工作。 如果您不能幫我修復它,那么有人可以幫我在 python discum 庫中寫入嗎?:

from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='!')

@bot.event
async def on_ready():
    for user in bot.user.friends:
        message = "This Message is sent via DM"
        await bot.send_message(user, message)

bot.run("")

即使令牌有效,它也會拋出此錯誤。:

   Traceback (most recent call last):
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\http.py", line 300, in static_login
    data = await self.request(Route('GET', '/users/@me'))
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\http.py", line 254, in request
    raise HTTPException(r, data)
discord.errors.HTTPException: 401 Unauthorized (error code: 0): 401: Unauthorized

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

Traceback (most recent call last):
  File "C:\Users\ArtyF\AppData\Roaming\Microsoft\Windows\Network Shortcuts\HWMonitor\squirm\test.py", line 12, in <module>
    bot.run("ODI5Mzc2MDY2Nzk0ODE1NDk4.YNYJEQ.crJ2GzQZxRkCXy6fsd1zS8lxZCE")
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 723, in run
    return future.result()
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 702, in runner
    await self.start(*args, **kwargs)
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 665, in start
    await self.login(*args, bot=bot)
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\client.py", line 511, in login
    await self.http.static_login(token.strip(), bot=bot)
  File "C:\Users\ArtyF\AppData\Roaming\Python\Python39\site-packages\discord\http.py", line 304, in static_login
    raise LoginFailure('Improper token has been passed.') from exc
discord.errors.LoginFailure: Improper token has been passed.
Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001C7D4077940>
Traceback (most recent call last):
  File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Program Files\Python39\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 746, in call_soon
    self._check_closed()
  File "C:\Program Files\Python39\lib\asyncio\base_events.py", line 510, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

Process finished with exit code 1

如您所見,您收到錯誤discord.errors.LoginFailure: Improper token has been passed. . 您需要傳遞 API 密鑰才能進行身份驗證。

我認為對於 DM 用戶,您必須使用 User 對象!

例子

from discord.ext import commands
import discord

bot = commands.Bot(command_prefix='!')

Token = "Token Here"

@bot.event
async def on_ready():
    for user in bot.user.friends:
        message = "This Message is sent via DM"
        await user.send(message)

bot.run(Token)

暫無
暫無

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

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