我正在使用discord.py制作一个Discorde机器人,并且有一个很好的小设置,所以当我向我的json文件添加一些东西时,它每隔20秒判断一次,如果有什么改变了,它将发送一个嵌入了新内容的Discorde.唯一的问题是,它不是发送最新的行,而是发送每一行(所有内容中的行)
以下是代码:
@tasks.loop(seconds=10)
async def update():
print("Games Update Loop Started")
last_modified = os.path.getmtime('Game Bot/games.json')
last_length = 0 # initialize the last length of the JSON file
while True:
current_modified = os.path.getmtime('Game Bot/games.json')
if current_modified != last_modified:
last_modified = current_modified
with open('Game Bot/games.json', 'r') as f:
config_data = json.load(f)
if len(config_data) > last_length: # check if new line(s) have been added
for key, value in config_data.items():
embed = discord.Embed(title="New Game Added!", color=0xFFFFFF)
embed.add_field(name=key, value=f"[Download]({value})")
channel = bot.get_channel(1090051943633780758)
await channel.send(embed=embed)
print("Games updated.")
last_length = len(config_data) # update the last length of the JSON file
await asyncio.sleep(10)
这是我的.json文件
{
"Sons Of The Forest [MULTIPLAYER]" : "https://youtube.com/watch?v=dQw4w9WgXcQ",
"Geometry Dash [FULL VERSION]" : "https://youtube.com/watch?v=dQw4w9WgX",
"Rust [MULTIPLAYER]" : "https://youtube.com/watch?v=dQw4w"
}
我几乎try 了我能想到的所有东西(基本上没有什么是因为我很傻),并希望它只发送最新的内容行.它发出了非常丰富的内容.