Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change status and colours of discord proposal events #185

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading