【问题标题】:Send message to specific channel (command on module.exports)向特定频道发送消息(module.exports 上的命令)
【发布时间】:2021-09-22 02:35:36
【问题描述】:

所以我不想把我的 Main.js 弄得一团糟,所以我尝试通过 module.exports 在其他documents.js 中创建所有可能的命令

基本上我需要,如果我发送命令,机器人将删除我的消息并在特定频道上发布评论+嵌入。 这就是我所拥有的(让它变得简单):

module.exports = {
    name: 'chtest',
    execute(message, args, Discord) {
        let chComment = 'Normal comment';
            chComment += '\nLine2';
            message.channel.send(chComment)
        const chEmbed = blablaEmbedCode
            message.channel.send(chEmbed)
        message.delete();
    },s
};

我读过另一个问题,他们使用

client.channels.cache.get(`Channel_ID`).send('Text')

我尝试使用它,但出现错误ReferenceError: client is not defined

我将 Client 添加到我的 execute 行: execute(client, message, args, Discord) {

现在我有另一个错误TypeError: Cannot read property 'cache' of undefined

而且...我现在不知道该怎么办。有什么解决办法吗? 提前谢谢你。

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    使用Message 类的client 属性试试这个。这是docs

    module.exports = {
        name: 'chtest',
        execute(message, args, Discord) {
            let channel = message.client.channels.cache.get('CHANNEL_ID');
       //channel is now the channel, unless it could not be found.
    channel.send('Message');     
    /*let chComment = 'Normal comment';
                chComment += '\nLine2';
                message.channel.send(chComment)
            const chEmbed = blablaEmbedCode
                message.channel.send(chEmbed)
            message.delete();*/
        },
    };
    

    【讨论】:

    • 哦,我明白了,非常感谢!!我测试了它,它有效!
    猜你喜欢
    • 2020-07-02
    • 2021-08-21
    • 2020-04-04
    • 2021-03-24
    • 2018-12-09
    • 2022-01-08
    • 2021-01-26
    • 2021-01-01
    相关资源
    最近更新 更多