Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
96 views
in Technique[技术] by (71.8m points)

javascript - How to use this values in another file's function

Hi so i am making a discord.js command handler package and set this.prefix in the main file when a new class is invoked so how would i use this.prefix in a secondary file's function rather than passing the prefix as an argument.

const { Client } = require('discord.js')
const {load} = require('cmd-handler/load-commands')
const Discord = require('discord.js')
class newhandler {
    constructor(client, commandpath, eventpath, {prefix}){
        this.commandpath = commandpath
        this.prefix = prefix
        this.client = client
        client.cmds = new Discord.Collection()
        client.aliases = new Discord.Collection()
        load(this.commandpath, client)
        client.on('message', (msg) => {
            const {msgev} = require('./events/message')
            msgev(msg)
        })
    }
}
module.exports.newhandler = newhandler

const msgev = async(message) => {
    console.log(this.prefix)
if(!message.content.startsWith(this.prefix)) return
if(!message.guild) return
if(message.author.bot) return
const args = message.content.slice(prefix.length).trim().split(/ + /g)
const cmd = args.shift().toLowerCase()
const command = this.client.cmds.get(cmd) || this.client.aliases.get(cmd)
console.log(command)
if(command) {
    command.run(client, message, args)
}
}
module.exports.msgev = msgev

question from:https://stackoverflow.com/questions/65860198/how-to-use-this-values-in-another-files-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Why not just get an event handler?

Hey, You could just make a event handler that loads events without need to require it, And pass in the prefix as a param


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...