[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。
ただいまコメントを受けつけておりません。
「Discord の bot を衝動的に作り始めるにはプログラミング環境を整えるのが少し大変なのでは?」からの「Windows 標準機能だけでできればいいじゃん!」がきっかけだった。なお、Mac とか Linux とか使っているユーザは自前でより良い方法を使えるだろう、と考えているので知らないのである。
結論として挫折した。以下のようなものを書いてみたが、これ以上は無理。しかも結局 jq を入れた。jq コマンドはhttps://stedolan.github.io/jq/から入手。
@echo off
setlocal enabledelayedexpansion
set botCommand=ボットのコマンド名
set botLength=6
set token=Tokenの情報
set channel=チャンネルのID
curl -X GET -H "Authorization:Bot %token%" https://discordapp.com/api/channels/%channel%/messages > tmp_log
for /l %%n in (0,0,0) do (
type tmp_log | jq .[0].id > tmp_lastId
for /f %%a in (tmp_lastId) do (
if "%%a" neq "null" (
set messageId=%%a
set messageId=!messageId:"=!
set url=https://discordapp.com/api/channels/%channel%/messages?after=!messageId!
call echo !url! > url
) else (
echo skipped
)
for /f "usebackq" %%a in (`findstr %botCommand% tmp_log`) do (
echo %%a%
if "%%a"=="" (
echo not found
) else (
set /a dice=!random!*%botLength%/32767
echo !dice!
if "!dice!"=="0" (
curl -X POST -H "Content-Type:application/json" -H "Authorization:Bot %token%" -d "{\"content\": \"0だったよ\"}" https://discordapp.com/api/channels/%channel%/messages
)
if "!dice!"=="1" (
curl -X POST -H "Content-Type:application/json" -H "Authorization:Bot %token%" -d "{\"content\": \"1だったよ\"}" https://discordapp.com/api/channels/%channel%/messages
)
if "!dice!"=="2" (
curl -X POST -H "Content-Type:application/json" -H "Authorization:Bot %token%" -d "{\"content\": \"2だったよ\"}" https://discordapp.com/api/channels/%channel%/messages
)
if "!dice!"=="3" (
curl -X POST -H "Content-Type:application/json" -H "Authorization:Bot %token%" -d "{\"content\": \"3だったよ\"}" https://discordapp.com/api/channels/%channel%/messages
)
if "!dice!"=="4" (
curl -X POST -H "Content-Type:application/json" -H "Authorization:Bot %token%" -d "{\"content\": \"4だったよ\"}" https://discordapp.com/api/channels/%channel%/messages
)
if "!dice!"=="5" (
curl -X POST -H "Content-Type:application/json" -H "Authorization:Bot %token%" -d "{\"content\": \"5だったよ\"}" https://discordapp.com/api/channels/%channel%/messages
)
)
)
for /f %%b in (url) do (
timeout /t 10 /nobreak > nul
curl -X GET -H "Authorization:Bot %token%" %%b% > tmp_log
)
)
)
問題点は以下の3つ。
遅延変数がもうちょっとわかりやすくなり、for や if が使いやすくなり、json を強引にでもパースできるようになればもうちょっとやれるかもしれないけど……
半年ほど前の Windows 10 の更新で curl コマンドや ssh コマンドなどがコマンドプロンプトからも使えるようになったので「やれるかも」と思ったのだがまだちょっと早かった。
ただいまコメントを受けつけておりません。