Skip to content

Commit

Permalink
fix: Change status and colours of discord proposal events
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaituVR committed Mar 7, 2024
1 parent ac67304 commit 0a62da5
Showing 1 changed file with 35 additions and 23 deletions.
58 changes: 35 additions & 23 deletions src/providers/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
DiscordAPIError,
StringSelectMenuBuilder,
StringSelectMenuOptionBuilder,
ComponentType
ComponentType,
ColorResolvable
} from 'discord.js';
import db from '../helpers/mysql';
import removeMd from 'remove-markdown';
Expand Down Expand Up @@ -117,26 +118,30 @@ const PROPOSAL_EVENTS = [
{
id: 'proposal/created',
label: 'Proposal Created',
message: 'New proposal created',
description: 'When a proposal is created.'
status: 'Created',
description: 'When a proposal is created.',
color: '#C5C5C5'
},
{
id: 'proposal/start',
label: 'Proposal Start',
message: 'Proposal started',
description: 'When a proposal starts.'
status: 'Started',
description: 'When a proposal starts.',
color: '#21B66F'
},
{
id: 'proposal/end',
label: 'Proposal End',
message: 'Proposal closed',
description: 'When a proposal ends.'
status: 'Closed',
description: 'When a proposal ends.',
color: '#BF40BF'
},
{
id: 'proposal/deleted',
label: 'Proposal Deleted',
message: 'Proposal deleted',
description: 'When a proposal is deleted.'
status: 'Deleted',
description: 'When a proposal is deleted.',
color: '#FF0000'
}
];

Expand Down Expand Up @@ -480,9 +485,8 @@ const sendToSubscribers = (event, proposal, embed, components) => {
if (subs[proposal.space.id] || subs['*']) {
[...(subs['*'] || []), ...(subs[proposal.space.id] || [])].forEach(sub => {
if (sub.events && !JSON.parse(sub.events).includes(event)) return;
const eventName = PROPOSAL_EVENTS.find(e => e.id === event)?.message;
sendMessage(sub.channel, {
content: `${eventName} on \`${proposal.space.id}\` space ${sub.mention}`,
content: `${sub.mention}`,
embeds: [embed],
components
});
Expand All @@ -493,22 +497,31 @@ const sendToSubscribers = (event, proposal, embed, components) => {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
export async function send(eventObj, proposal, _subscribers) {
const event = eventObj.event;
const proposalEvent = PROPOSAL_EVENTS.find(e => e.id === event)!;
const color = proposalEvent.color;
const status = proposalEvent.status;
const avatar = `https://cdn.stamp.fyi/space/${proposal.space.id}?s=56`;
try {
if (event === 'proposal/deleted') {
const color = '#FF0000';
const embed = new EmbedBuilder().setColor(color).addFields({
name: 'Proposal ID',
value: `\`${proposal.id}\``,
inline: true
});
const embed = new EmbedBuilder()
.setAuthor({
name: `${proposal.space.id}`,
iconURL: avatar
})
.setColor(color as ColorResolvable)
.addFields(
{
name: 'Proposal ID',
value: `\`${proposal.id}\``,
inline: true
},
{ name: 'Status', value: status, inline: true }
)
.addFields();
sendToSubscribers(event, proposal, embed, []);
return { success: true };
}

let status = proposal.start > Date.now() / 1e3 ? 'Pending' : 'Active';
if (proposal.end < Date.now() / 1e3) status = 'Ended';
const color = '#21B66F';

const url = `https://snapshot.org/#/${proposal.space.id}/proposal/${proposal.id}`;

let components =
Expand All @@ -533,10 +546,9 @@ export async function send(eventObj, proposal, _subscribers) {
const limit = 4096 / 16;
let preview = removeMd(proposal.body).slice(0, limit);
if (proposal.body.length > limit) preview += `... [Read more](${url})`;
const avatar = `https://cdn.stamp.fyi/space/${proposal.space.id}?s=56`;

const embed = new EmbedBuilder()
.setColor(color)
.setColor(color as ColorResolvable)
.setTitle(proposal.title)
.setURL(url)
.setTimestamp(proposal.created * 1e3)
Expand Down

0 comments on commit 0a62da5

Please sign in to comment.