有人可以帮我找出我的代码有啥问题吗?

Posted

技术标签:

【中文标题】有人可以帮我找出我的代码有啥问题吗?【英文标题】:Can someone help me find what is wrong with my code?有人可以帮我找出我的代码有什么问题吗? 【发布时间】:2021-10-18 16:38:35 【问题描述】:

我试图做一个秒表命令,当你说 !duty on 然后 !duty off 时,它会计算停止它所花费的时间。它像普通秒表一样工作,但它是为了不和谐。我已经尝试修复错误一周,但我不明白为什么它不起作用请帮助我。错误在第 10 行 id 中,上面写着 message.guild.id

代码:

const Discord = require('discord.js');
const StopWatch = require("timer-stopwatch-dev"); 
const moment = require('moment');

module.exports = 
    name: 'duty',
    description: "This is a stopwatch command",
    async execute(client, message, args, CurrentTimers) 
      try 
          let guildTimers = CurrentTimers.get(message.guild.id);
          let guildTimersUser = guildTimers.get(message.author.id);
          if(!guildTimersUser) guildTimers.set(message.author.id, new StopWatch()); guildTimersUser = guildTimers.get(message.author.id); ;

 if(!args[0] || args[0] === 'on')
   if(guildTimersUser.isRunning()) return message.channel.send('You need to stop your shift first!')
   guildTimersUser.start();
   message.channel.send('You have started your shift')
  else if(args[0] === 'off')
  if(!guildTimersUser.isRunning()) return message.channel.send('You need to start the Stopwatch first!')
  guildTimersUser.stop();
  message.channel.send(new Discord.RichEmbed().setTitle('You have stopped the Stopwatch!').setDescription('Total Time: ' + dhm(guildTimersUser.ms)).setTimestamp());
 

 
 
 catch(err) 
   console.log(err)
 

 function dhm(ms)
  days = Math.floor(ms / (24*60*60*1000));
  daysms=ms % (24*60*60*1000);
  hours = Math.floor((daysms)/(60*60*1000));
  hoursms=ms % (60*60*1000);
  minutes = Math.floor((hoursms)/(60*1000));
  minutesms=ms % (60*1000);
  sec = Math.floor((minutesms)/(1000));
  return days+" days, "+hours+" hours, "+minutes+" minutes, "+sec+" seconds.";
    
   
  

我的主要:

require('dotenv').config();
//create cooldowns map
const cooldowns = new Map();
module.exports = (Discord, client, message) => 
    const prefix = '!';

    if(!message.content.startsWith(prefix) || message.author.bot) return;

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

    const command = client.commands.get(cmd) || 
                    client.commands.find(a => a.aliases && a.aliases.includes(cmd));
    
    if(command)

    //If cooldowns map doesn't have a command.name key then create one.
    if(!cooldowns.has(command.name))
        cooldowns.set(command.name, new Discord.Collection());
    

    const current_time = Date.now();
    const time_stamps = cooldowns.get(command.name);
    const cooldown_amount = (command.cooldown) * 1000;

    //If time_stamps has a key with the author's id then check the expiration time to send a message to a user.
    if(time_stamps.has(message.author.id))
        const expiration_time = time_stamps.get(message.author.id) + cooldown_amount;

        if(current_time < expiration_time)
            const time_left = (expiration_time - current_time) / 1000;

            return message.reply(`Please wait $time_left.toFixed(1) more seconds before using $command.name`);
        
    

    //If the author's id is not in time_stamps then add them with the current time.
    time_stamps.set(message.author.id, current_time);
    //Delete the user's id once the cooldown is over.
    setTimeout(() => time_stamps.delete(message.author.id), cooldown_amount);
    
    
    try
        command.execute(message,args, cmd, client, Discord, CurrentTimers);
     catch (err)
        message.reply("There was an error trying to execute this command!");
        console.log(err);
    


【问题讨论】:

欢迎来到 Stack Overflow。请务必查看How to ask 和tour。你应该写一个总结问题的标题。 你能显示实际的错误吗 是的TypeError: Cannot read property 'id' of undefined at Object.execute (/home/runner/New-Wave-RP/commands/duty.js:10:61) 【参考方案1】:

您没有显示实际错误,但我怀疑它类似于 Cannot read property 'id' of undefined。解决这个问题的方法是确保两件事:

    确保邮件不在 DM 中 确保您的执行参数正确传递。
command.execute(client, message, args, CurrentTimers)
//these may not be the same variable names, but make sure the values are correct

【讨论】:

你的意思是我的主? 是的。但是你调用这个文件 没有。您可以将其编辑到您的问题中。另外,在编辑之前,您可以接受我的编辑建议。 好的,我已经将它应用到我知道的主要代码中 您刚刚通过该编辑泄露了您的令牌!尽快重置您的令牌。

以上是关于有人可以帮我找出我的代码有啥问题吗?的主要内容,如果未能解决你的问题,请参考以下文章

有人可以帮我解决我的不和谐机器人代码错误吗? [复制]

有人可以帮我修复错误 pls-00049 吗?

有人可以帮我解决错误吗?

有人可以帮我并行化这个 C++ 代码吗?

我的android清单文件有啥问题吗?

我的代码看起来很完美,但显示错误的输出。有人可以帮我我在这里做错了啥吗?