javascript - 我正在制作 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]")
解决方案
setInterval(interval, 10000);
调用interval()
没有任何参数的函数,所以bumpChannel
里面interval()
是undefined
.
另外,setInterval(interval, bumpChannel, 10000)
毫秒间隔应该是第二个参数,而不是第三个。
推荐阅读
- python - 将 groupby() 组中的每个元素与该组中的唯一值进行比较,并获得相等的位置
- python - WINdows 7 上 PgAdmin 4 的“启动应用程序服务器时出错。无法启动应用程序服务器”
- node.js - 如何在不使用控制器中的 try 和 catch 的情况下全局处理 express 中的错误
- javascript - 用另一个更新页面的 html 及其对谷歌搜索/元标记的影响
- eclipse - Eclipse 插件开发 - 如何设置项目基线?
- android - 使用 Flutter Firebase 构建 apk 时出错
- java - 仅用于打印六位数字的 Java 随机类执行
- python - 如何在特定符号处拆分长文本文件并将拆分的文件并排粘贴
- reactjs - 在反应中设置和清除输入的默认值
- django - Django 2 在模板中调用 {% url %} 后添加 URL 参数