-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (71 loc) · 4 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { Client, GatewayIntentBits, Partials, PermissionsBitField } = require('discord.js');
const cron = require('node-cron');
const client = new Client(
{
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMembers],
//partials needed to detect users even if they're appearing offline
partials: [Partials.User, Partials.GuildMember]
});
const { TOKEN, GUILDID, USERID } = require('./config.json');
let last_avi;
client.on('userUpdate', (old_user, new_user) => {
if (old_user.id !== USERID)
return;
if (old_user.avatar !== new_user.avatar) //user changed their avatar
client.guilds.fetch(GUILDID).then(guild =>
guild.members.fetch(USERID).then(user => {
if (user.avatarURL() == null) //if user has a guild specific avatar, we don't care about a normal avatar change
change_avi(new_user.displayAvatarURL()).catch(error => { console.log(error); last_avi = null });
}).catch(console.error)
).catch(console.error);
else if (old_user.globalName !== new_user.globalName) //user changed their username
{
client.guilds.fetch(GUILDID).then(guild =>
guild.members.fetch(USERID).then(user => {
if (user.nickname == null) //if the user has a nickname, we don't care about a username change
guild.members.me.setNickname(new_user.globalName).catch(console.error);
}).catch(console.error)
).catch(console.error);
}
});
client.on('guildMemberUpdate', async (old_user, new_user) => {
if (old_user.guild.id !== GUILDID || (new_user.id !== USERID && new_user.id !== client.user.id))
return;
if (new_user.id === USERID && old_user.nickname !== new_user.nickname) //user changed their nickname
new_user.guild.members.me.setNickname(new_user.nickname ?? new_user.user.globalName).catch(console.error);
else if (new_user.id === USERID && old_user.avatar !== new_user.avatar) //user changed their guild specific avatar
change_avi(new_user.avatarURL() ?? new_user.displayAvatarURL()).catch(error => { console.log(error); last_avi = null });
else if (new_user.id === client.user.id && old_user.nickname !== new_user.nickname) //the bot's nickname was changed by someone
new_user.guild.members.fetch(USERID).then(user => new_user.guild.members.me.setNickname(user.nickname ?? user.user.globalName).catch(console.error));
});
let change_avi = (avi_url) => client.user.setAvatar((last_avi = avi_url).replace(/(?:.(?!\.))+$/, '.png?size=4096'));
client.login(TOKEN).then(() =>
{
console.log('Connected as ' + client.user.tag);
client.guilds.fetch(GUILDID).then(guild => {
guild.members.fetch(USERID).then(user => {
if (!guild.members.me.permissions.has(PermissionsBitField.Flags.ChangeNickname))
{
console.log('No permissions to change nickname, exiting');
process.exit();
}
change_avi(user.avatarURL() ?? user.displayAvatarURL()).catch(error => { console.log(error); last_avi = null });
guild.members.me.setNickname(user.nickname ?? user.user.globalName).catch(console.error);
}).catch(console.error);
}).catch(console.error);
}).catch(console.error);
//check every hour that we haven't somehow missed an avatar/name change
//(maybe we got e.g. disconnected and the user made a change during that time)
cron.schedule('0 * * * *', () => {
if (!client.isReady())
return;
client.guilds.fetch(GUILDID).then(guild => {
guild.members.fetch(USERID).then(user => {
let avi_url = user.avatarURL() ?? user.displayAvatarURL();
if (last_avi !== avi_url)
change_avi(avi_url).catch(error => { console.log(error); last_avi = null });
if (guild.members.me.nickname !== user.nickname ?? user.user.globalName)
guild.members.me.setNickname(user.nickname ?? user.user.globalName).catch(console.error);
}).catch(console.error);
}).catch(console.error);
});