Discord bot发送生日消息

桁架

我做了一个Discord机器人,我希望它能够检查当前日期,如果当前日期是用户生日中的任何一个,它将标记他们并说生日快乐。我已经尝试使用Node.js的日期,例如

if(Date === "this or that")

但这似乎不起作用。因此,我需要帮助的是:

  • 检查当前日期(例如每天检查一次等等)
  • 在当前日期发送消息

因为我们生日不多,所以我可能会硬编码所有生日(我知道这不是一个好习惯,但是我只是希望现在可以工作,以后可以完善)

如果有人可以帮助我,那就太好了。

这是我的index.js文件的外观(如果相关):

const discord = require("discord.js");
const config = require("./config.json");
const client = new discord.Client();
const prefix = '>';
const fs = require('fs');
const { time, debug } = require("console");
client.commands = new discord.Collection();
const commandFiles = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'));
for(const file of commandFiles)
{
    const command = require(`./commands/${file}`)
    client.commands.set(command.name, command);
}

client.once('ready', () => 
{
    console.log(`${client.user.username} is online`);
})

client.on('message', message =>
{
    if(!message.content.startsWith(prefix) || message.author.bot) return;
    const args = message.content.slice(prefix.length).split(/ + /);
    const command = args.shift().toLowerCase();

    if(command === 'ping')
    {
        client.commands.get('ping').execute(message, args);
    }else if(command === 'wololo')
    {
        client.commands.get('wololo').execute(message, args);        
    }
})

client.login('TOKEN')

编辑:

我知道我在“ client.on('message',message => {})”中写了“ if(date)”语句,因此除非您在特定时间写了一条命令,否则显然不会检查。我要使用什么功能?

加布里埃尔·安德拉德(Gabriel Andrade)

您可以在此处查看Discord.js事件的完整列表
Discord.js中没有基于计划的时间触发的内置事件。

为此,您可以构建一个时间系统,根据超时或间隔每天触发事件。或者...您可以使用诸如node-cron之类的库来为您执行此操作。

我将尝试举例说明您可以使用上述node-cron做到这一点的方法。

首先,您需要使用node-cron软件包,因此可以npm像对discord.js本身一样使用。因此,在终端上粘贴并执行以下命令:

npm install node-cron

不幸的是,Discord.js无法为您提供获取用户生日日期的方法。因此,对生日进行硬编码(如您所说的还可以),对于本示例,您将发送祝贺消息的通用频道ID也可以这样:

const Discord = require('discord.js');
const cron = require('node-cron');

const client = new Discord.Client();

// Hard Coded Storing Birthdays in a Map
// With the key beeing the user ID
// And the value a simplified date object
const birthdays = new Map();
birthdays.set('User 1 Birthday Id', { month: 'Jan', day: 26 });
birthdays.set('User 2 Birthday Id', { month: 'Jun', day: 13 });
birthdays.set('User 3 Birthday Id', { month: 'Aug', day: 17 });

const setSchedules = () => {
  // Fetch the general channel that you'll send the birthday message
  const general = client.channels.cache.get('Your General Channels Id Go Here');

  // For every birthday
  birthdays.forEach((birthday, userId) => {
    // Define the user object of the user Id
    const user = client.users.cache.get(userId);
    
    // Create a cron schedule
    cron.schedule(`* * ${birthday.day} ${birthday.month} *`, () => {
      general.send(`Today's ${user.toString()} birthday, congratulations!`);
    });
  });
};

client.on('ready', setSchedules);
client.login('Your Token Goes Here');

这不是经过优化的,只是打算给出如何实现的总体思路。请注意,尽管应该在设置任务之前实例化客户端,所以我建议像本例一样进行操作,并安排好计划。

无论如何,您始终可以使用已创建并经过测试的bot,例如Birthday Bot

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Discord Bot从udp连接向特定通道发送消息

来自分类Dev

在for循环中发送多个消息Discord python bot

来自分类Dev

尽管异步/等待,Discord bot仍以错误的顺序发送消息

