# 接続中の配信内のギフト/スパチャ一覧を取得したい

**URL:** <https://forum.onecomme.com/t/topic/1867>\
**Category:** 質問\
**Tags:** 解決済\
**Created:** [2024 年 9 月 4 日午後 2:37 UTC](https://forum.onecomme.com/t/topic/1867 "2024-09-04T14:37:57Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mittsun](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.onecomme.com/mittsun/32/2709_2.png) [@Mittsun](https://forum.onecomme.com/u/Mittsun)\
**Post date:** [2024 年 9 月 4 日午後 2:37 UTC](https://forum.onecomme.com/t/topic/1867/1 "2024-09-04T14:37:57Z")

</div>

## 対象

わんコメ / テンプレート

## 利用OS

Windows

## 利用バージョン: Windows、わんコメやOBSのバージョン

Windows: 11  
わんコメ： 6.0.0-rc.6  
OBS: 0.0.0

## PRO版を利用

している

## 過去ログ確認 ([検索はこちらから](https://forum.onecomme.com/search))

した

## ドキュメント確認 ([公式サイト](https://onecomme.com/docs/faq/))

した

## 内容

独自のテンプレートを開発中なのですが、その中でわんコメ本体のギフト/スパチャ欄データを取得したいです。具体的には、ユーザごとにギフト・スパチャをそれぞれ累計いくらくれているかを画面に表示しようと思っています。今までの全てではなく、その時接続している配信内での累計です。

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/onecomme/original/2X/5/5bcf5003da6b2698012577e2158dfb04407948e4.png)

HTTP APIのSearch Comment JSON File (from 5.0.0)に検索パラメータの種類を説明しているらしいURLがありますが( [https://github.com/auditassistant/json-query](https://github.com/auditassistant/json-query) )、リンク先がnotfoundでした。「[http://localhost:11180/api/comments/search」に特定のパラメータを加えればギフト/スパチャ欄のみ取得可能なのでしょうか？](http://localhost:11180/api/comments/search%E3%80%8D%E3%81%AB%E7%89%B9%E5%AE%9A%E3%81%AE%E3%83%91%E3%83%A9%E3%83%A1%E3%83%BC%E3%82%BF%E3%82%92%E5%8A%A0%E3%81%88%E3%82%8C%E3%81%B0%E3%82%AE%E3%83%95%E3%83%88/%E3%82%B9%E3%83%91%E3%83%81%E3%83%A3%E6%AC%84%E3%81%AE%E3%81%BF%E5%8F%96%E5%BE%97%E5%8F%AF%E8%83%BD%E3%81%AA%E3%81%AE%E3%81%A7%E3%81%97%E3%82%87%E3%81%86%E3%81%8B%EF%BC%9F)  
[わんコメ HTTP API](https://documenter.getpostman.com/view/20406518/2s9Y5SX6EE#4cc8dbd6-685d-445d-9242-6e9f390cb8d2)

 ![image](https://canada1.discourse-cdn.com/flex031/uploads/onecomme/original/2X/9/948efbd3e0682f27b9971d28c7c1c1724691a4e5.png)

---

<div class="post-metadata">

**Author:** ![pintocuru](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.onecomme.com/pintocuru/32/1785_2.png) [@pintocuru](https://forum.onecomme.com/u/pintocuru)\
**Post date:** [2024 年 9 月 4 日午後 5:27 UTC](https://forum.onecomme.com/t/topic/1867/2 "2024-09-04T17:27:04Z")

</div>

こんにちは。  
わんコメであれば、OneSDKが使えるので、それを使った取得方法を。  
script.js はVueで書かれてますが、  
data には filteredComments: []  
methodsに下記のような関数 async fetchData() を入れてみて下さい  
（コードはChatGPTに書いてもらいました。十分なテストは行ってないので、もし何らかのエラーが出たらすみません💦）

```auto
data() {
    return {
      comments: [],
      filteredComments: [] // フィルター済みのコメントを格納するプロパティ
    };
  },
  methods: {
    getClassName(comment) {
      if (comment.commentIndex % 2 === 0) {
        return 'comment even';
      }
      return 'comment odd';
    },
    getStyle(comment) {
      return OneSDK.getCommentStyle(comment);
    },
    async fetchData() {
      try {
        // 非同期リクエストを実行し、結果をフィルター済みのコメントに格納
        this.filteredComments = await OneSDK.post("http://localhost:11180/api/comments/search", {
          "comments": ["hasGift=true"]
        });
        
        // リクエスト結果をコンソールに出力
        console.log(this.filteredComments);
        
        // 他の処理が必要な場合はここで行う
        // 例: this.someState = this.filteredComments;

      } catch (error) {
        // エラーが発生した場合の処理
        console.error('Error:', error);
      }
    }
  },

```

そして、 mounted 内で this.fetchData() を動かせば、  
filteredCommentsに「スパチャ」のみのコメントが入るはず・・です  
（this.fetchData() は、OneSDK.setup より後で使います）

スパチャの金額を合計するには、  
methods に以下の関数を追加して下さい  
この calculateTotalPrice() を mounted やhtmlで使えます。どうでしょうか。

```auto
    calculateTotalPrice() {
      // filteredComments内の全てのdata.priceを合計
      return this.filteredComments.reduce((total, comment) => {
        // comment.data.priceが存在しない場合は0を加算
        return total + (comment.data.price || 0);
      }, 0);
    }

```

---

<div class="post-metadata">

**Author:** ![Mittsun](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.onecomme.com/mittsun/32/2709_2.png) [@Mittsun](https://forum.onecomme.com/u/Mittsun)\
**Post date:** [2024 年 9 月 5 日午前 9:54 UTC](https://forum.onecomme.com/t/topic/1867/3 "2024-09-05T09:54:29Z")

</div>

pintocuru様

ご回答ありがとうございます、  
this.filteredComments = await OneSDK.post(“[http://localhost:11180/api/comments/search](http://localhost:11180/api/comments/search)”, {  
“comments”: [“hasGift=true”]  
});  
が特に知りたかった部分でした、ありがとうございます。  
これらをもとに自分で色々改変した結果、メンギフも含めて必要な値の合算ができることを確認できました！

---

<div class="post-metadata">

**Author:** ![sikozon](https://yyz1.discourse-cdn.com/flex031/user_avatar/forum.onecomme.com/sikozon/32/77_2.png) [@sikozon](https://forum.onecomme.com/u/sikozon)\
**Post date:** [2024 年 9 月 18 日午後 6:06 UTC](https://forum.onecomme.com/t/topic/1867/4 "2024-09-18T18:06:42Z")

</div>

トピッククローズ：質問解決済のため
