【问题标题】:A bot that says user input一个说用户输入的机器人
【发布时间】:2020-10-05 20:35:12
【问题描述】:

所以基本上,我试图让我的代码让我的机器人看到当用户在任何文本频道中键入't!say #channel "message"' 时,机器人将转到指定的频道并说“消息”

const { MessageEmbed } = require('discord.js');

module.exports = {
 name: 'say',
 aliases: ['bc', 'broadcast'],
 description: 'Says your input via the bot',
 usage: '<input>',
 run: async (bot, message, args) => {
  if (!message.member.hasPermission(['MANAGE_MESSAGES'])) {
   if (message.mentions.channels.size == 0) {
    message.reply('please mention a channel first.');
   } else if (!message.member.hasPermission(['MANAGE_MESSAGES'])) {
    let targetChannel = message.mentions.channels.first();
    // Get the message to print

    const args = message.content.split(' ').slice(2);
    let saytext = args.join(' ');
    targetChannel.send(saytext);
    message.delete();
   }
  }
 },
};

当我尝试使用该命令时,我在控制台中收到一条错误消息“TypeError: client.commands.get(...).execute is not a function”

这是我的 index.js 文件中的代码 sn-p,命令提示符说它位于

client.on('message', (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 (!client.commands.has(command)) return;

 try {
  client.commands.get(command).execute(message, args);
 } catch (error) {
  console.error(error);
  message.reply('there was an error trying to execute that command!');
 }
});

有人知道我做错了什么吗?

【问题讨论】:

  • 您发布的错误表明问题出在client.commands.get().execute 但是在您发布的代码的 sn-p 中,该行不存在。请更新您的问题以显示发生错误的相关代码段,否则我们无法帮助您

标签: javascript node.js discord.js


【解决方案1】:

根据您更新的问题,我的猜测是该错误是由于您在命令上调用 .execute() 而命令没有 execute 函数引起的。从您的say 命令代码来看,您的命令似乎具有.run 函数。

所以尝试改变

client.commands.get(command).execute(message, args);

// I've added the `client` property because your run command takes three arguments, not two.
client.commands.get(command).run(client, message, args);

【讨论】:

  • 当我尝试这样做时,它导致所有其他命令抛出错误“TypeError:client.commands.get(...).run is not a function”并且say命令不会输出任何东西。
  • 所以对于每个其他命令,您都有一个execute 函数吗?为什么要使用 run 命令创建 Say 命令文件?无论如何,要解决该问题,您必须在 Say 命令中将 run: 更改为 execute: 并根据我的回答恢复您所做的更改
  • 至于为什么你的 Say 命令不会输出任何内容,execute 函数中的第一行检查用户是否没有MANAGE_MESSAGES 权限,如果是,则执行该命令。因此,确实具有所述权限的用户不会执行该命令。我的猜测是你的意思是检查用户是否有权限,而不是他们没有权限
猜你喜欢
  • 2017-10-14
  • 2021-09-28
  • 1970-01-01
  • 2019-11-23
  • 2021-07-27
  • 2019-02-19
  • 2017-07-10
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多