Skip to content
Extraits de code Groupes Projets
Valider 45a7841b rédigé par Will Hunt's avatar Will Hunt
Parcourir les fichiers

Begin to add support for bridged rooms.

parent e840da4d
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -2,7 +2,7 @@ import { DiscordBridgeConfig } from "./config"; ...@@ -2,7 +2,7 @@ import { DiscordBridgeConfig } from "./config";
import { DiscordClientFactory } from "./clientfactory"; import { DiscordClientFactory } from "./clientfactory";
import { DiscordStore } from "./store"; import { DiscordStore } from "./store";
import { DiscordDMHandler } from "./dmhandler"; import { DiscordDMHandler } from "./dmhandler";
import { MatrixUser, RemoteUser, Bridge, RemoteRoom } from "matrix-appservice-bridge"; import { MatrixUser, RemoteUser, Bridge, RemoteRoom, Entry } from "matrix-appservice-bridge";
import { Util } from "./util"; import { Util } from "./util";
import * as Discord from "discord.js"; import * as Discord from "discord.js";
import * as log from "npmlog"; import * as log from "npmlog";
...@@ -47,7 +47,7 @@ export class DiscordBot { ...@@ -47,7 +47,7 @@ export class DiscordBot {
client.on("typingStart", (c, u) => { this.OnTyping(c, u, true); }); client.on("typingStart", (c, u) => { this.OnTyping(c, u, true); });
client.on("typingStop", (c, u) => { this.OnTyping(c, u, false); }); client.on("typingStop", (c, u) => { this.OnTyping(c, u, false); });
client.on("userUpdate", (_, newUser) => { this.UpdateUser(newUser); }); client.on("userUpdate", (_, newUser) => { this.UpdateUser(newUser); });
client.on("channelUpdate", (_, newChannel) => { this.UpdateRoom(<Discord.TextChannel> newChannel); }); client.on("channelUpdate", (_, newChannel) => { this.UpdateRooms(<Discord.TextChannel> newChannel); });
client.on("presenceUpdate", (_, newMember) => { this.UpdatePresence(newMember); }); client.on("presenceUpdate", (_, newMember) => { this.UpdatePresence(newMember); });
client.on("message", (msg) => { Bluebird.delay(MSG_PROCESS_DELAY).then(() => { client.on("message", (msg) => { Bluebird.delay(MSG_PROCESS_DELAY).then(() => {
this.OnMessage(msg); this.OnMessage(msg);
...@@ -152,7 +152,6 @@ export class DiscordBot { ...@@ -152,7 +152,6 @@ export class DiscordBot {
}).then((attachment) => { }).then((attachment) => {
if (attachment !== null) { if (attachment !== null) {
let name = this.GetFilenameForMediaEvent(event.content); let name = this.GetFilenameForMediaEvent(event.content);
console.log(name);
return { return {
file : { file : {
name, name,
...@@ -187,7 +186,7 @@ export class DiscordBot { ...@@ -187,7 +186,7 @@ export class DiscordBot {
return "matrix-media." + mime.extension(content.info.mimetype); return "matrix-media." + mime.extension(content.info.mimetype);
} }
private GetRoomIdFromChannel(channel: Discord.Channel): Promise<string> { private GetRoomIdsFromChannel(channel: Discord.Channel): Promise<string[]> {
return this.bridge.getRoomStore().getEntriesByRemoteRoomData({ return this.bridge.getRoomStore().getEntriesByRemoteRoomData({
discord_channel: channel.id, discord_channel: channel.id,
}).then((rooms) => { }).then((rooms) => {
...@@ -195,36 +194,45 @@ export class DiscordBot { ...@@ -195,36 +194,45 @@ export class DiscordBot {
log.verbose("DiscordBot", `Got message but couldn"t find room chan id:${channel.id} for it.`); log.verbose("DiscordBot", `Got message but couldn"t find room chan id:${channel.id} for it.`);
return Promise.reject("Room not found."); return Promise.reject("Room not found.");
} }
return rooms[0].matrix.getId(); return rooms.map((room) => {return room.matrix.getId(); });
}); });
} }
private UpdateRoom(discordChannel: Discord.TextChannel): Promise<null> { private UpdateRooms(discordChannel: Discord.TextChannel): Promise<null> {
const intent = this.bridge.getIntent(); const intent = this.bridge.getIntent();
const roomStore = this.bridge.getRoomStore(); const roomStore = this.bridge.getRoomStore();
let entry: RemoteRoom; return this.GetRoomIdsFromChannel(discordChannel).then((rooms) => {
let roomId = null; return roomStore.getEntriesByMatrixIds(rooms).then( (entries) => {
return this.GetRoomIdFromChannel(discordChannel).then((r) => { return Object.keys(entries).map((key) => entries[key]);
roomId = r; });
return roomStore.getEntriesByMatrixId(roomId); }).then((entries: any) => {
}).then((entries) => { return Promise.all(entries.map((entry) => {
if (entries.length === 0) { if (entry.length === 0) {
return Promise.reject("Couldn't update room for channel, no assoicated entry in roomstore."); return Promise.reject("Couldn't update room for channel, no assoicated entry in roomstore.");
} }
entry = entries[0]; return this.UpdateRoomEntry(entry[0], discordChannel);
return; }));
}).then(() => { });
}
private UpdateRoomEntry(entry: Entry, discordChannel: Discord.TextChannel): Promise<null> {
const intent = this.bridge.getIntent();
const roomStore = this.bridge.getRoomStore();
const roomId = entry.matrix.getId();
return new Promise(() => {
const name = `[Discord] ${discordChannel.guild.name} #${discordChannel.name}`; const name = `[Discord] ${discordChannel.guild.name} #${discordChannel.name}`;
if (entry.remote.get("discord_name") !== name) { if (entry.remote.get("update_name") && entry.remote.get("discord_name") !== name) {
return intent.setRoomName(roomId, name).then(() => { return intent.setRoomName(roomId, name).then(() => {
log.info("DiscordBot", `Updated name for ${roomId}`);
entry.remote.set("discord_name", name); entry.remote.set("discord_name", name);
return roomStore.upsertEntry(entry); return roomStore.upsertEntry(entry);
}); });
} }
}).then(() => { }).then(() => {
if (entry.remote.get("discord_topic") !== discordChannel.topic) { if ( entry.remote.get("update_topic") && entry.remote.get("discord_topic") !== discordChannel.topic) {
return intent.setRoomTopic(roomId, discordChannel.topic).then(() => { return intent.setRoomTopic(roomId, discordChannel.topic).then(() => {
entry.remote.set("discord_topic", discordChannel.topic); entry.remote.set("discord_topic", discordChannel.topic);
log.info("DiscordBot", `Updated topic for ${roomId}`);
return roomStore.upsertEntry(entry); return roomStore.upsertEntry(entry);
}); });
} }
...@@ -292,9 +300,11 @@ export class DiscordBot { ...@@ -292,9 +300,11 @@ export class DiscordBot {
} }
private OnTyping(channel: Discord.Channel, user: Discord.User, isTyping: boolean) { private OnTyping(channel: Discord.Channel, user: Discord.User, isTyping: boolean) {
return this.GetRoomIdFromChannel(channel).then((room) => { return this.GetRoomIdsFromChannel(channel).then((rooms) => {
const intent = this.bridge.getIntentFromLocalpart(`_discord_${user.id}`); const intent = this.bridge.getIntentFromLocalpart(`_discord_${user.id}`);
return intent.sendTyping(room, isTyping); return Promise.all(rooms.map((room) => {
return intent.sendTyping(room, isTyping);
}));
}).catch((err) => { }).catch((err) => {
log.verbose("DiscordBot", "Failed to send typing indicator.", err); log.verbose("DiscordBot", "Failed to send typing indicator.", err);
}); });
...@@ -335,8 +345,8 @@ export class DiscordBot { ...@@ -335,8 +345,8 @@ export class DiscordBot {
return; // Skip *our* messages return; // Skip *our* messages
} }
this.UpdateUser(msg.author).then(() => { this.UpdateUser(msg.author).then(() => {
return this.GetRoomIdFromChannel(msg.channel); return this.GetRoomIdsFromChannel(msg.channel);
}).then((room) => { }).then((rooms) => {
const intent = this.bridge.getIntentFromLocalpart(`_discord_${msg.author.id}`); const intent = this.bridge.getIntentFromLocalpart(`_discord_${msg.author.id}`);
// Check Attachements // Check Attachements
msg.attachments.forEach((attachment) => { msg.attachments.forEach((attachment) => {
...@@ -353,22 +363,27 @@ export class DiscordBot { ...@@ -353,22 +363,27 @@ export class DiscordBot {
info.w = attachment.width; info.w = attachment.width;
info.h = attachment.height; info.h = attachment.height;
} }
intent.sendMessage(room, { rooms.forEach((room) => {
body: attachment.filename, intent.sendMessage(room, {
info, body: attachment.filename,
msgtype, info,
url: content.mxc_url, msgtype,
url: content.mxc_url,
});
}); });
}); });
}); });
if (msg.content !== null && msg.content !== "") { if (msg.content !== null && msg.content !== "") {
// Replace mentions. // Replace mentions.
let content = this.FormatDiscordMessage(msg); let content = this.FormatDiscordMessage(msg);
intent.sendMessage(room, { const fBody = marked(content);
body: content, rooms.forEach((room) => {
msgtype: "m.text", intent.sendMessage(room, {
formatted_body: marked(content), body: content,
format: "org.matrix.custom.html", msgtype: "m.text",
formatted_body: fBody,
format: "org.matrix.custom.html",
});
}); });
} }
}).catch((err) => { }).catch((err) => {
......
import { DiscordStore } from "../store"; import { DiscordStore } from "../store";
export interface IDbSchema { export interface IDbSchema {
description: string, description: string;
run(store: DiscordStore): Promise<null>; run(store: DiscordStore): Promise<null>;
} }
...@@ -145,6 +145,8 @@ export class MatrixRoomHandler { ...@@ -145,6 +145,8 @@ export class MatrixRoomHandler {
remote.set("discord_type", "text"); remote.set("discord_type", "text");
remote.set("discord_guild", channel.guild.id); remote.set("discord_guild", channel.guild.id);
remote.set("discord_channel", channel.id); remote.set("discord_channel", channel.id);
remote.set("update_name", true);
remote.set("update_topic", true);
const gname = channel.guild.name.replace(" ", "-"); const gname = channel.guild.name.replace(" ", "-");
const cname = channel.name.replace(" ", "-"); const cname = channel.name.replace(" ", "-");
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter