0
votes

Okay so I was wondering if anyone knew how I'd go about having a bot select a channel. I'm creating a multi-server bot for discord, and was wondering how I'd go about with selecting a role/channel with a command. For example, I have this code that auto roles members:

client.on("guildMemberAdd", member => {

    //Change "RoleName" tto the role yoou want to give them
    var role = member.guild.roles.find ("name", "RoleName")
    member.addRole (role)
        if (role = null)
        return;
})

Now, obviously not every server is going to have their member role as, "RoleName," so what I was wondering is, how would I make the bot choose the role they wish to be given to new members with a command. So if they were to do: "cb!autorole set Member" it would then make that role be what they automatically give new members, but if someone in a different server did "cb!autorole set Newbie" it would make it that servers auto role, withoout changing or adding a different servers autorole.

And same with channels. Example: "cb!welcome set #welcome" it'd set that servers welcome message channel, without changing or adding a different servers selected channel.

I'd also like to know how to unset them as well. Example: "cb!unset welcome" and "cb!unset autorole" and of course it'd only unset that server's stuff.

If it's too long to explain but you know a link, please do share it with me.

I'm sorry if this is confusing, if you have any questions on what I mean just comment. I'm new to coding and such so I probably got some of the terminology wrong.

Thank you for taking the time to read this.

1

1 Answers

0
votes

You should use a database to set the channel (for example the channel id / role id) and then you can do:

var chan = member.guild.channels.get(channelid).catch(console.error);
if(chan) chan.send(`welcome ${member} to the server`);

var role = member.guild.roles.get(roleid).catch(console.error);
if(role) member.addRole(role);

If you're not really experienced with databases I suggest quick.db. Then you can do

const db = require("quick.db");
client.on("message", message => {
    const args = message.content.slice(prefix.length).trim().split(/ +/g);
    const command = args.shift().toLowerCase();
    if(command == "setautorole"){
        let roleName = args.slice(0).join(" ");
        let role = message.guild.roles.find(role => role.name == roleName).catch(message.reply("Couldn't find that role")
        db.set(`autorole_${message.guild.id}`, role.id)
    }
    if(command == "unsetautorole"){
        db.delete(`autorole_${message.guild.id})
    }
    if(command == "setwelcomechannel"){
        let channelName = args.slice(0).join(" ");
        let channel = message.guild.channels.find(channel => channel.name == channelName).catch(message.reply("Couldn't find that channel")
        db.set(`welcomechannel_${message.guild.id}`, channel.id)
    }
    if(command == "unsetwelcomechannel"){
        db.delete(`welcomechannel_${message.guild.id})
    }
})
client.on("guildMemberAdd", member => {
    let roleId = db.get(`autorole_${member.guild.id}`);
    if(roleId) member.addRole(roleId).catch(console.error);
    let channelId = db.get(`welcomechannel_${member.guild.id}`);
    if(channelId) channel = member.guild.channels.get(channelId).catch(console.error);
    if(channel) channel.send("x")
})

setautorole setwelcomechannel

Of course, you can modify this to use another way to save it.