【问题标题】:Discord.js Sending a message to another channelDiscord.js 向另一个频道发送消息
【发布时间】:2021-02-06 23:22:29
【问题描述】:

我目前有一个工作脚本,它根据发布者输入的参数分配角色。 我想要做的是让机器人向本地频道发送一条消息,以便发布者知道它有效,然后向另一个频道发送另一条消息,欢迎将角色获得服务器的用户。这就是我所拥有的:

const { client } = require('../main.js');


module.exports = {
    name: 'roles',
    description: "This is the command to give new recruits their role!",
    args: true,
    usage: '<department> <@user>',
    execute(message, args){

        //variables are defined here

        const channel = client.channels.cache.get('the channel id');
        message.delete(); //This deletes the posters command message

        if (!message.mentions.users.size){
            return message.channel.send('Please make sure to tag someone!');
             
        else if(args[0] === 'bcso'){
            user.roles.add(bcso);
            user.roles.add(member);
            user.roles.add(whitelist)
            user.roles.remove(leoR);
            message.channel.send(`${user} is now a full member of the BCSO`);
            channel.send('Message goes here');
        }
   //rest of my code

但我在控制台中遇到的错误是:“TypeError: Cannot read property 'channels' of undefined”

只是为了确认我设置了一个高级代码处理程序,因此该代码位于其自己文件中的命令文件夹内。

想法?

【问题讨论】:

  • 尝试将客户端作为execute() 参数传递并以这种方式引用它,而不是从另一个文件中导入它
  • @Elitezen 所以把它和另外两个一起添加?那么execute(message, args, client) ?
  • 是的,只要确保所有文件中的参数顺序相同
  • 我必须将它添加到甚至不引用它的命令文件中吗?
  • 是的,所有参数的顺序和大小都应该相同

标签: javascript discord.js message roles


【解决方案1】:

您导入的client 似乎没有定义。可能是因为您没有在main.js 文件中正确导出它。

幸运的是,您只需使用该消息即可找到频道。为此,您需要将 client.channels.cache.get('the channel id'); 更改为 message.client.channels.cache.get('ID HERE');

【讨论】:

  • 我很确定我试过了,而且我很确定它没有出错,但它也没有发布消息。我知道身份证是对的。我唯一能想到的是频道名称使用不和谐字体来更改字体,这会导致在消息中标记它的问题,但在将它们作为嵌入消息中的 ID 时,我从来没有遇到过问题。
  • 您可以发送console.log(channel) 看看您的频道是否真的被找到了吗?
  • 没想到。我会试试,让你知道
  • 我一定是第一次搞砸了,因为它起作用了。谢谢!