使用自定义功能编辑json文件| discord.js,nodejs

时间:2020-07-12 22:44:01

标签: javascript node.js discord discord.js

嘿,我有一个类似data.json的JSON数据文件:

"servers":
{
   "sv-123344":
   {
      "owner": "id",
      "vstats": false
   },
   "sv-44332211":
   {
      "owner": "id",
      "vstats": false
   }
},

"users":
{
    "uid-111222":
    {
       "favlist":
       [
          "1",
          "2",
          "3"
       ]
    },

    "uid-445566":
    {
       "favlist":
       [
          "1",
          "2",
          "3"
       ]
    }
}

所以基本上我想要3个我不知道如何编写它们的函数

1-我需要一个函数来检查用户的favlist中是否存在输入 像这样:if(checkExist(userid, input) == true)

2-我需要一个函数来将输入添加到favlist 像这样:addFav(userid, input)

3-,我需要一个删除功能才能从favlist删除输入 像:removeFav(userid, input)

因此,如果有人可以通过此代码帮助我,我将非常感谢 所有的功劳将归功于对我有帮助的人

2 个答案:

答案 0 :(得分:0)

1。)您的JSON格式错误。您需要像这样用{}包围一切:

{
  "servers":
  {
    ...
  },
  "users":
  {
    ...
  }
}

因此,如果收藏夹为let favlist = [1, 2, 3],则可以使用favlist.includes(value)查看收藏夹中是否包含输入内容。只需让您的checkExists函数返回此函数的结果即可。

由于收藏夹是数组,因此可以使用favlist.push(value)向数组添加值。不过,它不会检查重复项。

要从数组中删除特定值,您需要找到该值的索引。使用以下内容:

/* if value = 2 and favlist = [1,2,3], then this gives index = 1 */
let index = favlist.findIndex(x => x === value); 
favlist.splice(index, 1);

请记住要避免重复。

答案 1 :(得分:0)

您的class BrowserFrame(tk.Frame): def __init__(self, master, navigation_bar=None): self.navigation_bar = navigation_bar cef.Initialize() self.closing = False self.browser = None tk.Frame.__init__(self, master) self.bind("<FocusIn>", self.on_focus_in) self.bind("<FocusOut>", self.on_focus_out) self.bind("<Configure>", self.on_configure) self.focus_set() def embed_browser(self): window_info = cef.WindowInfo() rect = [0, 0, self.winfo_width(), self.winfo_height()] window_info.SetAsChild(self.get_window_handle(), rect) self.browser = cef.CreateBrowserSync(window_info, url=f"file:///{os.getcwd()}/data/map_location.html") #todo assert self.browser self.browser.SetClientHandler(LoadHandler(self)) self.browser.SetClientHandler(FocusHandler(self)) self.message_loop_work() def get_window_handle(self): if self.winfo_id() > 0: return self.winfo_id() elif MAC: from AppKit import NSApp import objc return objc.pyobjc_id(NSApp.windows()[-1].contentView()) else: raise Exception("Couldn't obtain window handle") def message_loop_work(self): cef.MessageLoopWork() self.after(10, self.message_loop_work) def on_configure(self, _): if not self.browser: self.embed_browser() def on_root_configure(self): # Root <Configure> event will be called when top window is moved if self.browser: self.browser.NotifyMoveOrResizeStarted() def on_mainframe_configure(self, width, height): if self.browser: if WINDOWS: ctypes.windll.user32.SetWindowPos( self.browser.GetWindowHandle(), 0, 0, 0, width, height, 0x0002) elif LINUX: self.browser.SetBounds(0, 0, width, height) self.browser.NotifyMoveOrResizeStarted() def on_focus_in(self, _): if self.browser: self.browser.SetFocus(True) def on_focus_out(self, _): if self.browser: self.browser.SetFocus(False) def on_root_close(self): if self.browser: self.browser.CloseBrowser(True) self.clear_browser_references() self.destroy() def clear_browser_references(self): self.browser = None class LoadHandler(object): def __init__(self, browser_frame): self.browser_frame = browser_frame def OnLoadStart(self, browser, **_): if self.browser_frame.master.navigation_bar: self.browser_frame.master.navigation_bar.set_url(browser.GetUrl()) class FocusHandler(object): def __init__(self, browser_frame): self.browser_frame = browser_frame def OnTakeFocus(self, next_component, **_): self.browser_frame.browser.SetFocus(False) def OnSetFocus(self, source, **_): self.browser_frame.browser.SetFocus(False) return False def OnGotFocus(self, **_): self.browser_frame.browser.SetFocus(False) 对象缺少一些JSON才能从数组/对象中删除数据,您需要使用filtermap

{}