LoginSignup
4
6

More than 5 years have passed since last update.

DiscordのBotをC#でサーモンランのステージ情報を取得してみる

Last updated at Posted at 2018-02-22

DiscordのBotをC#でサーモンランのステージ情報を取得してみる

開発環境

  • Visual Studio 2017 (C#)
  • Discord.Net 1.0.2
  • Microsoft.AspNet.WebApi.Client 5.2.4

初めに

前回、記事のDiscordのBotをC#で作ってみるでBotが動作するところまで確認しましたが、Spla2 APIを使いサーモランのステージ情報をBotが回答する部分を今回は、実装してみようと思います。

参考

Spla2 API
@otuhs_dさんの記事
Splatoon2チーム向けDiscord Bot『Roboty』を公開しました

実装後のイメージ

WS000026.JPG

Visual Studio

プロジェクトの作成

前回、作成した続きなので、プロジェクトの作成は、前回記事「DiscordのBotをC#で作ってみる」を確認してください。

Nugetから「Microsoft.AspNet.WebApi.Client 5.2.4」インストール

  • 「プロジェクトエクスプローラ」>「参照設定」を右クリック >「NuGetパッケージ管理」
  • 「参照」タブを選択 > 検索欄に「Microsoft.AspNet.WebApi.Client」を入力します。
  • 「Microsoft.AspNet.WebApi.Client」を選択します。
  • 最新の安定版を選択して「インストール」ボタンを押下します。

CoopExクラスの作成

  • サーモンランのステージ情報を格納するクラスです。
CoopEx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Splatoo2ConsoleApp
{
    public class CoopEx
    {
        public CoopResult[] result { get; set; }
    }

    public class CoopResult
    {
        public DateTime start { get; set; }
        public DateTime start_utc { get; set; }
        public int start_t { get; set; }
        public DateTime end { get; set; }
        public DateTime end_utc { get; set; }
        public int end_t { get; set; }
        public Stage stage { get; set; }
        public Weapon[] weapons { get; set; }
    }

    public class Stage
    {
        public string image { get; set; }
        public string name { get; set; }
    }

    public class Weapon
    {
        public int id { get; set; }
        public string image { get; set; }
        public string name { get; set; }
    }
}

Messagesクラスの作成

  • Messagesクラスは、前回作成しているので、新しくコマンドを追加するだけです。
  • 今回は、「.smn」とコメントすると、サーモンランのステージ情報を取得したいので、「smn」を作成します。
Messages.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord.Commands;
using System.Net.Http;
using System.Net.Http.Headers;

using Newtonsoft.Json;

namespace Splatoo2ConsoleApp
{
    public class Messages : ModuleBase
    {
        [Command("smn")]
        public async Task smn()
        {
            using (HttpClient client = new HttpClient())
            {

                var result = new List<CoopResult>();

                string messages = string.Empty;


                client.BaseAddress = new Uri("https://spla2.yuu26.com/");
                client.DefaultRequestHeaders.Accept.Clear();

                using (HttpResponseMessage response = client.GetAsync("coop/schedule").Result)
                {
                    if (response.IsSuccessStatusCode)
                    {
                        messages += "・サーモンラン\n";

                        var str = await response.Content.ReadAsStringAsync();
                        var config = JsonConvert.DeserializeObject<CoopEx>(str);

                        for (int i = 0; i < 2; i++)
                        {
                            messages += "```";
                            messages += "時間:\n";
                            messages += " " + config.result[i].start + "~" + config.result[i].end + "\n";
                            messages += "マップ:\n";
                            messages += " " + config.result[i].stage.name;
                            messages += "ブキ:\n";

                            foreach (var item in config.result[i].weapons)
                            {
                                messages += " " + item.name + "\n";
                            }
                            messages += "```\n";
                        }

                    }
                }
                await ReplyAsync(messages);
            }

        }
    }
}

作成完了!

  • ビルドを実行します。

WS000024.JPG

  • Discordで「.smn」コメントしてみると、Botがランダムで返してくれます。

WS000026.JPG

まとめ

  • NuGetにNewtonsoft.JsonMicrosoft.AspNet.WebApi.Clientがあるので、APIクライアントで接続して、Newton.Jsonでデシリアライズを行うことで比較的簡単に作成することができました。
4
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6