How to use the matrix-bot-sdk.RichReply.createFor function in matrix-bot-sdk

To help you get started, we’ve selected a few matrix-bot-sdk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github matrix-org / mjolnir / src / commands / ImportCommand.ts View on Github external
export async function execImportCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) {
    const importRoomId = await mjolnir.client.resolveRoom(parts[2]);
    const list = mjolnir.lists.find(b => b.listShortcode === parts[3]);
    if (!list) {
        const errMessage = "Unable to find list - check your shortcode.";
        const errReply = RichReply.createFor(roomId, event, errMessage, errMessage);
        errReply["msgtype"] = "m.notice";
        return mjolnir.client.sendMessage(roomId, errReply);
    }

    let importedRules = 0;

    const state = await mjolnir.client.getRoomState(importRoomId);
    for (const stateEvent of state) {
        const content = stateEvent['content'] || {};
        if (!content || Object.keys(content).length === 0) continue;

        if (stateEvent['type'] === 'm.room.member' && stateEvent['state_key'] !== '') {
            // Member event - check for ban
            if (content['membership'] === 'ban') {
                const reason = content['reason'] || '';
github matrix-org / mjolnir / src / commands / StatusCommand.ts View on Github external
}

    html += `<b>Protected rooms: </b> ${Object.keys(mjolnir.protectedRooms).length}<br>`;
    text += `Protected rooms: ${mjolnir.protectedRooms.length}\n`;

    // Append list information
    html += "<b>Subscribed ban lists:</b><br><ul>";
    text += "Subscribed ban lists:\n";
    for (const list of mjolnir.lists) {
        const ruleInfo = `rules: ${list.serverRules.length} servers, ${list.userRules.length} users, ${list.roomRules.length} rooms`;
        html += `<li><a href="${list.roomRef}">${list.roomId}</a> (${ruleInfo})</li>`;
        text += `* ${list.roomRef} (${ruleInfo})\n`;
    }
    html += "</ul>";

    const reply = RichReply.createFor(roomId, event, text, html);
    reply["msgtype"] = "m.notice";
    return mjolnir.client.sendMessage(roomId, reply);
}
github matrix-org / mjolnir / src / commands / CommandHandler.ts View on Github external
"!mjolnir rules                                                      - Lists the rules currently in use by Mjolnir\n" +
                "!mjolnir sync                                                       - Force updates of all lists and re-apply rules\n" +
                "!mjolnir verify                                                     - Ensures Mjolnir can moderate all your rooms\n" +
                "!mjolnir list create                    - Creates a new ban list with the given shortcode and alias\n" +
                "!mjolnir watch                                       - Watches a ban list\n" +
                "!mjolnir unwatch                                     - Unwatches a ban list\n" +
                "!mjolnir import                      - Imports bans and ACLs into the given list\n" +
                "!mjolnir default                                         - Sets the default list for commands\n" +
                "!mjolnir deactivate                                        - Deactivates a user ID\n" +
                "!mjolnir protections                                                - List all available protections\n" +
                "!mjolnir enable                                         - Enables a particular protection\n" +
                "!mjolnir disable                                        - Disables a particular protection\n" +
                "!mjolnir help                                                       - This menu\n";
            const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
            const text = `Mjolnir help:\n${menu}`;
            const reply = RichReply.createFor(roomId, event, text, html);
            reply["msgtype"] = "m.notice";
            return await mjolnir.client.sendMessage(roomId, reply);
        }
    } catch (e) {
        LogService.error("CommandHandler", e);
        const text = "There was an error processing your command - see console/log for details";
        const reply = RichReply.createFor(roomId, event, text, text);
        reply["msgtype"] = "m.notice";
        return await mjolnir.client.sendMessage(roomId, reply);
    }
}
github matrix-org / mjolnir / src / commands / CreateBanListCommand.ts View on Github external
powerLevels['users'][event['sender']] = 50;

    const listRoomId = await mjolnir.client.createRoom({
        preset: "public_chat",
        room_alias_name: aliasLocalpart,
        invite: [event['sender']],
        initial_state: [{type: SHORTCODE_EVENT_TYPE, state_key: "", content: {shortcode: shortcode}}],
        power_level_content_override: powerLevels,
    });

    const roomRef = Permalinks.forRoom(listRoomId);
    await mjolnir.watchList(roomRef);

    const html = `Created new list (<a href="${roomRef}">${listRoomId}</a>). This list is now being watched.`;
    const text = `Created new list (${roomRef}). This list is now being watched.`;
    const reply = RichReply.createFor(roomId, event, text, html);
    reply["msgtype"] = "m.notice";
    await mjolnir.client.sendMessage(roomId, reply);
}
github matrix-org / mjolnir / src / commands / UnbanBanCommand.ts View on Github external
entity = parts[serverIndex];
        if (!ruleType) ruleType = RULE_SERVER;
        argumentIndex = serverIndex + 1;
    }

    if (!list) {
        list = mjolnir.lists.find(b => b.listShortcode.toLowerCase() === defaultShortcode);
    }

    let replyMessage = null;
    if (!list) replyMessage = "No ban list matching that shortcode was found";
    else if (!ruleType) replyMessage = "Please specify the type as either 'user', 'room', or 'server'";
    else if (!entity) replyMessage = "No entity found to ban";

    if (replyMessage) {
        const reply = RichReply.createFor(roomId, event, replyMessage, replyMessage);
        reply["msgtype"] = "m.notice";
        await mjolnir.client.sendMessage(roomId, reply);
        return null;
    }

    return {
        list,
        entity,
        ruleType,
        reason: parts.splice(argumentIndex).join(" ").trim(),
    };
}
github matrix-org / mjolnir / src / commands / CommandHandler.ts View on Github external
"!mjolnir default                                         - Sets the default list for commands\n" +
                "!mjolnir deactivate                                        - Deactivates a user ID\n" +
                "!mjolnir protections                                                - List all available protections\n" +
                "!mjolnir enable                                         - Enables a particular protection\n" +
                "!mjolnir disable                                        - Disables a particular protection\n" +
                "!mjolnir help                                                       - This menu\n";
            const html = `<b>Mjolnir help:</b><br><pre><code>${htmlEscape(menu)}</code></pre>`;
            const text = `Mjolnir help:\n${menu}`;
            const reply = RichReply.createFor(roomId, event, text, html);
            reply["msgtype"] = "m.notice";
            return await mjolnir.client.sendMessage(roomId, reply);
        }
    } catch (e) {
        LogService.error("CommandHandler", e);
        const text = "There was an error processing your command - see console/log for details";
        const reply = RichReply.createFor(roomId, event, text, text);
        reply["msgtype"] = "m.notice";
        return await mjolnir.client.sendMessage(roomId, reply);
    }
}
github matrix-org / mjolnir / src / commands / DumpRulesCommand.ts View on Github external
if (!hasRules) {
            html += "<li><i>No rules</i>";
            text += "* No rules\n";
        }

        html += "";
        text += "\n";
    }

    if (!hasLists) {
        html = "No ban lists configured";
        text = "No ban lists configured";
    }

    const reply = RichReply.createFor(roomId, event, text, html);
    reply["msgtype"] = "m.notice";
    return mjolnir.client.sendMessage(roomId, reply);
}
</li>
github matrix-org / mjolnir / src / commands / ImportCommand.ts View on Github external
const recommendation = recommendationToStable(RECOMMENDATION_BAN);
                const ruleContent = {
                    entity: server,
                    recommendation,
                    reason: reason,
                };
                const stateKey = `rule:${ruleContent.entity}`;
                await mjolnir.client.sendStateEvent(list.roomId, ruleTypeToStable(RULE_SERVER), stateKey, ruleContent);
                importedRules++;
            }
        }
    }

    const message = `Imported ${importedRules} rules to ban list`;
    const reply = RichReply.createFor(roomId, event, message, message);
    reply['msgtype'] = "m.notice";
    await mjolnir.client.sendMessage(roomId, reply);
}