work-around d.js bug
This commit is contained in:
parent
61c9df599b
commit
2b8f616efe
|
@ -1,82 +1,82 @@
|
||||||
global.colorRoles = new Object();
|
global.colorRoles = new Object();
|
||||||
|
|
||||||
colorRoles.findColorRole = function (member) { // get color role of member
|
colorRoles.findColorRole = function (member) { // get color role of member
|
||||||
return member.roles.find(role => role.name.startsWith('['));
|
return member.roles.find(role => role.name.startsWith('['));
|
||||||
};
|
};
|
||||||
|
|
||||||
colorRoles.update = async function (member) { // create or remove member's color role based on presence
|
colorRoles.update = async function (member) { // create or remove member's color role based on presence
|
||||||
if (member.guild.id != config.guildID) return;
|
if (member.guild.id != config.guildID) return;
|
||||||
let existingColorRole = colorRoles.findColorRole(member);
|
let existingColorRole = colorRoles.findColorRole(member);
|
||||||
if (member.presence.status == "offline") { // they must not have the role
|
if (member.presence.status == "offline") { // they must not have the role
|
||||||
if (!existingColorRole) return; // ok, they already don't have the role
|
if (!existingColorRole) return; // ok, they already don't have the role
|
||||||
// save and delete their color role
|
// save and delete their color role
|
||||||
let role = existingColorRole;
|
let role = existingColorRole;
|
||||||
let dbrole = {
|
let dbrole = {
|
||||||
id: role.id,
|
id: role.id,
|
||||||
name: role.name,
|
name: role.name,
|
||||||
color: role.color,
|
color: role.color,
|
||||||
hoist: role.hoist,
|
hoist: role.hoist,
|
||||||
position: role.position,
|
position: role.position,
|
||||||
permissions: role.permissions.bitfield,
|
permissions: role.permissions.bitfield,
|
||||||
mentionable: role.mentionable
|
mentionable: role.mentionable
|
||||||
};
|
};
|
||||||
let id = member.id;
|
let id = member.id;
|
||||||
// upsert member's color_role json
|
// upsert member's color_role json
|
||||||
let res = await dbClient.query(`SELECT * FROM member_data WHERE id = $1`, [id]);
|
let res = await dbClient.query(`SELECT * FROM member_data WHERE id = $1`, [id]);
|
||||||
if (res.rows[0]) {
|
if (res.rows[0]) {
|
||||||
await dbClient.query(`UPDATE member_data SET color_role = $2 WHERE id = $1`, [id, dbrole]);
|
await dbClient.query(`UPDATE member_data SET color_role = $2 WHERE id = $1`, [id, dbrole]);
|
||||||
} else {
|
} else {
|
||||||
await dbClient.query(`INSERT INTO member_data (id, color_role) VALUES ($1, $2)`, [id, dbrole]);
|
await dbClient.query(`INSERT INTO member_data (id, color_role) VALUES ($1, $2)`, [id, dbrole]);
|
||||||
}
|
}
|
||||||
await role.delete();
|
await role.delete();
|
||||||
} else { // they must have their color role
|
} else { // they must have their color role
|
||||||
if (existingColorRole) return; // ok, they already have the role
|
if (existingColorRole) return; // ok, they already have the role
|
||||||
// give them their color role
|
// give them their color role
|
||||||
// check if database has their role
|
// check if database has their role
|
||||||
let member_data = (await dbClient.query(`SELECT (color_role) FROM member_data WHERE id = $1`, [member.id])).rows[0];
|
let member_data = (await dbClient.query(`SELECT (color_role) FROM member_data WHERE id = $1`, [member.id])).rows[0];
|
||||||
if (member_data && member_data.color_role) { // it does, reinstantiate it
|
if (member_data && member_data.color_role) { // it does, reinstantiate it
|
||||||
let dbrole = member_data.color_role;
|
let dbrole = member_data.color_role;
|
||||||
let role = member.guild.roles.get(dbrole.id); // get existing role if it still exists somehow
|
let role = member.guild.roles.get(dbrole.id); // get existing role if it still exists somehow
|
||||||
if (!role) role = await member.guild.roles.create({data:{ // otherwise recreate it
|
if (!role) role = await member.guild.roles.create({data:{ // otherwise recreate it
|
||||||
name: dbrole.name,
|
name: dbrole.name,
|
||||||
color: dbrole.color,
|
color: dbrole.color,
|
||||||
hoist: dbrole.hoist,
|
hoist: dbrole.hoist,
|
||||||
//position: dbrole.position,
|
//position: dbrole.position,
|
||||||
permissions: dbrole.permissions,
|
permissions: dbrole.permissions,
|
||||||
mentionable: dbrole.mentionable
|
mentionable: dbrole.mentionable
|
||||||
}});
|
}});
|
||||||
await member.roles.add(role);
|
await member.roles.add(role);
|
||||||
} else { // it doesn't, create a new one
|
} else { // it doesn't, create a new one
|
||||||
let role = await member.guild.roles.create({data:{
|
let role = await member.guild.roles.create({data:{
|
||||||
name: "[]",
|
name: "[]",
|
||||||
permissions: [],
|
permissions: [],
|
||||||
color: "RANDOM",
|
color: "RANDOM",
|
||||||
}});
|
}});
|
||||||
await member.roles.add(role);
|
await member.roles.add(role);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
colorRoles.updateAll = async function() { // update all members' color roles
|
colorRoles.updateAll = async function() { // update all members' color roles
|
||||||
var guild = dClient.defaultGuild || dClient.guilds.get(config.guildID);
|
var guild = dClient.defaultGuild || dClient.guilds.get(config.guildID);
|
||||||
await guild.members.fetch(); // load all members
|
await guild.members.fetch(); // load all members
|
||||||
for (let member of guild.members) {
|
for (let member of guild.members) {
|
||||||
member = member[1];
|
member = member[1];
|
||||||
try {
|
try {
|
||||||
await colorRoles.update(member);
|
await colorRoles.update(member);
|
||||||
} catch(e) { //TODO debug
|
} catch(e) { //TODO debug
|
||||||
console.error(e.stack);
|
console.error(e.stack);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
colorRoles.pruneOrphanRoles = async function() { // delete all color roles that have no member
|
colorRoles.pruneOrphanRoles = async function() { // delete all color roles that have no member
|
||||||
var guild = dClient.defaultGuild || dClient.guilds.get(config.guildID);
|
var guild = dClient.defaultGuild || dClient.guilds.get(config.guildID);
|
||||||
for (let role of guild.roles) {
|
for (let role of guild.roles) {
|
||||||
role = role[1];
|
role = role[1];
|
||||||
if (role.name.startsWith('[') && role.members.length == 0)
|
if (role.name.startsWith('[') && role.members.length == 0)
|
||||||
await role.delete();
|
await role.delete();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,15 +85,20 @@ colorRoles.pruneOrphanRoles = async function() { // delete all color roles that
|
||||||
|
|
||||||
// event listeners
|
// event listeners
|
||||||
|
|
||||||
dClient.on('presenceUpdate', async (oldMember, newMember) => { // update color role on presence update // emitted also on member join
|
dClient.on('presenceUpdate', async (oldMember, newMember) => { // update color role on presence update // emitted also on member join (iirc)
|
||||||
if (!oldMember || !newMember) return; //TODO debug
|
/* if (!oldMember || !newMember) return;
|
||||||
if (!oldMember.presence || !newMember.presence) return;
|
if (!oldMember.presence || !newMember.presence) return;
|
||||||
if (oldMember.presence.status != newMember.presence.status) {
|
// TODO discord.js bug?
|
||||||
await colorRoles.update(newMember);
|
// oldMember.presence & newMember.presence are undefined
|
||||||
}
|
|
||||||
|
if (oldMember.presence.status != newMember.presence.status) {
|
||||||
|
await colorRoles.update(newMember);
|
||||||
|
}*/
|
||||||
|
await colorRoles.update(newMember.user.presence.status);
|
||||||
});
|
});
|
||||||
|
|
||||||
dClient.on('guildMemberRemove', async member => { // update (delete) color role on member leave
|
dClient.on('guildMemberRemove', async member => { // update (delete) color role on member leave
|
||||||
await colorRoles.update(member);
|
await colorRoles.update(member);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
@ -103,40 +108,40 @@ dClient.on('guildMemberRemove', async member => { // update (delete) color role
|
||||||
// commands
|
// commands
|
||||||
|
|
||||||
commands.color = {
|
commands.color = {
|
||||||
aliases: ["col"],
|
aliases: ["col"],
|
||||||
usage: "<ColorResolvable>",
|
usage: "<ColorResolvable>",
|
||||||
description: "Changes your color",
|
description: "Changes your color",
|
||||||
exec: async function (message) {
|
exec: async function (message) {
|
||||||
var str = message.txt(1);
|
var str = message.txt(1);
|
||||||
if (!str) return false;
|
if (!str) return false;
|
||||||
var role = colorRoles.findColorRole(message.member);
|
var role = colorRoles.findColorRole(message.member);
|
||||||
if (!role) {
|
if (!role) {
|
||||||
if (message.member.presence.status == "offline")
|
if (message.member.presence.status == "offline")
|
||||||
return message.reply([
|
return message.reply([
|
||||||
"You are offline.",
|
"You are offline.",
|
||||||
"I can't change your color when you're invisible."
|
"I can't change your color when you're invisible."
|
||||||
].random());
|
].random());
|
||||||
else {
|
else {
|
||||||
await colorRoles.update(message.member);
|
await colorRoles.update(message.member);
|
||||||
role = colorRoles.findColorRole(message.member);
|
role = colorRoles.findColorRole(message.member);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
role.setColor(str.toUpperCase());
|
role.setColor(str.toUpperCase());
|
||||||
message.react("🆗");
|
message.react("🆗");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
commands.title = {
|
commands.title = {
|
||||||
aliases: ["tit"],
|
aliases: ["tit"],
|
||||||
usage: "<title>",
|
usage: "<title>",
|
||||||
description: "Sets your title (the name of your color role).\nUse “none” to clear your title.",
|
description: "Sets your title (the name of your color role).\nUse “none” to clear your title.",
|
||||||
exec: async function (message) {
|
exec: async function (message) {
|
||||||
var str = message.txt(1);
|
var str = message.txt(1);
|
||||||
if (!str) return false;
|
if (!str) return false;
|
||||||
if (str == "none") str = "";
|
if (str == "none") str = "";
|
||||||
if (str.length > 98) str = str.substr(0,97) + '…';
|
if (str.length > 98) str = str.substr(0,97) + '…';
|
||||||
var role = message.member.roles.find(role => role.name.startsWith('['));
|
var role = message.member.roles.find(role => role.name.startsWith('['));
|
||||||
role.setName(`[${str}]`);
|
role.setName(`[${str}]`);
|
||||||
message.react("🆗");
|
message.react("🆗");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue