Создание файла .env и модифицирующих кодов на discord.js

#node.js #discord.js #dotenv

#node.js #discord.js #dotenv

Вопрос:

Кто-нибудь поможет мне создать файл .env и изменить коды! это discord.js v12 я не могу изменить коды .env в этих кодах! но это не сработало, пожалуйста, помогите мне в этой системе! и это метод установки yarn!

js-файл => RushGamerzClient.js

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

module.exports = class RushGamerzClient extends Client {

    constructor(options = {}) {
        super({
            disableMentions: 'everyone'
        });
        this.validate(options);

        this.once('ready', () => {
            console.log(`Logged in as ${this.user.username}!`);
        });

        this.on('message', async (message) => {
            const mentionRegex = RegExp(`^<@!${this.user.id}>$`);
            const mentionRegexPrefix = RegExp(`^<@!${this.user.id}> `);

            if (!message.guild || message.author.bot) return;

            if (message.content.match(mentionRegex)) message.channel.send(`My prefix for ${message.guild.name} is `${this.prefix}`.`);

            const prefix = message.content.match(mentionRegexPrefix) ?
                message.content.match(mentionRegexPrefix)[0] : this.prefix;

            if (!message.content.startsWith(prefix)) return;

            // eslint-disable-next-line no-unused-vars
            const [cmd, ...args] = message.content.slice(prefix.length).trim().split(/  /g);

            if (cmd.toLowerCase() === 'hello') {
                message.channel.send('Hello!');
            }
        });
    }

    validate(options) {
        if (typeof options !== 'object') throw new TypeError('Options should be a type of Object.');

        if (!options.token) throw new Error('You must pass the token for the client.');
        this.token = options.token;

        if (!options.prefix) throw new Error('You must pass a prefix for the client.');
        if (typeof options.prefix !== 'string') throw new TypeError('Prefix should be a type of String.');
        this.prefix = options.prefix;
    }

    async login(token = this.token) {
        super.login(token);
    }

};
  

js-файл => index.js коды здесь

 const RushGamerzClient = require('./Structures/RushGamerzClient');
const config = require('../config.json');


const client = new RushGamerzClient(config);
client.login();
  

Ответ №1:

У вас есть несколько вариантов решения проблемы:

Используйте файл .env, который имеет DISCORD_TOKEN="<your-token>" затем use dotenv (установить с помощью npm i -G dotenv ) в верхней части вашего файла, и войдите в систему с переменной среды

 require('dotenv').config();

...

client.login(process.env.DISCORD_TOKEN);
  

Комментарии:

1. будет показано, что вы должны передать токен для клиента