当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Intent.getClient方法代码示例

本文整理汇总了TypeScript中matrix-appservice-bridge.Intent.getClient方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Intent.getClient方法的具体用法?TypeScript Intent.getClient怎么用?TypeScript Intent.getClient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在matrix-appservice-bridge.Intent的用法示例。


在下文中一共展示了Intent.getClient方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。

示例1: UploadContentFromUrl

    /**
     * uploadContentFromUrl - Upload content from a given URL to the homeserver
     * and return a MXC URL.
     */
    public static async UploadContentFromUrl(url: string, intent: Intent, name: string | null): Promise<IUploadResult> {
        let contenttype;
        name = name || null;
        try {
            const bufferRet = (await (new Promise((resolve, reject) => {
                let ht;
                if (url.startsWith("https")) {
                    ht = https;
                } else {
                    ht = http;
                }
                const req = ht.get( url, (res) => {
                    let buffer = Buffer.alloc(0);

                    if (res.headers.hasOwnProperty("content-type")) {
                        contenttype = res.headers["content-type"];
                    } else {
                        log.verbose("No content-type given by server, guessing based on file name.");
                        contenttype = mime.lookup(url);
                    }

                    if (name === null) {
                        const names = url.split("/");
                        name = names[names.length - 1];
                    }

                    res.on("data", (d) => {
                        buffer = Buffer.concat([buffer, d]);
                    });

                    res.on("end", () => {
                        resolve(buffer);
                    });
                });
                req.on("error", (err) => {
                    reject(`Failed to download. ${err.code}`);
                });
            }))) as Buffer;
            const size = bufferRet.length;
            const contentUri = await intent.getClient().uploadContent(bufferRet, {
                name,
                onlyContentUri: true,
                rawResponse: false,
                type: contenttype,
            });
            log.verbose("Media uploaded to ", contentUri);
            return {
                mxcUrl: contentUri,
                size,
            };
        } catch (reason) {
            log.error("Failed to upload content:\n", reason);
            throw reason;
        }
    }
开发者ID:Half-Shot,项目名称:matrix-appservice-discord,代码行数:59,代码来源:util.ts

示例2: GetMxidFromName

 public static async GetMxidFromName(intent: Intent, name: string, channelMxids: string[]) {
     if (name[0] === "@" && name.includes(":")) {
         return name;
     }
     const client = intent.getClient();
     const matrixUsers = {};
     let matches = 0;
     await Promise.all(channelMxids.map((chan) => {
         // we would use this.bridge.getBot().getJoinedMembers()
         // but we also want to be able to search through banned members
         // so we gotta roll our own thing
         return client._http.authedRequestWithPrefix(
             undefined,
             "GET",
             `/rooms/${encodeURIComponent(chan)}/members`,
             undefined,
             undefined,
             "/_matrix/client/r0",
         ).then((res) => {
             res.chunk.forEach((member) => {
                 if (member.membership !== "join" && member.membership !== "ban") {
                     return;
                 }
                 const mxid = member.state_key;
                 if (mxid.startsWith("@_discord_")) {
                     return;
                 }
                 let displayName = member.content.displayname;
                 if (!displayName && member.unsigned && member.unsigned.prev_content &&
                     member.unsigned.prev_content.displayname) {
                     displayName = member.unsigned.prev_content.displayname;
                 }
                 if (!displayName) {
                     displayName = mxid.substring(1, mxid.indexOf(":"));
                 }
                 if (name.toLowerCase() === displayName.toLowerCase() || name === mxid) {
                     matrixUsers[mxid] = displayName;
                     matches++;
                 }
             });
         });
     }));
     if (matches === 0) {
         throw Error(`No users matching ${name} found`);
     }
     if (matches > 1) {
         let errStr = "Multiple matching users found:\n";
         for (const mxid of Object.keys(matrixUsers)) {
             errStr += `${matrixUsers[mxid]} (\`${mxid}\`)\n`;
         }
         throw Error(errStr);
     }
     return Object.keys(matrixUsers)[0];
 }
开发者ID:Half-Shot,项目名称:matrix-appservice-discord,代码行数:54,代码来源:util.ts


注:本文中的matrix-appservice-bridge.Intent.getClient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。