首页 > 解决方案 > 我正在制作 Discord 机器人,它以特定消息触发的间隔发送消息但它不起作用?

问题描述

我正在制作一个机器人,它以当用户在特定频道中发送消息 [$here] 时触发的间隔发送消息,它会自动获取频道 ID 并仅在该频道中发送消息。

但是发生的事情是它发送了一次消息,然后,我收到了这个错误:

bumpChannel.send('!d bump');
                    ^
TypeError: Cannot read property 'send' of undefined

这是我的代码:

const { time } = require('console');
const Discord = require('discord.js');
const { send } = require('process');
const client = new Discord.Client();

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}!`);

    setInterval(interval, 10000);

    client.user.setActivity('With javascript');

    client.guilds.cache.forEach((guild) => {
        console.log(guild.name);

        guild.channels.cache.forEach((channel) => {
            console.log(` -${channel.name} ${channel.type} ${channel.id}`);
        });

        // General channel id:- 800353026904031257
    });

    let generalChannel = client.channels.cache.get('800353026904031257');

    generalChannel.send('Hello world');

    client.on('message', (receivedMessage) => {
        if (receivedMessage.content.startsWith('$')) {
            let bumpId = `${receivedMessage.channel.id}`;

            let bumpChannel = client.channels.cache.get(bumpId);
            processCommand(receivedMessage, bumpChannel);
        } else if (receivedMessage.author.id == client.user.id && receivedMessage.content == '!d bump') {
            let bumpId = `${receivedMessage.channel.id}`;

            let bumpChannel = client.channels.cache.get(bumpId);

            setInterval(interval, bumpChannel, 10000);
        }
    });
});

function processCommand(receivedMessage, bumpChannel) {
    let fullCommand = receivedMessage.content.substr(1);
    let splitCommand = fullCommand.split(' ');
    let primaryCommand = splitCommand[0];
    let arguments = splitCommand.splice(1);

    if (primaryCommand == 'here') {
        helpCommand(arguments, receivedMessage, bumpChannel);
    } else {
        receivedMessage.channel.send('I am not sure what are you talking about. Try `!here` to auto bump your server');
    }
}

function helpCommand(arguments, receivedMessage, bumpChannel) {
    if (arguments.length > 0) {
        receivedMessage.channel.send('I am not sure what are you talking about. Try `!here` to auto bump your server');
    } else {
        
        interval(bumpChannel);
    }
}

function interval(bumpChannel) {
   

    bumpChannel.send('!d bump');
}


client.login("[my bot token]")

标签: javascriptdiscord.js

解决方案


setInterval(interval, 10000);调用interval()没有任何参数的函数,所以bumpChannel里面interval()undefined.

另外,setInterval(interval, bumpChannel, 10000)毫秒间隔应该是第二个参数,而不是第三个。


推荐阅读