revolt-bot/util/friendships.js

61 lines
2.2 KiB
JavaScript
Raw Normal View History

2021-03-06 14:39:00 +00:00
/**
2021-03-01 23:10:35 +00:00
* Automatically accepts friend requests and
* sends requests to users messaging the bot.
* Required for users to add it to groups.
*/
2021-03-03 15:07:46 +00:00
const { client, logger } = require('..');
2021-03-06 14:39:00 +00:00
const isBlocked = require('../util/is_blocked');
2021-03-01 23:10:35 +00:00
client.on("packet", async packet => {
try {
if (packet.type !== 'UserRelationship') return;
if (packet.status === 'Incoming') {
// Incoming friend request
2021-03-06 14:39:00 +00:00
// Ignore blocked
if (isBlocked(packet.user)) {
logger.info(`Rejecting friend request from blocked user ${packet.user}`);
client.users.removeFriend(packet.user)
.catch(console.error);
return;
}
2021-03-01 23:10:35 +00:00
const user = await client.users.fetch(packet.user)
.catch(e => {
logger.error(`Failed to fetch author of friend request:\n${e}`);
});
logger.info(`Incoming friend request from ${user.username} (${user._id})`);
client.users.addFriend(user.username)
.catch(e => {
logger.error(`Failed to accept friend request:\n${e}`);
client.channels.sendMessage(packet.user, `Sorry, due to an error I was unable to process your friend request.`)
.catch(() => {
});
})
.then(() => logger.done(`Friend added`));
}
} catch(e) {
console.error(e);
}
});
// Scan friend list for new friend requests created during downtime
client.user.relations
.filter(r => r.status === "Incoming")
.forEach(async relationship => {
try {
2021-03-06 14:39:00 +00:00
if (isBlocked(relationship._id)) {
logger.info(`Rejecting friend request from blocked user ${relationship._id}`);
client.users.removeFriend(relationship._id)
.catch(console.error);
return;
}
2021-03-01 23:10:35 +00:00
const user = await client.users.fetch(relationship._id);
await client.users.addFriend(user.username);
logger.info(`Accepted pending friend request from ${user.username}`);
} catch(e) {
console.error(e);
}
});