首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Discord.js pat命令

Discord.js pat命令
EN

Stack Overflow用户
提问于 2021-01-23 20:25:01
回答 1查看 573关注 0票数 0

我试图为我的不和谐机器人发出和拍拍命令,但它不起作用。

我收到以下错误:

代码语言:javascript
复制
(node:14648) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'users' of undefined
    at Object.execute (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\commands\pat.js:13:26)
    at Client.<anonymous> (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\index.js:90:13)
    at Client.emit (events.js:315:20)
    at MessageCreateAction.handle (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\node_modules\discord.js\src\client\actions\MessageCreate.js:31:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:384:31)
    at WebSocketShard.onPacket (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\nbabu\OneDrive\Desktop\Discord bots\Raphtalia bot\Raphtalia bot\node_modules\ws\lib\event-target.js:125:16)
    at WebSocket.emit (events.js:315:20)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:14648) 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)
(node:14648) [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.

代码语言:javascript
复制
const Discord = require('discord.js');
const { patch } = require('node-superfetch');
const superagent = require('superagent');
const customisation = require('../customisation.json');

module.exports = {
  name: "pat",
  description: "Pat someone UwU",
  aliases:["pat"],
  category: "fun",

  execute: async (client, message, args, tools) => {
    if (message.mentions.users.first().id)
    return message.reply("you almost mention someone to pat them");
    
    if (message.mentions.users.first().id) 
    return message.channel.send('UwU');

    const { body } = await superagent
    .get("https://nekos.life/api/pat");
    
    const embed = new Discord.MessageEmbed()
    .catch(body)
    .setColor("#3bb9ff")
    .setTitle(`i see that is, ${message.author.username} patted ${message.mentions.users.first().username}`) 
    .setImage(body.url)
    .setFooter(`© ${customisation.ownername}`);
    message.channel.send({embed})
}}

命令Index.js responsabile

代码语言:javascript
复制
const commandFiles = readdirSync(join(__dirname, "commands")).filter((file) => file.endsWith(".js"));
for (const file of commandFiles) {
  const command = require(join(__dirname, "commands", `${file}`));
  client.commands.set(command.name, command);
}

client.on("message", async (message) => {
  if (message.author.bot) return;
  if (!message.guild) return;

  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().split(/ +/);
  const commandName = args.shift().toLowerCase();

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

  if (!command) return;

  if (!cooldowns.has(command.name)) {
    cooldowns.set(command.name, new Collection());
  }

  const now = Date.now();
  const timestamps = cooldowns.get(command.name);
  const cooldownAmount = (command.cooldown || 1) * 1000;

  if (timestamps.has(message.author.id)) {
    const expirationTime = timestamps.get(message.author.id) + cooldownAmount;

    if (now < expirationTime) {
      const timeLeft = (expirationTime - now) / 1000;
      return message.reply(
        `please wait ${timeLeft.toFixed(1)} more second(s) before reusing the \`${command.name}\` command.`
      );
    }
  }

  timestamps.set(message.author.id, now);
  setTimeout(() => timestamps.delete(message.author.id), cooldownAmount);

  try {
    command.execute(message, args);
  } catch (error) {
    console.error(error);
    message.reply("There was an error executing that command.").catch(console.error);
  }
});

I您看到错误/当我可以升级代码时,请告诉我。,我的不和是IncarnateWill#6969。,如果您需要其他东西,请添加我,我将屏幕上共享bot.的代码。

EN

回答 1

Stack Overflow用户

发布于 2021-01-23 20:56:04

好吧,message总是有一个mentions属性,即使没有提到(当消息中没有人提到时,message.mentions.users.first()返回undefined )。由于上面的代码看起来是正确的,如果您没有将message.mentions对象向下传递给execute方法,或者传递参数的顺序不对,那么唯一可能的方法是undefined

更新:

在您的index.js中,您要像这样调用execute:command.execute(message, args)。您需要用正确的参数更新它:

代码语言:javascript
复制
// ...
try {
  command.execute(client, message, args)
} catch (error) {

// ...

pat.js

代码语言:javascript
复制
// ...
execute: async (client, message, args) => {
  // ...
}}

如果您有其他当前使用该顺序的命令,则可以添加client作为第三个参数,因此不需要更新它们,只需要更新这两个命令:

代码语言:javascript
复制
// in index.js

try {
  command.execute(message, args, client)
} catch (error) {

// in pat.js

execute: async (message, args, client) => {
  // ...
}}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65864024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档