feat: discord mention roles

This commit is contained in:
Nevo David 2025-08-07 12:23:01 +07:00
parent 689612d0fd
commit 5bf30484b6
6 changed files with 61 additions and 14 deletions

View File

@ -263,8 +263,10 @@ export class IntegrationsController {
let newList: any[] | {none: true} = [];
try {
newList = await this.functionIntegration(org, body);
} catch (err) {}
newList = (await this.functionIntegration(org, body)) || [];
} catch (err) {
console.log(err);
}
if (!Array.isArray(newList) && newList?.none) {
return newList;
@ -283,8 +285,9 @@ export class IntegrationsController {
name: p.label || '',
username: p.id || '',
image: p.image || '',
doNotCache: p.doNotCache || false,
}))
.filter((f: any) => f.name)
.filter((f: any) => f.name && !f.doNotCache)
);
}
@ -298,7 +301,7 @@ export class IntegrationsController {
...newList as any[],
],
(p) => p.id
).filter((f) => f.label && f.image && f.id);
).filter((f) => f.label && f.id);
}
@Post('/function')

View File

@ -100,7 +100,7 @@ const MentionList: FC = (props: any) => {
onClick={() => selectItem(index)}
>
<img
src={item.image}
src={item.image || '/no-picture.jpg'}
alt={item.label}
className="w-[30px] h-[30px] rounded-full object-cover"
/>

View File

@ -161,6 +161,8 @@ export const stripHtmlValidation = (
.replace(/<h1>([.\s\S]*?)<\/h1>/g, (match, p1) => {
return `<h1># ${p1}</h1>\n`;
})
.replace(/&amp;/gi, '&')
.replace(/&nbsp;/gi, ' ')
.replace(/<h2>([.\s\S]*?)<\/h2>/g, (match, p1) => {
return `<h2>## ${p1}</h2>\n`;
})

View File

@ -37,7 +37,7 @@ export abstract class SocialAbstract {
d: { query: string },
id: string,
integration: Integration
): Promise<{ id: string; label: string; image: string }[] | { none: true }> {
): Promise<{ id: string; label: string; image: string, doNotCache?: boolean }[] | { none: true }> {
return { none: true };
}

View File

@ -159,7 +159,7 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
form.append(
'payload_json',
JSON.stringify({
content: post.message.replace(/\[\[\[(@\d.*?)]]]/g, (match, p1) => {
content: post.message.replace(/\[\[\[(@.*?)]]]/g, (match, p1) => {
return `<${p1}>`;
}),
attachments: post.media?.map((p, index) => ({
@ -228,6 +228,21 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
id: string,
integration: Integration
) {
const allRoles = await (
await fetch(`https://discord.com/api/guilds/${id}/roles`, {
headers: {
Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN_ID}`,
'Content-Type': 'application/json',
},
})
).json();
const matching = allRoles
.filter((role: any) =>
role.name.toLowerCase().includes(data.query.toLowerCase())
)
.filter((f) => f.name !== '@everyone' && f.name !== '@here');
const list = await (
await fetch(
`https://discord.com/api/guilds/${id}/members/search?query=${data.query}`,
@ -240,14 +255,41 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider {
)
).json();
return list.map((p: any) => ({
id: String(p.user.id),
label: p.user.global_name || p.user.username,
image: `https://cdn.discordapp.com/avatars/${p.user.id}/${p.user.avatar}.png`,
}));
return [
...[
{
id: String('here'),
label: 'here',
image: '',
doNotCache: true,
},
{
id: String('everyone'),
label: 'everyone',
image: '',
doNotCache: true,
},
].filter((role: any) => {
return role.label.toLowerCase().includes(data.query.toLowerCase());
}),
...matching.map((p: any) => ({
id: String('&' + p.id),
label: p.name.split('@')[1],
image: '',
doNotCache: true,
})),
...list.map((p: any) => ({
id: String(p.user.id),
label: p.user.global_name || p.user.username,
image: `https://cdn.discordapp.com/avatars/${p.user.id}/${p.user.avatar}.png`,
})),
];
}
mentionFormat(idOrHandle: string, name: string) {
return `[[[@${idOrHandle}]]]`;
if (name === '@here' || name === '@everyone') {
return name;
}
return `[[[@${idOrHandle.replace('@', '')}]]]`;
}
}

View File

@ -134,6 +134,6 @@ export interface SocialProvider
) => Promise<{ client_id: string; client_secret: string }>;
mention?: (
token: string, data: { query: string }, id: string, integration: Integration
) => Promise<{ id: string; label: string; image: string }[] | {none: true}>;
) => Promise<{ id: string; label: string; image: string, doNotCache?: boolean }[] | {none: true}>;
mentionFormat?(idOrHandle: string, name: string): string;
}