【问题标题】:UnhandledPromiseRejectionWarning: SyntaxError: Unexpected end of JSON inputUnhandledPromiseRejectionWarning:SyntaxError:JSON 输入意外结束
【发布时间】:2020-09-04 18:32:55
【问题描述】:
const Discord = require('discord.js');  
const client = new Discord.Client();
const fs = require("fs");
const config = require('./config.json');
const prefix = config.prefix;
const glois = JSON.parse(fs.readFileSync("global.json", "utf8"));

const escapeRegex = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');

client.on('ready', () => {
    console.log(`${client.user.username} is ready`);
    const activities_list = [({ activity: { name: `${client.guilds.cache.reduce((a, b) => a + b.memberCount, 0).toLocaleString()} Usern auf ${client.guilds.cache.size} Servern zu` }, status: 'online' }), ({ activity: { name: `${prefix} | Kanax in Discord ????` }, status: 'online' })]

      setInterval(() => {
          const index = Math.floor(Math.random() * (activities_list.length - 1) + 1);
          client.user.setPresence(activities_list[index]);
      }, 15000); // Runs this every 15 seconds.
  })

client.on("message", async message => {
  if (message.author.bot) return;
  const sender = message.author; 
 const channelmtn = message.mentions.channels.first()

 const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`);
    if (!prefixRegex.test(message.content)) return;

    const [, matchedPrefix] = message.content.match(prefixRegex);
 const args = message.content.slice(matchedPrefix.length).trim(/ +/g);
 const cmd = args.toLowerCase().split();

 if(cmd == "ping") {
   let Pong = "Pong!"
   var ping = Date.now() - message.createdTimestamp;
   let pembed = new Discord.MessageEmbed()
   .setTitle(client.user.username + "'s Ping")
   .setDescription(`:hourglass: **Latency**\n${ping}ms\n:heartbeat: **API**\n${client.ws.ping}ms`)
   .setColor("#00ffff")
   await message.channel.send('Pingen...').catch(console.error)
   .then(async message => {
    setTimeout(() => { message.edit(Pong); }, 2000);
    setTimeout(() => { message.edit(pembed); }, 2000);
  })
 };

 if (cmd == "invite") {
   let invembed = new Discord.MessageEmbed()
   .setTitle("Du kannst mich hier einladen")
   .setURL("https://discord.com/api/oauth2/authorize?client_id=711620266492690474&permissions=537390145&scope=bot")
   .setColor("#00ffff")
   message.channel.send(invembed)
  };

  if(cmd == "stop")
if (message.author.id === "602935358316871709") {
  console.log(`${sender.username} (${sender.id}) hat den Bot gestoppt`)
  message.channel.send(`<@${sender.id}> hat den Bot gestoppt`)
  process.exit();
}

if(message.content.startsWith(prefix + "set global")) {
if(!channelmtn) return message.reply("Erwähne einen Textchannel!")

    glois[message.guild.id] = {
      globalchat: channelmtn.id
    };
    fs.writeFile("global.json", JSON.stringify(glois), err => {
      if (err) console.log(err);
    });
    let setembed = new Discord.MessageEmbed()
    .setTitle("Setup")
    .setDescription(`Der Globalchat wurde zu <#${channelmtn.id}> gesetzt`)
    message.channel.send(setembed);
    };

  const sett = JSON.parse(fs.readFileSync("global.json", "utf8"));
  if (!sett[message.guild.id]) {
    sett[message.guild.id] = {
      globalchat: "698616682452746281"
    };
  }

  const globis = sett[message.guild.id].globalchat;
  if(message.channel.id === globis) {
    const embed = new Discord.MessageEmbed()
    .setTitle(message.author.tag)
    .setDescription(message.content)
    .setThumbnail(message.author.avatarURL) 
    .setColor("#000000")
    .setFooter(`Coming soon`)
    client.guilds.cache.forEach(guild => {
      try{
        client.channels.cache.get(glois[message.guild.id].globalchat).send(embed)
      } catch(error) {
        console.error(error)
      } 
    });
};
});

client.login(config.Token)

如果我发送gk!set global #test 然后在控制台中将保持这个

(node:8216) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
warning.js:32
(node:8216) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

【问题讨论】:

  • global.jsonconfig.json的内容是什么?需要为数组或对象,不能为空。也可能是你有错误的对象表示法,ide/linter 应该告诉你,=

标签: javascript node.js discord discord.js


【解决方案1】:

这只是意味着您的 JSON 不是有效的 JSON。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-22
    • 2016-08-05
    • 2018-01-23
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多