Skip to content

Conversation

@kamiloox
Copy link
Contributor

@kamiloox kamiloox commented Jan 7, 2022

This function gives roles for top members of the last two weeks.

@typeofweb
Copy link
Member

@allcontributors please add @kamiloox for code

@allcontributors
Copy link
Contributor

@mmiszy

I've put up a pull request to add @kamiloox! 🎉

const removeMembersRoles = async (role: Role) => {
const membersWithRole = role?.members;

if (membersWithRole) await Promise.all(membersWithRole.map((member) => removeRole(member, role)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (membersWithRole) await Promise.all(membersWithRole.map((member) => removeRole(member, role)));
if (membersWithRole) {
return Promise.all(membersWithRole.map((member) => removeRole(member, role)));
}

type Role = Discord.Role | undefined;

const createKarmaRole = async (guild: Guild) => {
await guild?.roles.create({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await guild?.roles.create({
return guild.roles.create({


type Role = Discord.Role | undefined;

const createKarmaRole = async (guild: Guild) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const createKarmaRole = async (guild: Guild) => {
const createKarmaRole = (guild: Discord.Guild) => {

};

const fetchKarmaRole = async (guild: Guild) => {
return guild && (await guild?.roles.fetch()).cache.find((role) => role.name === ROLE_NAME);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Czemu wszędzie guild ma typ | undefined? Nie podoba mi się to

Comment on lines 36 to 38
if (!role) await createKarmaRole(guild);
else return role;
return await fetchKarmaRole(guild);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ten return await fetchKarmaRole nigdy się nie wykona, no nie? :P

Comment on lines 44 to 46
if (!member) console.error(`Member with id ${memberId} doesn't exists!`);
if (!role) console.error(`Role for karma doesn't exists`);
else await member?.roles.add(role);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ta linijka się wykona nawet jeśli nie ma role, tak ma być?

if (!member) console.error(`Member with id ${memberId} doesn't exists!`);
if (!role) console.error(`Role for karma doesn't exists`);
else await member?.roles.add(role);
} catch (err) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pozbyłbym się tego try catch stąd

const startDate = offsetDateByWeeks(new Date(), 2);
const bestKarmaMembers = await getBestMembers(startDate);

await Promise.all(bestKarmaMembers.map(({ _id }) => giveRole(guild, role, _id)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
await Promise.all(bestKarmaMembers.map(({ _id }) => giveRole(guild, role, _id)));
return Promise.all(bestKarmaMembers.map(({ _id }) => giveRole(guild, role, _id)));

const reassignRoles = async () => {
const client = new Discord.Client();
await client.login(getConfig('DISCORD_BOT_TOKEN'));
const guild = client.guilds.cache.get(TYPE_OF_WEB_GUILD_ID);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A jeśli nie ma gildii w cache?

const karmaRole = await getKarmaRole(guild);

await removeMembersRoles(karmaRole);
await assignMembersRoles(guild, karmaRole);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pozbyłbym się wszystkich try…catch z pozostałych funkcji i dał try…catch tylko tutaj na obie te funkcje :)

};

const fetchKarmaRole = async (guild: Discord.Guild) => {
return (await guild.roles.fetch()).cache.find((role) => role.name === ROLE_NAME);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return (await guild.roles.fetch()).cache.find((role) => role.name === ROLE_NAME);
const roles = await guild.roles.fetch();
return roles.cache.find((role) => role.name === ROLE_NAME);

Comment on lines 32 to 33
if (role) return role;
else return createKarmaRole(guild);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (role) return role;
else return createKarmaRole(guild);
if (role) {
return role;
} else {
return createKarmaRole(guild);
}

await client.login(getConfig('DISCORD_BOT_TOKEN'));
const guild = client.guilds.cache.get(TYPE_OF_WEB_GUILD_ID);

if (!guild) return console.error(`Guild with id ${TYPE_OF_WEB_GUILD_ID} doesn't exists!`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!guild) return console.error(`Guild with id ${TYPE_OF_WEB_GUILD_ID} doesn't exists!`);
if (!guild) {
return console.error(`Guild with id ${TYPE_OF_WEB_GUILD_ID} doesn't exists!`);
}


const karmaRole = await getKarmaRole(guild);

if (!karmaRole) return console.error(`Karma role doesn't exists!`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!karmaRole) return console.error(`Karma role doesn't exists!`);
if (!karmaRole) {
return console.error(`Karma role doesn't exists!`);
}

@typeofweb typeofweb closed this Jan 8, 2022
@typeofweb
Copy link
Member

#77

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants