Skip to content
Extraits de code Groupes Projets
Sélectionner une révision Git
  • e02671ac66e6576e98422e510115aee6a05184bf
  • develop par défaut protégée
  • implement-discord-markdown-update
  • matrix-attachments-order-fix
  • fix-oversized-file-transfer
  • matrix-attachment-order-fix
  • matrix-answer-modified-fix
  • cherry-pick-moise
8 résultats

clientfactory.ts

Blame
  • Bifurcation depuis ARISE / matrix-appservice-discord
    Le projet source a une visibilité limitée.
    clientfactory.ts 4,80 Kio
    /*
    Copyright 2017 - 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 { DiscordBridgeConfigAuth } from "./config";
    import { DiscordStore } from "./store";
    import { Client as DiscordClient, Intents, TextChannel } from "better-discord.js";
    import { Log } from "./log";
    import { MetricPeg } from "./metrics";
    
    const log = new Log("ClientFactory");
    
    export class DiscordClientFactory {
        private config: DiscordBridgeConfigAuth;
        private store: DiscordStore;
        private botClient: DiscordClient;
        private clients: Map<string, DiscordClient>;
        constructor(store: DiscordStore, config?: DiscordBridgeConfigAuth) {
            this.config = config!;
            this.clients = new Map();
            this.store = store;
        }
    
        public async init(): Promise<void> {
            if (this.config === undefined) {
                return Promise.reject("Client config not supplied.");
            }
            // We just need to make sure we have a bearer token.
            // Create a new Bot client.
            this.botClient = new DiscordClient({
                fetchAllMembers: this.config.usePrivilegedIntents,
                messageCacheLifetime: 5,
                ws: {
                    intents: this.config.usePrivilegedIntents ? Intents.ALL : Intents.NON_PRIVILEGED,
                },
            });
    
            const waitPromise = new Promise((resolve, reject) => {
                this.botClient.once("shardReady", resolve);
                this.botClient.once("shardError", reject);
            });
    
            try {
                await this.botClient.login(this.config.botToken, true);
                log.info("Waiting for shardReady signal");
                await waitPromise;
                log.info("Got shardReady signal");
            } catch (err) {
                log.error("Could not login as the bot user. This is bad!", err);
                throw err;
            }
    
        }
    
        public async getDiscordId(token: string): Promise<string> {
            const client = new DiscordClient({
                fetchAllMembers: false,
                messageCacheLifetime: 5,