我目前正在開發一個機器人,它以特定的時間間隔向給定的 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")
以防萬一你想知道:短時間間隔和不必要的訊息:“你好”,只是為了測驗目的。代碼中提到的“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"]
}
我已經將“dm_loop.start()”-方法添加到我的“on_ready()”中,但它根本不起作用。
如果有人能在這里幫助我,我會很高興。謝謝
uj5u.com熱心網友回復:
根據檔案,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")
不過,我確實推薦第一個選項,它要簡單得多。因為如果你沒有在命令中使用它,你將不得不創建一個自定義背景關系,我相信。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/334122.html
上一篇:當我更改元素的id時,通過ConstraintSet以編程方式添加障礙不起作用?
下一篇:JSON輸入意外結束-純JS
