简体   繁体   English

Discord.js 向另一个频道发送消息

[英]Discord.js Sending a message to another channel

I currently have a working script that assigns roles based on the args that the poster inputs.我目前有一个工作脚本,它根据海报输入的参数分配角色。 What I am trying to do is have the bot send a message to the local channel so the poster knows that it worked and then send another message to another channel welcoming the user who got the roles to the server.我想要做的是让机器人向本地频道发送一条消息,以便发布者知道它有效,然后向另一个频道发送另一条消息,欢迎将角色授予服务器的用户。 This is what I have:这就是我所拥有的:

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

But the error I'm getting in the console is: "TypeError: Cannot read property 'channels' of undefined"但是我在控制台中得到的错误是:“TypeError:无法读取未定义的属性‘通道’”

Just to confirm I have an advanced code handler set up so this code is inside of a commands folder in its own file.只是为了确认我设置了一个高级代码处理程序,因此此代码位于其自己文件中的命令文件夹内。

Ideas?想法?

It appears that your imported client is not defined.您导入的client似乎未定义。 Maybe because you don't export it correctly in your main.js file.也许是因为您没有在main.js文件中正确导出它。

Luckily you can simply use the message to find the channel.幸运的是,您可以简单地使用该消息来查找频道。 To do that you need to change client.channels.cache.get('the channel id');为此,您需要更改client.channels.cache.get('the channel id'); to message.client.channels.cache.get('ID HERE');message.client.channels.cache.get('ID HERE');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM