簡體   English   中英

更新 Typescript 中的導入模塊

[英]Update an imported module in Typescript

對不起,我對這門語言有點陌生。 這些天我正在創建一個自定義 discord 機器人,但我陷入了這個問題......我讓這個機器人可以從一個文件夾中動態加載命令,每個命令都有一個模塊,但現在我試圖發出一個命令重新加載它們,但每次重新加載命令后 output 總是相同的。

這是代碼:

refreshCommands = () => {
    this.commands = {};
    console.log("Refreshing commands");
    Promise.all(fs.readdirSync("./dist/commands").map(file => {
        return new Promise(async resolve => {
            const tmp = (await import(`./commands/${file}`)).default;
            this.commands[tmp.name] = tmp;
            resolve(tmp);
        });
    })).then(() => {
        console.log("Listing commands: ");
        console.log(Object.keys(this.commands));
    });
}

當然,我從 js 文件更新命令,而不是從 ts 更新命令,因為我必須再次編譯它。 我試着做一個簡單的“ping, Pong”之類的命令,然后將其編輯為“ping!ping!” 在使用 //reload 命令之前在運行時,但它一直在寫“ping!Pong!”

編輯1:我必須導入的模塊是這樣的:

import command from "../utils/command";
import { Guild, GuildEmoji, GuildEmojiManager, Message, MessageEmbed, Role } from "discord.js";
import { games } from "../utils/games";
import app from "../app";
import ReactionListener from "../utils/reactionListener";

const roleMessage: command = {
    name: "rolesMessage",
    description: "",
    execute: async (message, bot) => {
        message.delete();
        createRoles(message.guild as Guild);
        const embed = new MessageEmbed()
            .setColor('#F00')
            .setTitle("React to set your ROLE!");
    
        games.forEach(game => {
            let emoji = message.guild?.emojis.cache.find(emoji => emoji.name === game.emoji);
            console.log(emoji);
            embed.fields.push({
                name: game.name,
                value: (emoji as GuildEmoji).toString(),
                inline: false
            });
        });

        const msg = await message.channel.send(embed);
        app.reactionListeners.push(new ReactionListener(msg, 
            (reaction, user) => {
                let tmp = games.find(game=> reaction.emoji.name === game.emoji);
                if(tmp){
                    //msg.channel.send(tmp);
                    const role = (message.guild as Guild).roles.cache.find(role => role.name === tmp?.roleName) as Role;
                    message.guild?.members.cache.find(member => member.id === user.id)?.roles.add(role);
                }else{
                    reaction.remove();
                }
            }, (reaction, user)=>{
                let tmp = games.find(game=> reaction.emoji.name === game.emoji);
                if(tmp){
                    //msg.channel.send(tmp);
                    const role = (message.guild as Guild).roles.cache.find(role => role.name === tmp?.roleName) as Role;
                    message.guild?.members.cache.find(member => member.id === user.id)?.roles.remove(role);
                }
            })
        );

        games.forEach(game => {
            msg.react((message.guild?.emojis.cache.find(emoji => emoji.name === game.emoji) as GuildEmoji));
        });
    }
}

const createRoles = (guild: Guild) => {
    games.forEach(game => {
        if(!guild.roles.cache.find(role => role.name === game.roleName)){
            guild.roles.create({
                data: {
                name: game.roleName,
                color: "#9B59B6",
            },
                reason: 'we needed a role for Super Cool People',
            })
            .then(console.log)
            .catch(console.error);
        }
    });
}

export default roleMessage;

這和我之前說的不一樣,但問題是一樣的……一旦我更新並重新加載它(從js編譯的版本),舊版本一直在運行

我設法找到了解決問題的方法。 由於節點 js 會在導入后對每個模塊進行緩存,因此我將其從緩存中刪除,如下所示

refreshCommands = () => {
    Promise.all(fs.readdirSync("./dist/commands").map(file => {
        return new Promise(async resolve => {
            delete require.cache[require.resolve('./commands/' + file)];
            resolve(file);
        });
    })).then(() => {
        this.commands = {};
        console.log("Refreshing commands");
        Promise.all(fs.readdirSync("./dist/commands").map(file => {
            return new Promise(async resolve => {
                const tmp = (await import(`./commands/${file}`)).default;
                this.commands[tmp.name] = tmp;
                resolve(tmp);
            });
        })).then(() => {
            console.log("Listing commands: ");
            console.log(Object.keys(this.commands));

        });
    });
}

代碼可能看起來像垃圾,但它確實有效......我正在努力讓它變得更好,但同時我可以依賴它。 任何建議都被很好地接受

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM