Discord-Bot消息Users失败



我目前在一个机器人,消息用户在给定的json文件在一定的时间间隔。我尝试了这段代码,但它没有做它应该做的事情,也没有给我一个我可以处理的错误。

@tasks.loop(seconds=10)
async def dm_loop():
with open("users.json" "r") as file:
users = json.load(file)
for stgru in users:
for i in users[stgru]:
user = await client.fetch_user(i)
await user.send("hello")

如果您想知道:短时间间隔和不必要的消息:"hello"只是为了测试目的。代码中提到的"users.json"文件的格式如下:

{
"724": ["name1#2819", "name2#2781", "name3#2891"],
"727": [],
"986": ["name4#0192"],
"840": ["name5#1221", "name6#6652"],
"798": ["name7#3312", "name8#8242", "name9#1153", "name10#3318"]
}

我已经在我的on_ready()中添加了dm_loop.start()"方法但是它根本不起作用

如果有人能帮我解决这个问题,我会很高兴的。由于

根据文档,fetch_user根据用户ID查找用户,因此您需要存储用户ID而不是用户名。

https://discordpy.readthedocs.io/en/master/ext/commands/api.html?highlight=fetch_user discord.ext.commands.Bot.fetch_user

否则,您可以创建自己的UserConverter。下面是一个关于如何做到这一点的例子。

from discord.ext import commands
[...]
user_name = "some_tag#1234"
user = await commands.converter.UserConverter().convert(ctx, argument=user_name)
await user.send("Hello")

我确实推荐第一个选项,它简单得多。因为你必须创建一个自定义上下文,如果你不使用这个命令,我相信。

相关内容

最新更新