简体   繁体   中英

Discord.js manage roles with only id

I am trying to code a discord.js bot. Right now i am working on a tempmute system but can't seem to find out a way of removing a role from a user with only their id to work with! I have searched around the interwebs but i couldn't seem to find anything that helped me.
ex. Discord.js docs and An idiots guide

My code:

exports.run = (client, message, args, config) => { if(!message.member.roles.some(r=>[config.BotMaster].includes(r.name))) return message.reply("Du behöver en högre rank!");
const mysql = require('mysql');
const con = mysql.createConnection({
    host: config.host,
    user: config.user,
    password: config.password,
    database: config.database
});

    let role = message.guild.roles.find("name", config.Muted);
    let member = message.mentions.members.first();
    let sql = "SELECT * FROM TempMute WHERE Time<=" + Date.now() + ' AND Unmuted!="True"'
    con.query(sql, function (err, rows) {
        if (err) return console.log(err);
        for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            var sql2 = "UPDATE TempMute SET Unmuted = 'True' WHERE Id = " + row.Id + ";";
            con.query(sql2, function (err, result) {
                if (err) return console.log(err);
                var Id = row.Name
                var unmute = client.fetchUser(Id)
                unmute.removeRole(role).catch(console.error);
            });
        };
    });
};

Error:

/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/Parser.js:80
    throw err; // Rethrow non-MySQL errors
    ^

TypeError: unmute.removeRole is not a function
at Query._callback (/home/pi/Documents/PinkBot/commands/rm.js:22:24)
at Query.Sequence.end (/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24)
at Query._handleFinalResultPacket (/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/sequences/Query.js:139:8)
at Query.OkPacket (/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/sequences/Query.js:72:10)
at Protocol._parsePacket (/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/pi/Documents/PinkBot/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/pi/Documents/PinkBot/node_modules/mysql/lib/Connection.js:103:28)
at Socket.emit (events.js:160:13)
at addChunk (_stream_readable.js:269:12)

client.fetchUser(id) returns a promise, and from that promise you'll get a user. You can't remove roles from users. You need a GuildMember to remove a role.
Instead of: var unmute = client.fetchUser(Id) , you'll need something like this:

message.guild.fetchMember(Id).then(guildMember => {
    guildMember.removeRole(role).catch(console.error);
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM