Skip to content

Commit

Permalink
Merge pull request #51 from iluxonchik/feature/govbot-0.0.19
Browse files Browse the repository at this point in the history
Feature/govbot 0.0.19 - Deliberation Phase Community Feedback
  • Loading branch information
iluxonchik authored Sep 19, 2024
2 parents 077ca41 + 05e185e commit 0ad4d88
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mina-govbot",
"version": "0.0.18",
"version": "0.0.19",
"description": "Discord bot for collective decision making for Mina Protocol",
"main": "index.js",
"directories": {
Expand Down
2 changes: 2 additions & 0 deletions src/channels/admin/actions/CountVotesAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { EndUserError } from '../../../Errors';
import { AnyModalMessageComponent } from '../../../types/common';
import { DiscordStatus } from '../../DiscordStatus';
import { Client } from 'discord.js';
import logging from '../../../logging';

export class CountVotesAction extends Action {
public allSubActions(): Action[] {
Expand Down Expand Up @@ -129,6 +130,7 @@ export class CountVotesAction extends Action {
if (error instanceof EndUserError) {
throw error;
} else {
logging.error(error);
throw new EndUserError('An unexpected error occurred while counting votes.');
}
}
Expand Down
49 changes: 43 additions & 6 deletions src/logic/VoteCountingLogic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CommitteeDeliberationVoteChoice } from '../types';
import { TrackedInteraction } from '../core/BaseClasses';
import logger from '../logging';
import { Op } from 'sequelize';
import { EmbedBuilder } from 'discord.js';
import { APIEmbedField, EmbedBuilder } from 'discord.js';

interface VoteResult {
projectId: number;
Expand Down Expand Up @@ -381,6 +381,33 @@ export class VoteCountingLogic {
return communityFeedback;
}

private static splitTextIntoChunks(text: string, maxLength: number): string[] {
const chunks: string[] = [];
let currentChunk = '';

text.split('\n').forEach((line) => {
if (currentChunk.length + line.length + 1 > maxLength) {
chunks.push(currentChunk.trim());
currentChunk = '';
}
currentChunk += line + '\n';
});

if (currentChunk) {
chunks.push(currentChunk.trim());
}

return chunks;
}

private static createEmbedField(name: string, value: string): APIEmbedField[] {
const chunks = this.splitTextIntoChunks(value, 1024);
return chunks.map((chunk, index) => ({
name: index === 0 ? name : `${name} (continued)`,
value: chunk,
}));
}

public static formatVoteReasoningMessage(voteResults: VoteResultWithReasoning[]): EmbedBuilder[] {
const embeds: EmbedBuilder[] = [];

Expand All @@ -393,18 +420,28 @@ export class VoteCountingLogic {
continue; // Skip projects with no votes or feedback
}

const embed = new EmbedBuilder()
let currentEmbed = new EmbedBuilder()
.setColor('#0099ff')
.setTitle(`Vote Reasoning - ${result.projectName} (ID: ${result.projectId})`)
.setDescription(`Proposer: ${result.proposerUsername}`);

const addFieldsToEmbed = (fields: APIEmbedField[]) => {
fields.forEach((field) => {
if (currentEmbed.data.fields && currentEmbed.data.fields.length >= 25) {
embeds.push(currentEmbed);
currentEmbed = new EmbedBuilder().setColor('#0099ff').setTitle(`Vote Reasoning - ${result.projectName} (Continued)`);
}
currentEmbed.addFields(field);
});
};

if (result.deliberationVotes.length > 0) {
let deliberationField = '';
for (const vote of result.deliberationVotes) {
deliberationField += `**${vote.voterUsername}**: ${vote.vote}\n`;
deliberationField += `Reasoning: ${vote.reason || 'No reason provided'}\n\n`;
}
embed.addFields({ name: 'Deliberation Phase Votes', value: deliberationField.trim() });
addFieldsToEmbed(this.createEmbedField('Deliberation Phase Votes', deliberationField.trim()));
}

if (result.considerationVotes.length > 0) {
Expand All @@ -413,7 +450,7 @@ export class VoteCountingLogic {
considerationField += `**${vote.voterUsername}**: ${vote.isPass ? 'Yes' : 'No'}\n`;
considerationField += `Reasoning: ${vote.reason || 'No reason provided'}\n\n`;
}
embed.addFields({ name: 'Consideration Phase Votes', value: considerationField.trim() });
addFieldsToEmbed(this.createEmbedField('Consideration Phase Votes', considerationField.trim()));
}

if (result.communityFeedback && result.communityFeedback.length > 0) {
Expand All @@ -423,10 +460,10 @@ export class VoteCountingLogic {
feedbackField += `Feedback: ${feedback.feedback}\n`;
feedbackField += `Reason for Change: ${feedback.reason || 'No reason provided'}\n\n`;
}
embed.addFields({ name: 'Community Feedback', value: feedbackField.trim() });
addFieldsToEmbed(this.createEmbedField('Community Feedback', feedbackField.trim()));
}

embeds.push(embed);
embeds.push(currentEmbed);
}

return embeds;
Expand Down

0 comments on commit 0ad4d88

Please sign in to comment.