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
334 views
in Technique[技术] by (71.8m points)

javascript - Discord Bot - Can I use guildMember to create a 'personal' variable?

I'm trying to make a level bot in Discord. Basically, what I want to do is to assign a "pts" variable to every person in the server. I have an event handler and command handler in my bot.

Here is my event_handler.js:

const fs = require('fs');

module.exports = (client, Discord) =>{
    const load_dir = (dirs) =>{
        const event_files = fs.readdirSync(`./events/${dirs}`).filter(file => file.endsWith('.js'));
        
        for (const file of event_files) {
            const event = require(`../events/${dirs}/${file}`);
            const event_name = file.split('.')[0];
            client.on(event_name, event.bind(null, Discord, client));
        }
    }

    ['client', 'guild'].forEach(e => load_dir(e));
}

And here is my command_handler.js:

const fs = require('fs');

module.exports = (client, Discord) =>{
    const command_files = fs.readdirSync('./commands/').filter(file => file.endsWith('.js'))

    for(const file of command_files){
        const command = require(`../commands/${file}`);
        if(command.name){
            client.commands.set(command.name, command);
        } else {
            continue;
        }
    }
}

I also have a guildMemberAdd event, which has a guildMember variable, adds a Member role to the guildMember, and say a welcome message. Here it is (it's called guildMemberAdd.js):

module.exports = (Discord, client, guildMember) =>{
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'Member');

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get(MYCHANNELID).send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
}

I was thinking maybe when somebody joins the server I can give them a variable. For example:

module.exports = (Discord, client, guildMember) =>{
    var pts = new guildMember;
    let welcomeRole = guildMember.guild.roles.cache.find(role => role.name === 'Member');

    guildMember.roles.add(welcomeRole);
    guildMember.guild.channels.cache.get(MYCHANNELID).send(`Welcome, <@${guildMember.user.id}>, to our server! ... Check out the rules-cmd channel!`);
}

...but I'm really bad with javaScript and that, I know, will DEFINITELY not work.

Is there a way to do this? If there, is, how do I change it based on a command? (My commands are stored in another folder.)

If you guys need it, here is my ... um ... ordering of files and folders. I'm not really sure what that's called.

PHOTO: Ordering of files and folders

HELP IS GREATLY APPRECIATED!!

I don't think you guys need the node module files, right? If you do, please comment.

MY EDIT 1 (BY TYRCNEX): Some of you viewing this question might be saying, "Well there should be other Stack Overflow questions like this, right?" Yes, there is. However, I am a COMPLETE beginner with JS, as I mentioned above. I could spend a year trying to learn the basics of js...

...but I don't have that much time on my hands. I need help with my SPECIFIC problem, and I can't really understand other answers. This is why I'm asking the question. Thank you.

question from:https://stackoverflow.com/questions/66062947/discord-bot-can-i-use-guildmember-to-create-a-personal-variable

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

1 Reply

0 votes
by (71.8m points)

First of all you need some type of storage where you can save the lvl's and id's of GuildMembers, I recommend you some of that:

Easy to use: QuickDb, Json DB

The problem with that db's is that they have poor scalability and more they weight more time the queries take to execute.

For your current level that databases will give you the understanding of how it all works, when you have the understanding of how databases work you should look forward for some db's like:Mongo, postgres, etc.

Maybe you don't even need database and you want to save it as object in app's cache I will give you a quick example of how it's done:

var members = {}
//You should know that for level's you shouldn't save all the members on guildmembersJoin 
//Some the members won't last long on the server and even maybe leave right after and you will have 
//a useless row in you db. 
//Your message event
  module.exports = (Discord, client, message) =>{
    members = {
      ...members,
      [message.member.id] : {
        level: members[message.member.id] ? members[message.member.id].level : 0,
        exp: members[message.member.id] ? members[message.member.id].exp : 0 + expformessage
      }
    }
}

Example with quick.db:

const db = require('quick.db');
  module.exports = (Discord, client, message) =>{
    if(!db.get(message.member.id))
      db.set(message.member.id, {'level': 0, 'exp': expformessage})
    else
      db.set(`${message.member.id}.exp`, db.get(`${message.member.id}.exp`) + expformessage )
}

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

...