Discord Bot - Python的Java脚本代码

时间:2017-12-17 19:54:27

标签: javascript python discord

好的,所以我的java脚本机器人有一行代码,我试图在python中重新创建!如果有人能告诉我我需要做的部分。或者甚至如何在python中制作它我会非常欣赏它!

const fs = require("fs");
let points = JSON.parse(fs.readFileSync("./points.json", "utf8"));

client.on("message", message => {
  if (message.author.bot) return; // always ignore bots!

  // if the points don"t exist, init to 0;
  if (!points[message.author.id]) points[message.author.id] = {
    points: 0,
    level: 0
  };
  points[message.author.id].points++;

  // And then, we save the edited file.
  fs.writeFile("./points.json", JSON.stringify(points), (err) => {
    if (err) console.error(err)
  });
});

如果可能,我想继续使用.json!但如果他们的方式更好,请告诉我们! < 3

1 个答案:

答案 0 :(得分:0)

对于python 2,可以使用open打开文件。根据您是否要读取或写入,使用rw标志。例如:

with open('input.txt', 'r') as f:
    for line in f.readlines():
        print(line)
with open('output.txt', 'w') as f:
    f.write('my output')

对于json,请使用内置库https://docs.python.org/2/library/json.html

此代码段显示将json对象写入文件

import json

# store data in a dictionary
obj = {'name': 'joe'}

# write object to an output file
with open('output.txt', 'w') as f:
    json.dump(obj, f)