我怎样才能让这个功能重新启动?

时间:2021-06-11 09:54:20

标签: node.js json discord.js

我有一个使用 Discord.JS 用 Node.JS 编写的 Discord BOT 我有一个在机器人上线时启动的功能

它包括从 API 获取数组:https://jacob.rede-epic.com/api/upcomingEvents 使用时间戳,它应该在消息发生前 10、5 和 1 分钟、发生时(确切小时 + 15 分钟,例如:08:15/09:15/10:15 等)以及何时发送消息结束(20 分钟后,例如:08:35/09:35/10:35 等)

我怎样才能重置整个系统? 代码:

const { MessageEmbed } = require('discord.js');
const axios = require('axios');

class Jacob {
    constructor(client) {
        this.client = client;
    }

    async start() {
        const jacobContestsChannel = this.client.channels.cache.get('852596632591269968');
        let nextEventObject = null;
        let sentNotification = false;
        let sentNotification2 = false;
        let sentNotification3 = false;
        let sentNotificationActive = false;
        let sentNotificationFinished = false;

        const cropNames = [
            'Cactus',
            'Carrot',
            'Cocoa beans',
            'Melon',
            'Mushroom',
            'Nether wart',
            'Potato',
            'Pumpkin',
            'Sugar cane',
            'Wheat',
        ];

        setInterval(async () => {
            let upcomingEvents = [];
            let response = await axios.get('https://jacob.rede-epic.com/api/upcomingEvents');
            let request = response.data;

            for (let i in request) {
                upcomingEvents.push(request[i]);
            }

            if (nextEventObject == null) {
                nextEventObject = upcomingEvents[0];
            } else {
                let diff = nextEventObject.timestamp - Date.now();
                let active = diff < 0;

                setInterval(() => {
                    try {
                        diff = nextEventObject.timestamp - Date.now();
                    } catch {
                        nextEventObject = null;
                    }
                }, 1000);

                if (diff < -20 * 60 * 1000) {
                    sentNotification = false;
                    sentNotification2 = false;
                    sentNotification3 = false;
                    sentNotificationActive = false;
                    nextEventObject == null;

                    if (!sentNotificationFinished) {
                        jacobContestsChannel.send(new MessageEmbed()
                            .setColor(this.client.embedColor)
                            .setTitle('The current contest has ended!')
                        );
                        sentNotificationFinished = true;
                    }

                } else if (!active && diff < 10 * 60 * 1000 && !sentNotification) {
                    jacobContestsChannel.send(new MessageEmbed()
                        .setColor(this.client.embedColor)
                        .setTitle('A contest is starting in 10 minutes')
                        .setDescription(`Crops: ${nextEventObject.crops.map(crop => cropNames[crop]).join(', ')}`)
                    );
                    sentNotification = true;
                } else if (!active && diff < 5 * 60 * 1000 && sentNotification && !sentNotification2) {
                    jacobContestsChannel.send(new MessageEmbed()
                        .setColor(this.client.embedColor)
                        .setTitle('A contest is starting in 5 minutes')
                        .setDescription(`Crops: ${nextEventObject.crops.map(crop => cropNames[crop]).join(', ')}`)
                    );
                    sentNotification2 = true;
                } else if (!active && diff < 60 * 1000 && sentNotification && sentNotification2 && !sentNotification3) {
                    jacobContestsChannel.send(new MessageEmbed()
                        .setColor(this.client.embedColor)
                        .setTitle('A contest is starting in 1 minute')
                        .setDescription(`Crops: ${nextEventObject.crops.map(crop => cropNames[crop]).join(', ')}`)
                    );
                    sentNotification3 = true;
                } else if (active && !sentNotificationActive) {
                    jacobContestsChannel.send(new MessageEmbed()
                        .setColor(this.client.embedColor)
                        .setTitle('A contest has started!')
                        .setDescription(`Crops: ${nextEventObject.crops.map(crop => cropNames[crop]).join(', ')}`)
                    );
                    sentNotificationActive = true;
                }

                console.clear();
                console.log(nextEventObject);
                console.log(`Diff: ${diff}`);
                console.log(`Active: ${active}`);
                console.log(`Notification: ${sentNotification}`);
                console.log(`Notification Active: ${sentNotificationActive}`);
                console.log(`Notification Finished: ${sentNotificationFinished}`);
            }
        }, 1000);
    }

    async requestUpcomingEvents() {
        let response = await axios.get('https://jacob.rede-epic.com/api/upcomingEvents');
        return response.data;
    }
}


module.exports = Jacob;

0 个答案:

没有答案