来自分类Dev

如何使用Discord Bot发送问候消息-NodeJS

来自分类Dev

NodeJS discord bot命令获取错误无法发送空消息

来自分类Dev

如何让我的 Discord Bot 在启动后立即发送消息?

来自分类Dev

JS discord bot 出现错误:无法发送空消息

来自分类Dev

Python - Discord Bot Loop 命令并在私人消息中发送消息

来自分类Dev

Discord bot:发送消息并等待发送另一个

来自分类Dev

当成员不在JSON文件中时,Discord.py bot无法发送消息

来自分类Dev

Discord.py如何使用BOT获取已发送消息的ID?

来自分类Dev

Python Discord Bot如何在一条消息中发送多行?

来自分类Dev

如何使我的Discord Bot向服务器发送更干净的消息

来自分类Dev

使用discord bot发送高优先级和低优先级消息

来自分类Dev

Discord.py:发送长消息

来自分类Dev

在discord.py中发送循环消息

来自分类Dev

Discord.JS发送消息给被标记的用户

来自分类Dev

Discord.js -- 让 PresenceUpdate 发送消息

来自分类Dev

使JDA Discord Bot延迟写入消息

来自分类Dev

Python Discord bot删除用户消息

来自分类Dev

Discord.py bot 返回多条消息

来自分类Dev

Discord bot在特定频道中发送味精

来自分类Dev

Python Discord bot异常发送文本

来自分类Dev

通过 Discord bot (Python) 发送文件

来自分类Dev

Discord bot不会将我的消息发送到服务器,而是在终端中打印

来自分类Dev

发送 webhook 消息以通过 discord py 克隆用户消息

来自分类Dev

在发送前缀discord.py时发送消息

来自分类Dev

创建将消息作为嵌入消息移动的Discord Bot方法

来自分类Dev

Discord Bot 应删除“用户固定消息到频道”消息

Related 相关文章

  1. 1

    Discord Bot从udp连接向特定通道发送消息

  2. 2

    在for循环中发送多个消息Discord python bot

  3. 3

    尽管异步/等待,Discord bot仍以错误的顺序发送消息

  4. 4

    如何使用Discord Bot发送问候消息-NodeJS

  5. 5

    NodeJS discord bot命令获取错误无法发送空消息

  6. 6

    如何让我的 Discord Bot 在启动后立即发送消息?

  7. 7

    JS discord bot 出现错误:无法发送空消息

  8. 8

    Python - Discord Bot Loop 命令并在私人消息中发送消息

  9. 9

    Discord bot:发送消息并等待发送另一个

  10. 10

    当成员不在JSON文件中时,Discord.py bot无法发送消息

  11. 11

    Discord.py如何使用BOT获取已发送消息的ID?

  12. 12

    Python Discord Bot如何在一条消息中发送多行?

  13. 13

    如何使我的Discord Bot向服务器发送更干净的消息

  14. 14

    使用discord bot发送高优先级和低优先级消息

  15. 15

    Discord.py:发送长消息

  16. 16

    在discord.py中发送循环消息

  17. 17

    Discord.JS发送消息给被标记的用户

  18. 18

    Discord.js -- 让 PresenceUpdate 发送消息

  19. 19

    使JDA Discord Bot延迟写入消息

  20. 20

    Python Discord bot删除用户消息

  21. 21

    Discord.py bot 返回多条消息

  22. 22

    Discord bot在特定频道中发送味精

  23. 23

    Python Discord bot异常发送文本

  24. 24

    通过 Discord bot (Python) 发送文件

  25. 25

    Discord bot不会将我的消息发送到服务器,而是在终端中打印

  26. 26

    发送 webhook 消息以通过 discord py 克隆用户消息

  27. 27

    在发送前缀discord.py时发送消息

  28. 28

    创建将消息作为嵌入消息移动的Discord Bot方法

  29. 29

    Discord Bot 应删除“用户固定消息到频道”消息

热门标签

归档