Sélectionner une révision Git
matrixcommandhandler.ts
matrixcommandhandler.ts 9,88 Kio
/*
Copyright 2019 matrix-appservice-discord
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import { DiscordBot } from "./bot";
import { Log } from "./log";
import { DiscordBridgeConfig } from "./config";
import { IMatrixEvent } from "./matrixtypes";
import { Provisioner } from "./provisioner";
import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from "./util";
import * as Discord from "better-discord.js";
import { Appservice } from "matrix-bot-sdk";
import { IRoomStoreEntry } from "./db/roomstore";
import * as markdown from "marked";
const log = new Log("MatrixCommandHandler");
const PROVISIONING_DEFAULT_POWER_LEVEL = 50;
const PROVISIONING_DEFAULT_USER_POWER_LEVEL = 0;
const ROOM_CACHE_MAXAGE_MS = 15 * 60 * 1000;
export class MatrixCommandHandler {
private botJoinedRooms: Set<string> = new Set(); // roomids
private botJoinedRoomsCacheUpdatedAt = 0;
private provisioner: Provisioner;
constructor(
private discord: DiscordBot,
private bridge: Appservice,
private config: DiscordBridgeConfig,
) {
this.provisioner = this.discord.Provisioner;
}
public async HandleInvite(event: IMatrixEvent) {
log.info(`Received invite for ${event.state_key} in room ${event.room_id}`);
await this.bridge.botIntent.joinRoom(event.room_id);
this.botJoinedRooms.add(event.room_id);
}
public async Process(event: IMatrixEvent, roomEntry: IRoomStoreEntry|null) {
if (!(await this.isBotInRoom(event.room_id))) {
log.warn(`Bot is not in ${event.room_id}. Ignoring command`);
return;
}
const actions: ICommandActions = {
bridge: {
description: "Bridges this room to a Discord channel",
/* eslint-disable prefer-template */
help: "How to bridge a Discord guild:\n" +
"1. Invite the bot to your Discord guild using this link: " + Util.GetBotLink(this.config) + "\n" +
"2. Invite me to the matrix room you'd like to bridge\n" +
"3. Open the Discord channel you'd like to bridge in a web browser\n" +
"4. In the matrix room, send the message `!discord bridge <guild id> <channel id>` " +
"(without the backticks)\n" +
" Note: The Guild ID and Channel ID can be retrieved from the URL in your web browser.\n" +
" The URL is formatted as https://discordapp.com/channels/GUILD_ID/CHANNEL_ID\n" +
"5. Enjoy your new bridge!",
/* eslint-enable prefer-template */