61 lines
2.2 KiB
JavaScript
61 lines
2.2 KiB
JavaScript
/**
|
|
* Automatically accepts friend requests and
|
|
* sends requests to users messaging the bot.
|
|
* Required for users to add it to groups.
|
|
*/
|
|
|
|
const { client, logger } = require('..');
|
|
const isBlocked = require('../util/is_blocked');
|
|
|
|
client.on("packet", async packet => {
|
|
try {
|
|
if (packet.type !== 'UserRelationship') return;
|
|
if (packet.status === 'Incoming') {
|
|
// Incoming friend request
|
|
|
|
// 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;
|
|
}
|
|
|
|
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 {
|
|
if (isBlocked(relationship._id)) {
|
|
logger.info(`Rejecting friend request from blocked user ${relationship._id}`);
|
|
client.users.removeFriend(relationship._id)
|
|
.catch(console.error);
|
|
return;
|
|
}
|
|
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);
|
|
}
|
|
}); |