【问题标题】:How do i use a json as a variable我如何使用 json 作为变量
【发布时间】:2020-09-06 05:14:19
【问题描述】:

我正在制作一个不和谐的机器人并正在制作一个发誓过滤器,

const swears = require("./words.json");

const swearWords = [swears];
if (swearWords.some(word => message.content.includes(word))) {
message.reply("BRUH NO NAUGHTEE WURDS");
message.delete().catch(e => {});
}

我的 Json 是一个脏话列表,

{
"words": [
 "example",
 "example2",
 "example3"
]}

但是当有人发誓什么都没有发生时,有人可以帮助我吗?

【问题讨论】:

  • 如果你登录 console.log(swearWords)console.log(Array.isArray(swearWords)) 你会得到什么?

标签: javascript node.js json discord.js


【解决方案1】:

给你:

const swears = require("./words.json");

const swearWords = swears.words;

if (swearWords.some(word => message.content.includes(word))) {
  message.reply("BRUH NO NAUGHTEE WURDS");
  message.delete().catch(e => {});
}

当您 require 一个 JSON 文件时,基本上就像您复制粘贴 JSON 代码来代替 require(...) 调用一样。

【讨论】:

  • 非常感谢,我已经为此工作了 5 个小时
  • 如果答案对您有所帮助,请继续选择它作为接受的答案。对于花时间回答您问题的人来说,这是一个很好的奖励。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-05
相关资源
最近更新 更多