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

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

问题描述

我制作了一个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事件的完整列表

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.

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

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

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

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