Sélectionner une révision Git
matrixcommandhandler.ts
matrixcommandhandler.ts 9,16 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 { Bridge, BridgeContext } from "matrix-appservice-bridge";
import { IMatrixEvent } from "./matrixtypes";
import { Provisioner } from "./provisioner";
import { Util, ICommandActions, ICommandParameters, CommandPermissonCheck } from "./util";
import * as Discord from "discord.js";
const log = new Log("MatrixCommandHandler");
/* tslint:disable:no-magic-numbers */
const PROVISIONING_DEFAULT_POWER_LEVEL = 50;
const PROVISIONING_DEFAULT_USER_POWER_LEVEL = 0;
const ROOM_CACHE_MAXAGE_MS = 15 * 60 * 1000;
/* tslint:enable:no-magic-numbers */
export class MatrixCommandHandler {
private botJoinedRooms: Set<string> = new Set(); // roomids
private botJoinedRoomsCacheUpdatedAt = 0;
private provisioner: Provisioner;
constructor(
private discord: DiscordBot,
private bridge: Bridge,
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}`);
if (event.state_key === this.discord.GetBotId()) {
log.info("Accepting invite for bridge bot");
await this.bridge.getIntent().joinRoom(event.room_id);
this.botJoinedRooms.add(event.room_id);
}
}
public async Process(event: IMatrixEvent, context: BridgeContext) {
const intent = this.bridge.getIntent();
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",
// tslint: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" +