Sélectionner une révision Git
matrixeventprocessor.ts
-
Andrew Morgan andrew@amorgan.xyz a rédigéAndrew Morgan andrew@amorgan.xyz a rédigé
matrixeventprocessor.ts 5,37 Kio
import * as Discord from "discord.js";
import {MessageProcessorOpts, MessageProcessor} from "./messageprocessor";
import {DiscordBot} from "./bot";
import {DiscordBridgeConfig} from "./config";
import * as escapeStringRegexp from "escape-string-regexp";
import {Util} from "./util";
import * as path from "path";
import * as mime from "mime";
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,
readonly bridge: any,
) {
}
}
export class MatrixEventProcessor {
private config: DiscordBridgeConfig;
private bridge: any;
constructor (opts: MatrixEventProcessorOpts) {
this.config = opts.config;
this.bridge = opts.bridge;
}
public EventToEmbed(event: any, profile: any|null, channel: Discord.TextChannel): Discord.RichEmbed {
let body = this.config.bridge.disableDiscordMentions ? event.content.body :
this.FindMentionsInPlainBody(
event.content.body,
channel.members.array(),
);
// Replace @everyone
if (this.config.bridge.disableEveryoneMention) {
body = body.replace(new RegExp(`@everyone`, "g"), "@ everyone");
}
// Replace @here
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) {
if (profile.displayname &&
profile.displayname.length >= MIN_NAME_LENGTH &&
profile.displayname.length <= MAX_NAME_LENGTH) {
displayName = profile.displayname;
}
if (profile.avatar_url) {
const mxClient = this.bridge.getClientFactory().getClientAs();
avatarUrl = mxClient.mxcUrlToHttp(profile.avatar_url);
}
/* See issue #82
const isMarkdown = (event.content.format === "org.matrix.custom.html");
if (!isMarkdown) {
body = "\\" + body;