Skip to content
GitLab
Explorer
Connexion
Navigation principale
Rechercher ou aller à…
Projet
M
matrix-appservice-discord
Gestion
Activité
Membres
Labels
Programmation
Tickets
Tableaux des tickets
Jalons
Wiki
Wiki externe
Code
Requêtes de fusion
Dépôt
Branches
Validations
Étiquettes
Graphe du dépôt
Comparer les révisions
Extraits de code
Compilation
Pipelines
Jobs
Planifications de pipeline
Artéfacts
Déploiement
Releases
Registre de paquets
Registre de conteneurs
Registre de modèles
Opération
Environnements
Modules Terraform
Surveillance
Incidents
Analyse
Données d'analyse des chaînes de valeur
Analyse des contributeurs
Données d'analyse CI/CD
Données d'analyse du dépôt
Expériences du modèle
Aide
Aide
Support
Documentation de GitLab
Comparer les forfaits GitLab
Forum de la communauté GitLab
Contribuer à GitLab
Donner votre avis
Raccourcis clavier
?
Extraits de code
Groupes
Projets
Afficher davantage de fils d'Ariane
salixor
matrix-appservice-discord
Validations
1f74dbe9
Non vérifiée
Valider
1f74dbe9
rédigé
Il y a 7 ans
par
Sorunome
Parcourir les fichiers
Options
Téléchargements
Correctifs
Plain Diff
fix discord message content parsing
parent
b527fead
Branches
Branches contenant la validation
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Modifications
1
Afficher les modifications d'espaces
En ligne
Côte à côte
Affichage de
1 fichier modifié
src/messageprocessor.ts
+52
-37
52 ajouts, 37 suppressions
src/messageprocessor.ts
avec
52 ajouts
et
37 suppressions
src/messageprocessor.ts
+
52
−
37
Voir le fichier @
1f74dbe9
...
...
@@ -5,9 +5,12 @@ import { DiscordBot } from "./bot";
import
*
as
escapeStringRegexp
from
"
escape-string-regexp
"
;
const
USER_REGEX
=
/<@!
?([
0-9
]
*
)
>/g
;
const
USER_REGEX_POSTMARK
=
/<@!
?([
0-9
]
*
)
>/g
;
const
CHANNEL_REGEX
=
/<#
?([
0-9
]
*
)
>/g
;
const
CHANNEL_REGEX_POSTMARK
=
/<#
?([
0-9
]
*
)
>/g
;
const
EMOJI_SIZE
=
"
1em
"
;
const
EMOJI_REGEX
=
/<:
\w
+:
?([
0-9
]
*
)
>/g
;
const
EMOJI_REGEX_POSTMARK
=
/<:
\w
+:
?([
0-9
]
*
)
>/g
;
const
MATRIX_TO_LINK
=
"
https://matrix.to/#/
"
;
marked
.
setOptions
({
...
...
@@ -37,18 +40,27 @@ export class MessageProcessor {
public
async
FormatDiscordMessage
(
msg
:
Discord
.
Message
):
Promise
<
MessageProcessorMatrixResult
>
{
const
result
=
new
MessageProcessorMatrixResult
();
// Replace embeds.
// first do the plain-text body
result
.
body
=
await
this
.
InsertDiscordSyntax
(
msg
.
content
,
msg
,
false
);
// for the formatted body we need to parse markdown first
// as else it'll HTML escape the result of the discord syntax
let
content
=
msg
.
content
;
content
=
marked
(
content
);
content
=
await
this
.
InsertDiscordSyntax
(
content
,
msg
,
true
);
result
.
formattedBody
=
content
;
return
result
;
}
public
async
InsertDiscordSyntax
(
content
:
string
,
msg
:
Discord
.
Message
,
postmark
:
boolean
):
Promise
<
string
>
{
// Replace embeds.
content
=
this
.
InsertEmbeds
(
content
,
msg
);
// Replace Users
content
=
this
.
ReplaceMembers
(
content
,
msg
);
content
=
this
.
ReplaceChannels
(
content
,
msg
);
content
=
await
this
.
ReplaceEmoji
(
content
,
msg
);
// Replace channels
result
.
body
=
content
;
result
.
formattedBody
=
marked
(
content
);
return
result
;
content
=
this
.
ReplaceMembers
(
content
,
msg
,
postmark
);
content
=
this
.
ReplaceChannels
(
content
,
msg
,
postmark
);
content
=
await
this
.
ReplaceEmoji
(
content
,
msg
,
postmark
);
return
content
;
}
public
InsertEmbeds
(
content
:
string
,
msg
:
Discord
.
Message
):
string
{
...
...
@@ -66,34 +78,37 @@ export class MessageProcessor {
return
content
;
}
public
ReplaceMembers
(
content
:
string
,
msg
:
Discord
.
Message
):
string
{
let
results
=
USER_REGEX
.
exec
(
content
);
public
ReplaceMembers
(
content
:
string
,
msg
:
Discord
.
Message
,
postmark
:
boolean
=
false
):
string
{
const
reg
=
postmark
?
USER_REGEX_POSTMARK
:
USER_REGEX
;
let
results
=
reg
.
exec
(
content
);
while
(
results
!==
null
)
{
const
id
=
results
[
1
];
const
member
=
msg
.
guild
.
members
.
get
(
id
);
const
memberId
=
`@_discord_
${
id
}
:
${
this
.
opts
.
domain
}
`
;
const
memberStr
=
member
?
member
.
user
.
username
:
memberId
;
content
=
content
.
replace
(
results
[
0
],
memberStr
);
results
=
USER_REGEX
.
exec
(
content
);
results
=
reg
.
exec
(
content
);
}
return
content
;
}
public
ReplaceChannels
(
content
:
string
,
msg
:
Discord
.
Message
):
string
{
let
results
=
CHANNEL_REGEX
.
exec
(
content
);
public
ReplaceChannels
(
content
:
string
,
msg
:
Discord
.
Message
,
postmark
:
boolean
=
false
):
string
{
const
reg
=
postmark
?
CHANNEL_REGEX_POSTMARK
:
CHANNEL_REGEX
;
let
results
=
reg
.
exec
(
content
);
while
(
results
!==
null
)
{
const
id
=
results
[
1
];
const
channel
=
msg
.
guild
.
channels
.
get
(
id
);
const
roomId
=
`#_discord_
${
msg
.
guild
.
id
}
_
${
id
}
:
${
this
.
opts
.
domain
}
`
;
const
channelStr
=
channel
?
"
#
"
+
channel
.
name
:
"
#
"
+
id
;
content
=
content
.
replace
(
results
[
0
],
`[
${
channelStr
}
](
${
MATRIX_TO_LINK
}${
roomId
}
)`
);
results
=
CHANNEL_REGEX
.
exec
(
content
);
results
=
reg
.
exec
(
content
);
}
return
content
;
}
public
async
ReplaceEmoji
(
content
:
string
,
msg
:
Discord
.
Message
):
Promise
<
string
>
{
let
results
=
EMOJI_REGEX
.
exec
(
content
);
public
async
ReplaceEmoji
(
content
:
string
,
msg
:
Discord
.
Message
,
postmark
:
boolean
=
false
):
Promise
<
string
>
{
const
reg
=
postmark
?
EMOJI_REGEX_POSTMARK
:
EMOJI_REGEX
;
let
results
=
reg
.
exec
(
content
);
while
(
results
!==
null
)
{
const
id
=
results
[
1
];
try
{
...
...
@@ -105,7 +120,7 @@ export class MessageProcessor {
`Could not insert emoji
${
id
}
for msg
${
msg
.
id
}
in guild
${
msg
.
guild
.
id
}
:
${
ex
}
`
,
);
}
results
=
EMOJI_REGEX
.
exec
(
content
);
results
=
reg
.
exec
(
content
);
}
return
content
;
}
...
...
Ce diff est replié.
Cliquez pour l'agrandir.
Aperçu
0%
Chargement en cours
Veuillez réessayer
ou
joindre un nouveau fichier
.
Annuler
You are about to add
0
people
to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Enregistrer le commentaire
Annuler
Veuillez vous
inscrire
ou vous
se connecter
pour commenter