NodeJS 是否有任何错误或错误的格式,将阻止我的机器人响应命令[关闭]

a64a0gku  于 2023-05-17  发布在  Node.js
关注(0)|答案(1)|浏览(184)

已关闭,此问题需要details or clarity。目前不接受答复。
**想改善这个问题吗?**通过editing this post添加详细信息并澄清问题。

8小时前关闭
Improve this question
是否有任何错误或错误的格式,将停止我的机器人从响应命令
有所有的意图,也需要完全的权限,但仍然没有响应命令非常新的编码一个机器人,所以试图学习什么,我可以,但具体的功能,我需要一些帮助来掌握

const Discord = require('discord.js');
const fs = require('fs');
const https = require('https');
const { EmbedBuilder } = require('discord.js');
const { ButtonBuilder } = require('discord.js')
const { ActionRowBuilder } = require('discord.js')
const { Client, IntentsBitField } = require('discord.js');

const client = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMembers,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.MessageContent,
  ],
});

const PREFIX = '%';

client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
});

client.on('message', async message => {
  if (!message.content.startsWith(prefix) || message.author.bot) return;

  const args = message.content.slice(prefix.length).trim().split(/ +/);
  const command = args.shift().toLowerCase();

  
  if (command === 'upload' && args.length === 2) {
    const [initDataFile, jsonObjectFile] = args;

    // Handle init.data file
    if (!initDataFile.endsWith('.data')) {
      return message.reply('Please upload a DayZ init.data file.');
    }

    const initFile = await message.attachments.find(
      attachment => attachment.name.endsWith('.data')
    );
    if (!initFile) {
      return message.reply('Please upload a DayZ init.data file.');
    }

    // Handle JSON object file
    if (!jsonObjectFile.endsWith('.json')) {
      return message.reply('Please upload a JSON file.');
    }

    const jsonObject = await message.attachments.find(
      attachment => attachment.name.endsWith('.json')
    );
    if (!jsonObject) {
      return message.reply('Please upload a JSON file.');
    }

    try {
      const cleanedInitData = await cleanInitData(initFile);
      const cleanedJsonObject = await cleanJsonObject(jsonObject);
      const objectsJson = await convertToObjectsJson(cleanedInitData, cleanedJsonObject);

      const messageStrings = [
        'Here is your cleaned and converted Objects.json file:',
        '```json',
        objectsJson,
        '```'
      ];
      await message.author.send(messageStrings.join('\n'));
    } catch (error) {
      console.error(error);
      await message.reply('An error occurred while processing your files. Please try again later.');
    }
  }
});

// Define a check and replace list
const checkReplaceMap = {
  "bldr_rock_bright_apart2": "DZ\\rocks\\rock_apart2.p3d"
};

const checkReplaceList = Object.keys(checkReplaceMap);

async function cleanInitData(initFile) {
  const dirtyData = await initFile.attachment.download();
  let cleanData = dirtyData
    .replace(/SpawnObject\(\"/g, "")
    .replace(/\",/g, " ")
    .replace(/;/g, "")
    .replace(/\)/g, "")
    .replace(/"/g, "")
    .replace(/  /g, ' ');

  for (const [key, value] of Object.entries(checkReplaceMap)) {
    const regex = new RegExp(`(^|\\s)${key}($|\\s)`, 'g');
    cleanData = cleanData.replace(regex, `$1${value}$2`);
  }

  return cleanData;
}

async function cleanJsonObject(jsonObject) {
  const jsonString = await jsonObject.attachment.download();
  let cleanJsonString = jsonString;

  for (const [key, value] of Object.entries(checkReplaceMap)) {
    const regex = new RegExp(`(^|\\s)${key}($|\\s)`, 'g');
    cleanJsonString = cleanJsonString.replace(regex, `$1${value}$2`);
  }

  return JSON.parse(cleanJsonString);
}

function convertToObjectsJson(initData, jsonObject) {
  const lines = initData.split('\n');
  const objects = lines.map(line => {
    const [name, pos, ypr, scale] = line.split(/[(),"]/).slice(1, -1).map(str => str.trim());
    const [x, y, z] = pos.split(/\s+/).map(str => Number(str));
    const [pitch, yaw, roll] = ypr.split(/\s+/).map(str => Number(str));
    return {
      name,
      pos: [x, y, z],
      ypr: [-pitch, yaw, roll],
      scale: Number(scale)
    };
  });

  jsonObject.Objects = objects;
  return JSON.stringify(jsonObject, null, 2);
}

client.login('TOKEN_HEAR');

是否有任何错误或错误的格式,将停止我的机器人从响应命令
有所有的意图,也需要完全的权限,但仍然没有响应命令非常新的编码一个机器人,所以试图学习什么,我可以,但具体的功能,我需要一些帮助来掌握
当用户上传init. c或json游戏文件时,机器人将基本上使用ckeck替换名称列表,它将搜索任何匹配并替换上传文件中的名称,然后将数据返回给用户。json为dayz objectspawner. json设置

4uqofj5v

4uqofj5v1#

您将前缀定义为PREFIX,但后来将其用作prefix,这意味着代码正在检查未定义的变量

if (!message.content.startsWith(PREFIX) || message.author.bot) return;
const args = message.content.slice(PREFIX.length).trim().split(/ +/);

你能试着换掉这些吗

const initFile = await message.attachments.find(
      attachment => attachment.name.endsWith('.data')
    );
    if (!initFile) {
      return message.reply('Please upload a DayZ init.data file.');
    }
const jsonObject = await message.attachments.find(
      attachment => attachment.name.endsWith('.json')
    );
    if (!jsonObject) {
      return message.reply('Please upload a JSON file.');
    }

用这些

const initFile = message.attachments.first();
    if (!initFile || !initFile.name.endsWith('.data')) {
      return message.reply('Please upload a DayZ init.data file.');
    }
const jsonObject = message.attachments.first();
    if (!jsonObject || !jsonObject.name.endsWith('.json')) {
      return message.reply('Please upload a JSON file.');
    }

相关问题