首页 > 解决方案 > 使用 Python 通过用户 ID 发送不和谐的直接消息

问题描述

我制作了一个不和谐的机器人,它通过用户 ID 发送消息。
问题是我无法运行这个机器人。代码是:

import discord
from discord.ext import commands
import json
 
bot = commands.Bot(command_prefix='?')
 
@bot.event
async def on_ready():
    print(' [!] messaging..\n')
 
    with open("ids.json", "r") as file:
        data = json.load(file)
 
    indx = 0
    for i in data:
        indx += 1
        member = await bot.fetch_user(i)
        try:
            await member.send("message to send")
            print(f" [+] Sent message {indx} / {len(data)}")
        except Exception as e:
            print(f" [!] {e}")
 
    print(" [+] Done")
 
bot.run("bot token", bot = False)

我得到的错误是:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "main.py", line 17, in on_ready
    member = await bot.fetch_user(i)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/client.py", line 1384, in fetch_user
    data = await self.http.get_user(user_id)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/http.py", line 250, in request
    raise NotFound(r, data)
discord.errors.NotFound: 404 Not Found (error code: 10013): Unknown User

标签: pythonpython-3.xdiscord.py

解决方案


错误 404 表示未找到。

因此,在不和谐中,错误 404 表示未找到用户。

所以检查你的用户ID,然后再试一次


推荐阅读