45 lines
1.6 KiB
JavaScript
45 lines
1.6 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, config } = require('..');
|
||
|
|
||
|
client.on("packet", async packet => {
|
||
|
try {
|
||
|
if (packet.type !== 'UserRelationship') return;
|
||
|
if (packet.status === 'Incoming') {
|
||
|
// Incoming friend request
|
||
|
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 {
|
||
|
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);
|
||
|
}
|
||
|
});
|