Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fajnyCreeper committed Nov 30, 2019
0 parents commit 4fbbaa2
Show file tree
Hide file tree
Showing 10 changed files with 890 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SEContestChatCommands.credentials.php
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# SEContestCommands
Chat commands for StreamElements' Contests
15 changes: 15 additions & 0 deletions SEContestChatCommands.active.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
function GetActiveId($channel, $bearer)
{
$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
return $result_array["active"]["_id"];
}
15 changes: 15 additions & 0 deletions SEContestChatCommands.close.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
function CloseContest($channel, $bearer, $contestId)
{
$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel."/".$contestId."/close");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);

return $result;
}
30 changes: 30 additions & 0 deletions SEContestChatCommands.latest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
function GetLatestId($channel, $bearer)
{
$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
return $result_array["contests"][0]["_id"];
}

function GetLatestOptions($channel, $bearer)
{
$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
return $result_array["contests"][0]["options"];
}
47 changes: 47 additions & 0 deletions SEContestChatCommands.open.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
function OpenContest($channel, $bearer, $data)
{
/*$data = array(
"botResponses" => false,
"title" => "Test",
"minBet" => 10,
"maxBet" => 10000,
"duration" => 15,
"options" => array(
array(
"title" => "AAA",
"command" => "aaa"
),
array(
"title" => "BBB",
"command" => "bbb"
)
)
);*/
$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);
$result_array = json_decode($result, true);
$contestId = $result_array["_id"];



$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel."/".$contestId."/start");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);

return $result;
}
1 change: 1 addition & 0 deletions SEContestChatCommands.option.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
86 changes: 86 additions & 0 deletions SEContestChatCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/*
Credentials structure:
$key = "random string";
$channel = "SE channelID";
$bearer = "SE JWT token";
*/
require_once("SEContestChatCommands.credentials.php");

if (isset($_GET["key"], $_GET["action"]) && $_GET["key"] == $key)
{
require_once("SEContestChatCommands.open.php");
require_once("SEContestChatCommands.active.php");
require_once("SEContestChatCommands.close.php");
require_once("SEContestChatCommands.latest.php");
require_once("SEContestChatCommands.pick.php");

switch(strtolower($_GET["action"]))
{
case "start":
if (isset($_GET["name"], $_GET["duration"], $_GET["options"]) && $_GET["name"] != "" && $_GET["duration"] != "" && $_GET["options"] != "")
{
$duration = 10;
try
{
$duration = intval($_GET["duration"]);
}
catch (\Exception $e)
{
$duration = 15;
}

$name = str_replace("_", " ", $_GET["name"]);
$optionsRaw = preg_split('/ /', $_GET["options"]);
unset($value);
$options = array();
foreach($optionsRaw as $key => $value)
{
$options[$key] = array("title" => str_replace("_", " ", $value), "command" => strtolower($value));
}
unset($value);

$data = array(
"botResponses" => false,
"title" => "$name",
"minBet" => 10,
"maxBet" => 10000,
"duration" => $duration,
"options" => $options
);

OpenContest($channel, $bearer, $data);
}
else
echo "Wrong format! Expected !bets start Bets_title duration Option_1 Option_2 ...";
break;

case "close":
CloseContest($channel, $bearer, GetActiveId($channel, $bearer));
break;

case "draw":
if (isset($_GET["winner"]) && trim($_GET["winner"]) != "")
{
$winnerText = str_replace(" ", "_", trim(strtolower($_GET["winner"])));
$latest = GetLatestOptions($channel, $bearer);
$winnerId;
foreach ($latest as $option)
{
if ($option["command"] == $winnerText)
$winnerId = $option["_id"];
}
PickWinningOption($channel, $bearer, GetLatestId($channel, $bearer), $winnerId);
}
else
echo "Wrong format! Expected !bets draw winningOption";
break;

default:
echo "Invalid action! Expected: start|close|draw";
}
}
else
{
echo "Something went wrong!";
}
19 changes: 19 additions & 0 deletions SEContestChatCommands.pick.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
function PickWinningOption($channel, $bearer, $contestId, $winnerId)
{
$data = array(
"winnerId" => "$winnerId"
);
$ch = curl_init("https://api.streamelements.com/kappa/v2/contests/".$channel."/".$contestId."/winner");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: application/json',
'Content-Type: application/json',
'Authorization: Bearer '.$bearer)
);
$result = curl_exec($ch);

return $result;
}

0 comments on commit 4fbbaa2

Please sign in to comment.