Discord 机器人发送生日消息 [英] Discord bot send birthday message

查看:41
本文介绍了Discord 机器人发送生日消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个 Discord 机器人,我希望它能够检查当前日期,如果当前日期是任何用户的生日,那么它会标记他们并说生日快乐.我试过使用 Node.js 的日期,比如

I have made a Discord bot and I want it to be able to check the current date, and if current date is any of the users birthday, then it'll tag them and say happy birthday. I have tried using Node.js' Date, like

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

但这似乎不起作用.所以我需要帮助的是:

but that didn't seem to work. So what I need help with is:

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

我可能只是硬编码所有的生日,因为我们不是很多(我知道这不是一个好的做法,但我只是希望它现在可以工作,我可以稍后再完善它)

I'll probably just hardcode all the birthdays in since we're not that many (I know that's not good practice, but I just want it working for now, I can polish it later)

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

If anyone could help me with that, that'd be great.

这就是我的 index.js 文件的样子(如果相关的话):

This is what my index.js file looks like (if that is relevant):

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')

我意识到我写了if(date)"client.on('message',message =>{})"中的语句,因此除非您在该特定时间编写命令,否则显然不会检查.我会使用什么功能?

I realise I wrote the "if(date)" statement in "client.on('message',message =>{})", so that obviously wouldn't check unless you wrote a command at that specific time. What function would I use?

推荐答案

您可以查看 Discord.js 事件的完整列表 这里.
Discord.js 中没有基于预定时间触发的内置事件.

You can check the full list of Discord.js events here.
There are no built in events in Discord.js that are fired based on scheduled time.

为了做到这一点,您可以构建一个时间系统,每天根据超时或间隔触发事件.或者...您可以使用一个库,例如 node-cron 来执行此操作你.

In order to do that, you could build a time system that fires events every single day based on a timeout out or interval. Or... you could use a library, like node-cron that do this for you.

我将尝试举例说明一种方法,您可以使用刚才所说的 node-cron 来做到这一点.

I'll try to exemplify a way you could do this using the just said node-cron.

首先,您必须需要 node-cron 包,因此您可以像使用 discord.js 本身一样使用 npm.所以粘贴并在你的终端上执行这个命令:

First, you'll have to require the node-cron package, thus you can use npm just as you did with discord.js itself. So paste and execute this command on your terminal:

npm install node-cron

很遗憾,Discord.js 没有为您提供获取用户生日日期的方法.因此,对生日进行硬编码(正如您所说的那样),并且对于本示例,您将发送祝贺消息的通用频道 ID,您可以这样:

Unfortunately, Discord.js doesn't offer a way for you to get the birthday date of the user. So, hard coding the birthdays (as you stated that it's okay) and also, for this example, the general channel Id that you'll send the message of the congratulations, you could something like this:

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');

这不是优化的,只是为了让您大致了解如何做到这一点.请注意,在设置任务之前应该先实例化客户端,所以我建议您像这个示例一样执行此操作并将计划设置为就绪.

This is not optimized an intends to just give a general idea of how you can do it. Notice though that the client should be instantiated before you set the tasks, so I'd recommend doing it like this example and setting the schedule on ready.

无论如何,您始终可以使用已经创建和测试过的机器人,例如 生日机器人.

Anyway, you could always use an already created and tested bot such as Birthday Bot.

这篇关于Discord 机器人发送生日消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