更新 Typescript 中导入的模块

问题描述 投票:0回答:1

抱歉,我对这门语言有点陌生。 这些天我正在创建一个自定义的不和谐机器人,但我陷入了这个问题...... 我让这个机器人能够从每个命令一个模块的文件夹中动态加载命令,但现在我试图创建一个命令来重新加载所有命令,但每次重新加载命令后,输出总是相同的。

这是代码:

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 文件更新命令,因为我必须再次编译它。 我尝试发出简单的“乒!乒!”声。 like命令,然后将其编辑为“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编译版本),旧版本就会继续运行

node.js typescript import discord.js node-modules
1个回答
0
投票

我设法找到了问题的解决方案。 由于 Node 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));

        });
    });
}

代码可能看起来像垃圾,但它实际上有效......我正在努力使其变得更好,但同时我可以依赖它。 任何建议都会被接受

© www.soinside.com 2019 - 2024. All rights reserved.