Using Discord.js (V12)
I am currently trying to create a script that makes it so when someone says "what" the bot responds with "You are stuck with it!" then gives the user a role. But I want it so when the next person says what, it removes that role from the previous wielder. I currently have it working where it can add the role but not remove it from the previous person.
Here is my current working code:
module.exports = {
name: 'what',
description: 'Responds with You are stuck with it and adds role!',
execute(message, args) {
message.channel.send('You are stuck with it!');
message.member.roles.add('886996311117795429')
}
}
The problem of declaring variable inside your code like @Cannicide suggested is that if the bot goes down and you need to boot it up again, that variable will be lost.
The easiest way of having the data saved even after restarts is by using quick.db
//your other stuff if you have any
const db = require('quick.db')
module.exports = {
name: 'what',
description: 'Responds with You are stuck with it and adds role!',
execute(message, args) {
message.channel.send('You are stuck with it!');
message.member.roles.add('886996311117795429')
if(db.get('previousRoleUser')){
let prevUser = message.guild.members.cache.find(m => m.id === db.get('previousRoleUser'));
if(prevUser){
prevUser.roles.remove('886996311117795429')
}
db.set('previousRoleUser', message.author.id)
}
}
The above code is just an example, that is not tested. But I think it would work some how like that. Just set the value of the previous user to quick.db and then fetch the info from there when you want to use it.