将最新的行添加到json中并发送discord.py



我正在使用discord.py制作一个不和谐机器人,并有一个很好的小设置,所以当我向我的json文件添加一些东西时,它每20秒检查一次,如果有什么改变,那么它将发送一个不和谐嵌入新内容。唯一的问题是,它不是发送最新的行,而是发送每一行(行作为所有内容)

代码如下:

@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"
}

我试了我能想到的一切(基本上没有,因为我愚蠢),并期望它只发送最新的内容行。它发送了每一行内容。

您的JSON文件格式似乎不正确,请将数据放入列表并添加键以使其更易于排序。将JSON文件重新格式化为类似于下面的内容。

{
"videos": [
{
"title": "Sons Of The Forest [MULTIPLAYER]", "link": "https://youtube.com/watch?v=dQw4w9WgXcQ"
},
{ "title": "Geometry Dash [FULL VERSION]", "link":  "https://youtube.com/watch?v=dQw4w9WgX" },
{ "title": "Rust [MULTIPLAYER]", "link":  "https://youtube.com/watch?v=dQw4w" }
]

所有的对象都放在一个列表中,这意味着你可以对它们进行索引和排序。以前,您需要键才能访问对象。这意味着如果您想明确返回对象的标题,就需要对象的标题。例如,您需要键"Sons Of The Forest [MULTIPLAYER]"来返回"https://youtube.com/watch?v=dQw4w9WgXcQ"。否则,您只需要遍历每个对象以获得您正在查找的对象,因此会出现错误。

对于列表,要获得最新的添加项,您所需要做的就是索引最后一项。使用更新的JSON,您可以执行config_data['videos']来获取列表。然后,您可以在其上使用len函数。要访问最新的"行",请执行config_data['videos][-1]-1,访问列表中的最后一项。

一旦每个对象都在列表中,您就可以添加键来访问诸如标题或链接之类的值。这意味着如果需要某个值,就从列表中将其编入索引,并通过括号访问所需的值。因此,要返回最新行的标题,您可以执行config_data['videos'][-1]['title].

所有的括号都有点混乱,所以你应该先把它们中的一些放在变量中。以下是一些更新后的代码,其中的逻辑更改与JSON一致。


@tasks.loop(seconds=10)
async def update():
print("Games Update Loop Started")
last_modified = os.path.getmtime('Game Bot/games.json')
last_length = 0
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)
videos = config_data['videos'] # Getting list
if len(videos) > last_length: 
newest_video = videos[-1] # Getting last item in list

title = newest_video['title']
link = newest_video['link'] # Accessing link of object
embed = discord.Embed(
title="New Game Added!", color=0xFFFFFF)
embed.add_field(name=title, value=f"[Download]({link})")
channel = bot.get_channel(1090051943633780758)
await channel.send(embed=embed)
print("Games updated.")
last_length = len(videos)
await asyncio.sleep(10)

效果很好。

@tasks.loop(seconds=10)
async def update():
print("Games Update Loop Started")
last_modified = os.path.getmtime('Game Bot/games.json')
last_length = 0
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)
videos = config_data['videos'] # Getting list
if len(videos) > last_length: 
newest_video = videos[-1] # Getting last item in list

title = newest_video['title']
link = newest_video['link'] # Accessing link of object
embed = discord.Embed(
title="New Game Added!", color=0xFFFFFF)
embed.add_field(name=title, value=f"[Download]({link})")
channel = bot.get_channel(1090051943633780758)
await channel.send(embed=embed)
print("Games updated.")
last_length = len(videos)
await asyncio.sleep(10)

最新更新