CommunityFilter
CommunityFilter は、指定したコミュニティ ID に関連するイベントのみを内部のハンドラに渡す EventHandler ミドルウェアです。複数のコミュニティにインストールされた Plugin で、特定のコミュニティだけを処理したい場合に便利です。
インストール
Section titled “インストール”追加インストールは不要です。mixi2-js に同梱されています。
// ESMimport { CommunityFilter } from 'mixi2-js/helpers';// CJSconst { CommunityFilter } = require('mixi2-js/helpers');import { CommunityFilter } from '@otoneko1102/mixi2-js/helpers';- コミュニティ ID を持つイベント(
PostCreatedEventのcommunityId、CommunityMemberChangedEvent、CommunityPluginManagedEvent)は、指定した ID と一致した場合のみ内部ハンドラに渡されます - PING など、コミュニティと無関係なイベントはそのまま通過します
基本的な使い方
Section titled “基本的な使い方”import { EventType, StreamWatcher } from 'mixi2-js';import { EventRouter, CommunityFilter } from 'mixi2-js/helpers';import { EventType, StreamWatcher } from '@otoneko1102/mixi2-js';import { EventRouter, CommunityFilter } from '@otoneko1102/mixi2-js/helpers';const router = new EventRouter();
router.on(EventType.POST_CREATED, async (event) => { // community-id-1 のポストのみここに到達する console.log("コミュニティのポスト:", event.postCreatedEvent?.post?.text);});
// 特定のコミュニティのみ処理するconst filter = new CommunityFilter(router, ['community-id-1']);
const watcher = new StreamWatcher({ client, handler: filter });await watcher.watch();複数コミュニティの場合
Section titled “複数コミュニティの場合”// 複数のコミュニティ ID を指定できるconst filter = new CommunityFilter(router, [ 'community-id-1', 'community-id-2',]);EventDeduplicator と組み合わせる
Section titled “EventDeduplicator と組み合わせる”import { EventRouter, CommunityFilter, EventDeduplicator } from 'mixi2-js/helpers';
const router = new EventRouter();const filter = new CommunityFilter(router, ['community-id-1']);const dedup = new EventDeduplicator(filter);
// Webhook でも重複排除 + コミュニティフィルタが機能するconst server = new WebhookServer({ handler: dedup, secret: '...' });API リファレンス
Section titled “API リファレンス”コンストラクタ
Section titled “コンストラクタ”new CommunityFilter(handler: EventHandler, communityIds: string[])| 引数 | 型 | 説明 |
|---|---|---|
handler | EventHandler | フィルタ通過後にイベントを渡すハンドラ |
communityIds | string[] | 通過を許可するコミュニティ ID の一覧 |
| メソッド | 説明 |
|---|---|
handle(event) | EventHandler インターフェースの実装。フィルタリングを行う |