Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider 2f6b33a4 rédigé par Will Hunt's avatar Will Hunt Validation de GitHub
Parcourir les fichiers

Merge pull request #139 from anoadragon453/anoa/custom-emotes

[WIP] Add support for custom discord emojis
parents f4929b67 628677fc
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";
const MaxFileSize = 8000000;
const MIN_NAME_LENGTH = 2;
const MAX_NAME_LENGTH = 32;
const DISCORD_EMOJI_REGEX = /:(\w+):/g;
export class MatrixEventProcessorOpts {
constructor(
readonly config: DiscordBridgeConfig,
......@@ -45,6 +47,10 @@ export class MatrixEventProcessor {
if (this.config.bridge.disableHereMention) {
body = body.replace(new RegExp(`@here`, "g"), "@ here");
}
// Handle discord custom emoji
body = this.ReplaceDiscordEmoji(body, channel.guild);
let displayName = event.sender;
let avatarUrl = undefined;
if (profile) {
......@@ -92,6 +98,23 @@ export class MatrixEventProcessor {
return body;
}
public ReplaceDiscordEmoji(content: string, guild: Discord.Guild): string {
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
const emoji = guild.emojis.find((e) => e.name === emojiName);
if (emoji) {
// Replace :a: with <:a:123ID123>
content = content.replace(emojiNameWithColons, `<${emojiNameWithColons}${emoji.id}>`);
}
results = DISCORD_EMOJI_REGEX.exec(content);
}
return content;
}
public async HandleAttachment(event: any, mxClient: any): Promise<string|Discord.FileOptions> {
const hasAttachment = [
"m.image",
......
......@@ -25,7 +25,6 @@ export class MessageProcessorOpts {
constructor (readonly domain: string, readonly bot: DiscordBot = null) {
}
}
export class MessageProcessorMatrixResult {
......@@ -163,7 +162,9 @@ export class MessageProcessor {
`Could not insert emoji ${id} for msg ${msg.id} in guild ${msg.guild.id}: ${ex}`,
);
}
results = EMOJI_REGEX.exec(content);
}
return content;
}
......
export class MockCollection<T1, T2> extends Map {
import { Collection } from "discord.js";
export class MockCollection<T1, T2> extends Collection<T1, T2> {
public array(): T2[] {
return [...this.values()];
}
......
export class MockEmoji {
constructor (public id: string = "", public name = "") { }
}
import {MockCollection} from "./collection";
import {MockMember} from "./member";
import {MockEmoji} from "./emoji";
import {Channel} from "discord.js";
export class MockGuild {
public channels = new MockCollection<string, Channel>();
public members = new MockCollection<string, MockMember>();
public emojis = new MockCollection<string, MockEmoji>();
public id: string;
public name: string;
constructor(id: string, channels: any[] = [], name: string = null) {
......
......@@ -7,7 +7,9 @@ import * as Proxyquire from "proxyquire";
import { PresenceHandler } from "../src/presencehandler";
import { DiscordBot } from "../src/bot";
import { MockGuild } from "./mocks/guild";
import { MockCollection } from "./mocks/collection";
import { MockMember } from "./mocks/member";
import { MockEmoji } from "./mocks/emoji";
import {MatrixEventProcessor, MatrixEventProcessorOpts} from "../src/matrixeventprocessor";
import {DiscordBridgeConfig} from "../src/config";
import {MessageProcessor, MessageProcessorOpts} from "../src/messageprocessor";
......@@ -204,6 +206,42 @@ describe("MatrixEventProcessor", () => {
}, {avatar_url: "test"}, mockChannel as any);
Chai.assert.equal(evt.description, "@ here Hello!");
});
it("Should process custom discord emojis.", () => {
const processor = createMatrixEventProcessor(false, false, true);
const mockEmoji = new MockEmoji("123", "supercake");
const mockCollectionEmojis = new MockCollection<string, MockEmoji>();
mockCollectionEmojis.set("123", mockEmoji);
const mockChannelEmojis = new MockChannel("test", {
emojis: mockCollectionEmojis,
});
const evt = processor.EventToEmbed({
sender: "@test:localhost",
content: {
body: "I like :supercake:",
},
}, {avatar_url: "test"}, mockChannelEmojis as any);
Chai.assert.equal(evt.description, "I like <:supercake:123>");
});
it("Should not process invalid custom discord emojis.", () => {
const processor = createMatrixEventProcessor(false, false, true);
const mockEmoji = new MockEmoji("123", "supercake");
const mockCollectionEmojis = new MockCollection<string, MockEmoji>();
mockCollectionEmojis.set("123", mockEmoji);
const mockChannelEmojis = new MockChannel("test", {
emojis: mockCollectionEmojis,
});
const evt = processor.EventToEmbed({
sender: "@test:localhost",
content: {
body: "I like :lamecake:",
},
}, {avatar_url: "test"}, mockChannelEmojis as any);
Chai.assert.equal(evt.description, "I like :lamecake:");
});
});
describe("FindMentionsInPlainBody", () => {
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