I have implemented a login system which prompts the user for a password via DMs.
The user is in the server with my bot and when he tries to execute the command in order for the bot to DM him with the prompt, the bot 'cannot send message to this user', as I was told by the Unhandled Error Rejection (I will be handling them soon, don't worry).
We have managed to bypass the issue by getting the user and the bot into a seperate server where it is evident that the bot is sharing a server with the user and when he went to execute the command in the original server, the bot DM'ed the user. The DM's send for every other user, but him which I find strange.
await postgres.query(`SELECT * FROM login_details WHERE username = '${username}'`, async (err, res) => {
if (err) return message.channel.send("Error connecting to the Database. Contact JusSupra#6561 for help if this error continues to arise.")
if (res.rows.length === 0) return message.channel.send(`There is no entry in the database with the username: ${username}`)
message.channel.send("Go to DM's to enter your password!")
const filter = m => m.content
const channel = await message.author.send(`Enter password for ${username}:`)
const collector = channel.channel.createMessageCollector(filter, { max: 1 });
collector.on('collect', m => {
});
collector.on('end', async collected => {
let password = await bcrypt.compare(collected.first().content, res.rows[0].password).then((result) => {
return result
}).catch(err => {
message.author.send("Error when reading password. Please contact Supra immediately.")
return console.log(err)
})
if (password) {
message.author.send(`Sucessfully logged in as ${username}!`)
nicknameFile[message.author.id] = {
username: username,
airport: airport,
position: pos,
timeStart: new Date().toISOString(),
timer: 0,
originNick: message.member.nickname || message.author.username
}
module.exports.logDetails = nicknameFile[message.author.id]
if (member.manageable) {
member.setNickname(`${nicknameFile[message.author.id].AirportServed} ${nicknameFile[message.author.id].PositionServed} - ${nicknameFile[message.author.id].originNick}`)
}
postgres.query(`INSERT INTO login_logs(staff_user_id, username, airport_served, position_served, time_start, status) VALUES (${message.author.id}, '${nicknameFile[message.author.id].username}', '${nicknameFile[message.author.id].airport}', '${nicknameFile[message.author.id].position}', '${nicknameFile[message.author.id].timeStart}', 'ON_DUTY');`, (e, r) => {
console.log(e)
if (e) return message.author.send("ERROR: Error while entering data into Database. If this continues, contact JusSupra#6561.")
})
const jsonString = JSON.stringify(nicknameFile, null, 2)
fs.writeFileSync('./cmds/JSONS/nickname.json', jsonString, err => {
if (err) {
console.log('Error writing to nickname.json', err)
}
})
The snippet of the code concerning the DM part. If you need more code, please feel free to contact me.
If I had to guess, I think that this is something to do with bot cache and if so, would I be able to fix this by myself?
Further details:
Priority this issue should have: Low priority, we have found a temporary work around so no hurry.
[ ] I have also tested the issue on latest master, commit hash:
Please handle your promise rejection and look at the error it returns, it includes the API path the request failed on, which can give you a better idea about what exactly is happening.
Generally speaking this error is present if
1) The bot and the user do not share a server at the point of execution
2) The user has blocked direct messages globally
3) The user has blocked direct messages for this guild specifically
4) The user has blocked the bot
5) The bot is trying to DM a bot (including itself)
However this seems to require some more debugging, as such please join our discord server:
https://discord.gg/bRCvFy9
I am not yet convinced this is a bug on our end and should be discussed on the issue tracker
I'll see what I can do on my part for debugging. Thanks for the quick response. Should I close this issue or keep it open until further notice?
This is a sidenote but your code is open to SQL injection attacks, specifically where you insert the user's username directly into the sql query (WHERE username = '${username}') and VALUES('${nicknameFile[message.author.id].username}'). Use parameterized queries instead, where you replace these values with "$1", "$2", etc. and have the real values in a separate variable.
This is a sidenote but your code is open to SQL injection attacks, specifically where you insert the user's username directly into the sql query
(WHERE username = '${username}')andVALUES('${nicknameFile[message.author.id].username}'). Use parameterized queries instead, where you replace these values with "$1", "$2", etc. and have the real values in a separate variable.
Thanks for the advice, I was looking for things like that aswell and it's good to see that someone caught it out.
Most helpful comment
This is a sidenote but your code is open to SQL injection attacks, specifically where you insert the user's username directly into the sql query
(WHERE username = '${username}')andVALUES('${nicknameFile[message.author.id].username}'). Use parameterized queries instead, where you replace these values with "$1", "$2", etc. and have the real values in a separate variable.