Skip to content

Instantly share code, notes, and snippets.

@book000
Last active September 5, 2019 05:43
Show Gist options
  • Save book000/1328a254707cf8ff2becb3308d9c26e6 to your computer and use it in GitHub Desktop.
Save book000/1328a254707cf8ff2becb3308d9c26e6 to your computer and use it in GitHub Desktop.
<?php
// https://tomachi.jpn.ph/blog/discordmessagesend/
function DiscordMessageSend($channelid, $message, $embed = null){
$token = "TOKEN";
$data = array(
"content" => $message,
"embed" => $embed
);
$header = array(
"Content-Type: application/json",
"Content-Length: ".strlen(json_encode($data)),
"Authorization: Bot ".$token,
"User-Agent: DiscordBot (http://example.com, v0.0.1)"
);
$context = array(
"http" => array(
"method" => "POST",
"header" => implode("\r\n", $header),
"content" => json_encode($data)
)
);
$context = stream_context_create($context);
$contents = file_get_contents("https://discordapp.com/api/channels/".$channelid."/messages", FALSE, $context );
}
// https://github.com/book000/etc/blob/master/DiscordEmbed.class.php
require_once("DiscordEmbed.class.php");
$json = file_get_contents("php://input"); // ポストされたデータを取得
$json = str_replace("\n", "\\n", $json); // 改行を修正
$data = json_decode($json, true); // jsonをオブジェクトに変換
if ($data["appname"] != "NHK KOUHAKU") {
exit; // アプリ名がNHK KOUHAKUでなければ終了
}
$message = $data["message"]; // メッセージ取得
$messages = explode("\n", $message); // 改行で分割
$message = $messages[0]; // メッセージの一行目を取得
if (mb_substr($message, 0, 4) != "まもなく") {
exit; // 「まもなく」という文字列から始まらなければ終了
}
// DiscordEmbed作成
$embed = new DiscordEmbed();
$embed->setTitle("紅白歌合戦");
$embed->setUrl("https://www.nhk.or.jp/kouhaku/");
$embed->setDescription("紅白歌合戦の情報をお伝えします。\n(NHK紅白アプリでの通知から自動投稿)");
$embed->addFields("情報", $message, false);
DiscordSendSelect("CHANNEL ID", "", $embed->Export());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment