Skip to content

Commit

Permalink
Merge pull request #71 from show-karma/feat/improve-logs
Browse files Browse the repository at this point in the history
feat: improve logs
  • Loading branch information
Arthh authored Jul 24, 2023
2 parents 28dc77a + 30e35ab commit e72e265
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/services/apecoin/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable no-console */
/* eslint-disable @typescript-eslint/no-unsafe-return */
import axios from 'axios';
Expand Down Expand Up @@ -58,19 +59,23 @@ async function fetchDelegateTrustLevel(
}

async function delegateHasPermission(delegate, dao) {
let hasPermission =
delegate.trustLevel = !delegate.handle ? 'no forum link' : 'already have the criterea';

delegate.hasPermission =
delegate.offChainVotesPct >= 50 ||
(+delegate.balance >= 50000 && delegate.offChainVotesPct > 0);

if (!hasPermission && delegate.handle) {
const trustLevel = await fetchDelegateTrustLevel(delegate.handle, dao.forum);
hasPermission = hasPermission || trustLevel >= 2;
if (!delegate.hasPermission && delegate.handle) {
delegate.trustLevel = await fetchDelegateTrustLevel(delegate.handle, dao.forum);
delegate.hasPermission = delegate.hasPermission || delegate.trustLevel >= 2;
}

return hasPermission;
return delegate;
}

async function manageRoles(client: Client, guildId: string, handles: string[], action: string) {
async function manageRoles(client: Client, guildId: string, handles: any[], action: string) {
if (!handles.length) return;

const guild = client.guilds.cache.get(guildId);

if (!guild) throw new Error('Guild not found');
Expand All @@ -80,11 +85,13 @@ async function manageRoles(client: Client, guildId: string, handles: string[], a
if (!role) throw new Error('Role not found');

for (const handle of handles) {
const member = await guild.members.fetch(handle).catch(console.error);
const member = await guild.members.fetch(handle.discordHandle).catch(console.error);

if (member) {
await member.roles[action](role).catch(console.error);
console.log(`Role: ${role.name} | action: ${action} | user: ${member.user.tag}`);
console.log(
`Role: ${role.name} | action: ${action} | user: ${member.user.tag} | address: ${handle.publicAddress} | offChain: ${handle.offChainVotesPct} | balance: ${handle.balance} | forumLevel: ${handle.trustLevel} | hasCriterea: ${handle.hasPermission}`
);
}
}
}
Expand All @@ -97,7 +104,7 @@ async function manageRoles(client: Client, guildId: string, handles: string[], a
if (!delegates?.length) return;

const addHandles = [];
let revokeHandles = [];
const revokeHandles = [];

const delegatesBalance = await getTokenBalance(
delegates.map((d) => d.publicAddress),
Expand All @@ -106,16 +113,11 @@ async function manageRoles(client: Client, guildId: string, handles: string[], a

for (const delegate of delegates) {
delegate.balance = +delegate.balance + (delegatesBalance?.[delegate.publicAddress] || 0);
const hasPermission = await delegateHasPermission(delegate, dao);
const delegateWithStatus = await delegateHasPermission(delegate, dao);

(hasPermission ? addHandles : revokeHandles).push(delegate.discordHandle);
(delegateWithStatus.hasPermission ? addHandles : revokeHandles).push(delegateWithStatus);
}

revokeHandles = revokeHandles.filter((handle) => !addHandles.includes(handle));

console.log('addHandles', addHandles);
console.log('revokeHandles', revokeHandles);

const client = new Client({
intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.DIRECT_MESSAGES]
});
Expand Down

0 comments on commit e72e265

Please sign in to comment.