Skip to content
Extraits de code Groupes Projets
Valider 0b8e25a7 rédigé par Andrew Morgan andrew@amorgan.xyz's avatar Andrew Morgan andrew@amorgan.xyz
Parcourir les fichiers

Add support for custom discord emojis.

parent f4929b67
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -11,6 +11,8 @@ import * as log from "npmlog"; ...@@ -11,6 +11,8 @@ import * as log from "npmlog";
const MaxFileSize = 8000000; const MaxFileSize = 8000000;
const MIN_NAME_LENGTH = 2; const MIN_NAME_LENGTH = 2;
const MAX_NAME_LENGTH = 32; const MAX_NAME_LENGTH = 32;
const DISCORD_EMOJI_REGEX = /:(\w+):/g;
export class MatrixEventProcessorOpts { export class MatrixEventProcessorOpts {
constructor( constructor(
readonly config: DiscordBridgeConfig, readonly config: DiscordBridgeConfig,
...@@ -45,6 +47,10 @@ export class MatrixEventProcessor { ...@@ -45,6 +47,10 @@ export class MatrixEventProcessor {
if (this.config.bridge.disableHereMention) { if (this.config.bridge.disableHereMention) {
body = body.replace(new RegExp(`@here`, "g"), "@ here"); body = body.replace(new RegExp(`@here`, "g"), "@ here");
} }
// Handle discord custom emoji
body = this.ReplaceDiscordEmoji(body, channel.guild);
let displayName = event.sender; let displayName = event.sender;
let avatarUrl = undefined; let avatarUrl = undefined;
if (profile) { if (profile) {
...@@ -92,6 +98,25 @@ export class MatrixEventProcessor { ...@@ -92,6 +98,25 @@ export class MatrixEventProcessor {
return body; return body;
} }
public ReplaceDiscordEmoji(content: string, guild: Discord.Guild): string {
console.log("Gonna replace")
let results = DISCORD_EMOJI_REGEX.exec(content);
while (results !== null) {
const emojiName = results[1];
const emojiNameWithColons = results[0];
// Check if this emoji exists in the guild
if(guild.emojis[emojiName] !== null) {
// Replace :a: with <:a:123ID123>
const emojiID = guild.emojis.find((emoji) => { return emoji.name === emojiName }).id;
content = content.replace(emojiNameWithColons, `<${emojiNameWithColons}${emojiID}>`);
}
results = DISCORD_EMOJI_REGEX.exec(content);
}
return content;
}
public async HandleAttachment(event: any, mxClient: any): Promise<string|Discord.FileOptions> { public async HandleAttachment(event: any, mxClient: any): Promise<string|Discord.FileOptions> {
const hasAttachment = [ const hasAttachment = [
"m.image", "m.image",
......
...@@ -25,7 +25,6 @@ export class MessageProcessorOpts { ...@@ -25,7 +25,6 @@ export class MessageProcessorOpts {
constructor (readonly domain: string, readonly bot: DiscordBot = null) { constructor (readonly domain: string, readonly bot: DiscordBot = null) {
} }
} }
export class MessageProcessorMatrixResult { export class MessageProcessorMatrixResult {
...@@ -163,7 +162,9 @@ export class MessageProcessor { ...@@ -163,7 +162,9 @@ export class MessageProcessor {
`Could not insert emoji ${id} for msg ${msg.id} in guild ${msg.guild.id}: ${ex}`, `Could not insert emoji ${id} for msg ${msg.id} in guild ${msg.guild.id}: ${ex}`,
); );
} }
results = EMOJI_REGEX.exec(content); results = EMOJI_REGEX.exec(content);
} }
return content; return content;
} }
......
...@@ -204,6 +204,7 @@ describe("MatrixEventProcessor", () => { ...@@ -204,6 +204,7 @@ describe("MatrixEventProcessor", () => {
}, {avatar_url: "test"}, mockChannel as any); }, {avatar_url: "test"}, mockChannel as any);
Chai.assert.equal(evt.description, "@ here Hello!"); Chai.assert.equal(evt.description, "@ here Hello!");
}); });
// TODO: Add a test for replaceDiscordEmoji
}); });
describe("FindMentionsInPlainBody", () => { describe("FindMentionsInPlainBody", () => {
it("processes mentioned username correctly", async () => { it("processes mentioned username correctly", async () => {
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter