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

Discord users now apply profile information.

Fixed sending the discord bot's messages through matrix.
parent 023a98fd
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
import { DiscordBridgeConfig } from "./config";
import * as Discord from "discord.js";
import * as log from "npmlog";
import { MatrixUser, RemoteUser } from "matrix-appservice-bridge";
import { Util } from "./util";
export class DiscordBot {
private config: DiscordBridgeConfig;
private bot: Discord.Client;
private discord_user: Discord.ClientUser;
private bridge;
constructor(config: DiscordBridgeConfig, bridge) {
this.config = config;
......@@ -22,7 +25,12 @@ export class DiscordBot {
this.bot.on("typingStop", (c, u) => { this.OnTyping(c, u, false); });
// create an event listener for messages
this.bot.on("message", (msg) => {
this.GetRoomIdFromChannel(msg.channel).then((room) => {
if (msg.author.id === this.bot.user.id) {
return; // Skip *our* messages
}
this.UpdateUser(msg.author).then(() => {
return this.GetRoomIdFromChannel(msg.channel);
}).then((room) => {
const intent = this.bridge.getIntentFromLocalpart(`_discord_${msg.author.id}`);
intent.sendText(room, msg.content);
});
......@@ -31,30 +39,11 @@ export class DiscordBot {
this.bot.login(this.config.auth.botToken);
}
private GetRoomIdFromChannel(channel: Discord.Channel): Promise<string> {
return this.bridge.getRoomStore().getEntriesByRemoteRoomData({
discord_channel: channel.id,
}).then((rooms) => {
if (rooms.length === 0) {
log.warn("DiscordBot", `Got message but couldn't find room chan id:${channel.id} for it.`);
return Promise.reject("Room not found.");
}
return rooms[0].matrix.getId();
});
}
private OnTyping(channel: Discord.Channel, user: Discord.User, isTyping: boolean) {
this.GetRoomIdFromChannel(channel).then((room) => {
const intent = this.bridge.getIntentFromLocalpart(`_discord_${user.id}`);
intent.sendTyping(room, isTyping);
});
}
public GetBot (): Discord.Client {
return this.bot;
}
public LookupRoom (server: string, room: string): Promise<Discord.TextChannel> {
const guild = this.bot.guilds.find((g) => {
return (g.id === server || g.name.replace(" ", "-") === server);
......@@ -108,4 +97,64 @@ export class DiscordBot {
public OnUserQuery (userId: string): any {
return false;
}
private GetRoomIdFromChannel(channel: Discord.Channel): Promise<string> {
return this.bridge.getRoomStore().getEntriesByRemoteRoomData({
discord_channel: channel.id,
}).then((rooms) => {
if (rooms.length === 0) {
log.warn("DiscordBot", `Got message but couldn't find room chan id:${channel.id} for it.`);
return Promise.reject("Room not found.");
}
return rooms[0].matrix.getId();
});
}
private UpdateUser(discordUser: Discord.User) {
let remoteUser: RemoteUser;
const displayName = discordUser.username + "#" + discordUser.discriminator;
const id = `_discord_${discordUser.id}:${this.config.bridge.domain}`;
const intent = this.bridge.getIntent("@"+id);
const userStore = this.bridge.getUserStore();
return userStore.getRemoteUser(discordUser.id).then((u) => {
remoteUser = u;
console.log(remoteUser);
if (remoteUser === null) {
remoteUser = new RemoteUser(discordUser.id);
return userStore.linkUsers(
new MatrixUser(id),
remoteUser
);
}
return Promise.resolve();
}).then(() => {
console.log(remoteUser.get("displayname"), "!==", displayName);
if (remoteUser.get("displayname") !== displayName) {
return intent.setDisplayName(displayName).then(() => {
remoteUser.set("displayname", displayName);
return userStore.setRemoteUser(remoteUser);
});
}
return true;
}).then(() => {
console.log(remoteUser.get("avatarurl"), "!==", discordUser.avatarURL);
if (remoteUser.get("avatarurl") !== discordUser.avatarURL && discordUser.avatarURL !== null) {
return Util.uploadContentFromUrl(this.bridge, discordUser.avatarURL, intent, discordUser.avatar).then((avatar) => {
intent.setAvatarUrl(avatar.mxc_url).then(() => {
remoteUser.set("avatarurl", discordUser.avatarURL);
return userStore.setRemoteUser(remoteUser);
});
});
}
return true;
});
}
private OnTyping(channel: Discord.Channel, user: Discord.User, isTyping: boolean) {
this.GetRoomIdFromChannel(channel).then((room) => {
const intent = this.bridge.getIntentFromLocalpart(`_discord_${user.id}`);
intent.sendTyping(room, isTyping);
});
}
}
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