我正在制作一个调度 Discord.js 机器人,但它没有发送消息

时间:2021-05-31 01:00:02

标签: javascript discord.js node-schedule

我正在制作一个 discord.js 调度机器人。我正在为此使用节点计划。它没有抛出任何错误,但仍然没有发送消息。我做错了什么,我该如何摆脱这个问题? (提前致谢)

我的代码是:

const Discord = require('discord.js');
const client = new Discord.Client();
const schedule = require('node-schedule');


client.once('ready', () => {
  console.log('Ready!');
});

client.login('TOKEN IS HERE');

const rule = new schedule.RecurrenceRule();
rule.tz = 'EDT';


client.on('message', message => {
  if (message.content === '!schedule 9pm meeting') {
    message.channel.send('Alright. I will announce it for you, friend! :smiley:');
    const job = schedule.scheduleJob('00 21 * * *', function () {
      client.channels.cache.get("channel id is here").send("This is a test message. Does it work?");
    });
  }
});

1 个答案:

答案 0 :(得分:1)

您不能从 { "compilerOptions": { "target": "es5", "module": "esnext", "lib": ["ESNext", "DOM", "DOM.Iterable"], "declaration": true, "strict": true, "noImplicitAny": true, "strictNullChecks": true, "noImplicitThis": true, "alwaysStrict": true, "moduleResolution": "node", "noUnusedLocals": false, "noUnusedParameters": false, "jsx": "preserve", "downlevelIteration": true, "noImplicitReturns": true, "noFallthroughCasesInSwitch": true, "inlineSourceMap": true, "inlineSources": true, "experimentalDecorators": true, "strictPropertyInitialization": true, "baseUrl": ".", "allowJs": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true, "noEmit": true, "esModuleInterop": true, "resolveJsonModule": true, "isolatedModules": true, "paths": { "@/components/*": ["components/*"], "@/config/*": ["config/*"], "@/graphql/*": ["graphql/*"], "@/lib/*": ["lib/*"], "@/pages/*": ["pages/*"], "@/scripts/*": ["scripts/*"], "@/styles/*": ["styles/*"], "@/test/*": ["test/*"], "@/types/*": ["types/*"] } }, "include": ["**/*.d.ts", "**/*.ts", "**/*.tsx", "**/*.js"], "exclude": ["node_modules", ".next", "out"] } 函数内部运行 schedule.scheduleJob 并期望消息仍然存在。 Discord API 期望在超时之前的特定时间内响应网络钩子。

此外,如果机器人在云服务上运行,它运行的节点可能会偶尔重新启动,这会弄乱内存数据,例如在 client.on 中附加 cron 作业。

持久化数据

您可能应该获得用户的预定时间并将数据保存在某种数据库中。您应该使用数据库读/写来保存您的云提供商重新启动之间的数据(除非您有付费订阅)。

有一个全局定时任务或间隔

由于您可能有数千个预定的会议,因此最好在特定时间间隔内检查会议并同时发送所有提醒。

假设用户不能给我们一个比某一分钟更具体的时间。然后我们可以每分钟检查一次提醒,知道我们会在会议开始前通知用户。

node-schedule

新的提醒处理

// Run checkForReminders every 60 seconds to scan DB for outstanding reminders
setInterval(checkForReminders, 60000);

// Parse reminder request, save to DB, DM confirmation to user
client.on('message', (msg) => {
    createNewReminder(msg);
});

稍后向公会或用户发送消息

为了稍后发送消息,请将 const createNewReminder = (msg) => { const formattedMessage = formatMessage(msg) // If message isn't a remindme command, cease function execution if (!formattedMessage.startsWith('!remindme')) { return } // Determine if message contains a number to assign to intervalInteger checkForNumbersInMessage(formattedMessage) // Final format for message to be sent at reminderTime const messageToDeliverToUser = formattedMessage.replace('!remindme', '') // Set integer and verb values for moment.add() parameters const intervalInteger = parseInt(checkForNumbersInMessage(formattedMessage)) const intervalVerb = setIntervalVerb(formattedMessage) // Format time to send reminder to UTC timezone const reminderTime = moment().utc().add(intervalInteger, intervalVerb).format('YYYY-MM-DD HH:mm:ss') // Save reminder to DB saveNewReminder(msg.author.id, msg.author.username, messageToDeliverToUser, reminderTime) // Send embedded message to author & notify author in channel of remindertime request const embed = createEmbed(messageToDeliverToUser, reminderTime) msg.author.send(embed) msg.channel.send(`${msg.author} - A reminder confirmation has been sent to your DMs. I will DM you again at the requested reminder time`) } userId 保存到数据库,然后从 Discord 客户端检索用户或公会,并发送消息。

guildId

代码示例取自 NathanDennis/discord-reminder-bot。查看存储库以获取完整示例。他对他的代码进行了注释,因此很容易理解。