0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Dead by Daylightのランキングデータを取得するDiscordbotをpythonで作成してみた

Last updated at Posted at 2025-01-13

年末年始でゆっくりしたおかげか、dbdのモチベ(正確にはナースやるモチベ)が結構あります。

んでせっかくならたまに見かける世界ランキング○○位とかのやつで自分が何位なのか確認したいなぁと思ったので、
ランキングデータが確認できるサイト(https://dbd.tricky.lol/) を見たけど、自分で今何位なのか数えなきゃいけないのが結構めんどくさい・・・・・

なのでpythonの練習がてら、Discord上でコマンド実行すれば、「今何位だよ!」っていうのが分かるbotを作ってみた。

まずはAPIの確認

https://dbd.tricky.lol/apidocs/
ここにAPI仕様について記載があった。

リーダーボードの順位
https://dbd.tricky.lol/api/leaderboardposition?steamid=<steamid64>&stat=escaped
特定のプレイヤーと統計のリーダーボードの位置を返します。

って記載があるけど、stat=escapedの部分をどう変えればいいのか記載がない:worried:

→ページのソースをみると、「data-sort=」の部分がわりとそれっぽい気がする
image.png

ので、ナースの場合「blinkattacks」を指定
※結果これであってそうだった

次にDiscordbotの作成と必要なライブラリのインストールだけど、色んなところにやり方書いた記事があるので割愛

discordbotの作成/設定部分についてはこの方の記事が結構わかりやすかったです
https://hibari-c.com/blog/240701/

必要なライブラリについては、以下コマンドでdiscord.pyをインストールすればおk

#Macの場合

python3 -m pip install -U discord.py

#Windowsの場合

py -3 -m pip install -U discord.py

作成したpyファイル

import discord
from discord.ext import commands
import requests

TOKEN = "DummYTOK4N"  # 取得したトークンを入力
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix="!", intents=intents) # コマンドの接頭辞を指定."?"等でもよい

@bot.command()
async def stats(ctx, steamid: str = None):

    stat_type = "blinkattacks"  # 統計の種類を指定
    api_url = f"https://dbd.tricky.lol/api/leaderboardposition?steamid={mysteamid}&stat={stat_type}"  # APIエンドポイント.{mysteamid}には自分のsteamIDを入力(765から始まるやつ)

    try:
        # APIを呼び出してデータを取得
        response = requests.get(api_url)
        response.raise_for_status()  # エラーがあれば例外を発生

        data = response.json()

        # 結果を整形して表示
        stat_value = data.get("value", "N/A")  # 統計の値
        position = data.get("position", "N/A")  # ランクの位置

        embed = discord.Embed(
            title="統計情報",
            description=f"SteamID: XXX", # 自分のsteamIDを入力.
            color=0x1abc9c
        )
        embed.add_field(name="統計タイプ", value=stat_type, inline=True)
        embed.add_field(name="回数", value=stat_value, inline=True)
        embed.add_field(name="ランキング", value=position, inline=True)
        embed.set_footer(text="データはDBDStats APIから取得")

        await ctx.send(embed=embed)

    except requests.exceptions.RequestException as e:
        await ctx.send("統計情報の取得中にエラーが発生しました。")
        print(f"Error: {e}")
    except KeyError:
        await ctx.send("予期しないデータ形式が返されました。APIの構成を確認してください。")

bot.run(TOKEN) # Botの起動

◎補足

 stat_type = "blinkattacks"

の部分を変えれば、好きなキラーとか好きな情報のランキングが取得できる

使い方

①特定のチャンネルで、「!stats」と入力する
②botが統計情報を返してくれる
GhJjF1-a0AAtBoU.png

今は124位なことが分かりました。2桁になりたいな

※2025/02/20追記
image.png

2桁になりました。実力が伴ってないのでガンバロウ

地味に困ったとこ

1.よしできたぞ!と思って実行したら、

WARNING  discord.ext.commands.bot Privileged message content intent is missing, commands may not work as expected.

というエラー(BotがMessage Content Intentを有効にしていない)が出て、Discord Developer PortalでMessage Content Intentの権限を付与しても治らなかった

よくよくコードを見返すと、

intents.messages = True

と書いていた...

intents.message_content = True

に修正して、解決!

2.謎のモジュールエラー

ModuleNotFoundError: No module named 'audioop'

というエラーがなぜか出ていた。本来ならpython標準ライブラリに含まれているらしい。

pip install audioop-lts

を実行すれば解決した。

まとめと今後の目標

  • APIの仕様がわかれば、必要な情報を指定して記述すればデータを取得することができた。
  • 毎回botをオンラインにするためだけに、つくったpyファイルを実行するのがめんどう。定期実行のやり方とか考えてみる。できれば無料がいいな
  • 応用すればTwitchとかでたまに見るコメントでコマンド打てば順位とか統計情報を返してくれるやつとかも作れそう。配信とかはやってないので自分で使うことはないけど。
0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?