<commit_before><?php
namespace HedgeBot\Plugins\StreamControl;

use HedgeBot\Core\Plugins\Plugin;
use HedgeBot\Core\Events\CommandEvent;
use HedgeBot\Core\API\IRC;
use HedgeBot\Core\API\Tikal;
use HedgeBot\Core\API\Twitch;
use HedgeBot\Core\HedgeBot;

class StreamControl extends Plugin
{
    /**
     * Plugin initialization.
     */
    public function init()
    {
        // Don't load the API endpoint if we're not on the main environment
        if (ENV == "main") {
            Tikal::addEndpoint('/plugin/streamcontrol', new StreamControlEndpoint($this));
        }

    }

    /**
     * Command: sets the stream title.
     */
    public function CommandSetTitle(CommandEvent $ev)
    {
        $args = $ev->arguments;
        if (count($args) < 1) {
            return IRC::reply($ev, "Insufficient parameters.");
        }

        $title = join(' ', $args);
        Twitch::getClient()->channels->update($ev->channel, ['status' => $title]);
        IRC::reply($ev, "Stream title changed to: ". $title);
    }

    /**
     * Sets the stream game.
     */
    public function CommandSetGame(CommandEvent $ev)
    {
        $args = $ev->arguments;
        if (count($args) < 1) {
            return IRC::reply($ev, "Insufficient parameters.");
        }

        // Lookup for the game using the Twitch search API
        $gameSearch = join(' ', $args);

        HedgeBot::message("Game change requested: $0", [$gameSearch]);
        $closest = $this->resolveGameName($gameSearch);
        
        if(empty($closest)) {
            return IRC::reply($ev, "No matching game found.");
        }

        Twitch::getClient()->channels->update($ev->channel, ['game' => $closest]);
        IRC::reply($ev, "Stream game changed to: ". $closest);
    }

    /**
     * Starts a raid on the given channel.
     */
    public function CommandRaid(CommandEvent $ev)
    {
        $args = $ev->arguments;
        if (count($args) < 1) {
            return IRC::reply($ev, "Insufficient parameters.");
        }

        IRC::message($ev->channel, ".raid ". $args[0]);
    }

    /**
     * Gets the stream info for the given channel from the Twitch API.
     * 
     * @param string $channel The channel to get info from.
     * @return array|null The channel info, as an array, or null if an error occured.
     */
    public function getChannelInfo(string $channel)
    {
        $channelInfo = Twitch::getClient()->channels->info($channel);
        
        if($channelInfo != false) {   
            return (array) $channelInfo;
        }

        return null;
    }

    /**
     * Sets the stream info for the given channel via the Twitch API.
     * 
     * @param string $channel The channel to update the info of.
     * @param string $title The new strean title.
     * @param string $category The new stream category.
     * @return array|bool The new channel info, or False if the request didn't succeed.
     */
    public function setChannelInfo(string $channel, string $title, string $category)
    {
        $resolvedCategory = $this->resolveGameName($category);

        if(empty($resolvedCategory)) {
            return false;
        }

        $update = Twitch::getClient()->channels->update($channel, ['status' => $title, 'game' => $resolvedCategory]);

        return $update;
    }<commit_msg>Updated StreamControl plugin for commands to use the internal methods.<commit_after><?php
namespace HedgeBot\Plugins\StreamControl;

use HedgeBot\Core\Plugins\Plugin;
use HedgeBot\Core\Events\CommandEvent;
use HedgeBot\Core\API\IRC;
use HedgeBot\Core\API\Tikal;
use HedgeBot\Core\API\Twitch;
use HedgeBot\Core\HedgeBot;

class StreamControl extends Plugin
{
    /**
     * Plugin initialization.
     */
    public function init()
    {
        // Don't load the API endpoint if we're not on the main environment
        if (ENV == "main") {
            Tikal::addEndpoint('/plugin/streamcontrol', new StreamControlEndpoint($this));
        }

    }

    /**
     * Command: sets the stream title.
     */
    public function CommandSetTitle(CommandEvent $ev)
    {
        $args = $ev->arguments;
        if (count($args) < 1) {
            return IRC::reply($ev, "Insufficient parameters.");
        }

        $title = join(' ', $args);
        $updatedInfo = $this->setChannelInfo($ev->channel, $title);
        
        if(!empty($updatedInfo)) {
            IRC::reply($ev, "Stream title changed to: ". $title);
        } else {
            IRC::reply($ev, "Failed updating stream title.");
        }
    }

    /**
     * Sets the stream game.
     */
    public function CommandSetGame(CommandEvent $ev)
    {
        $args = $ev->arguments;
        if (count($args) < 1) {
            return IRC::reply($ev, "Insufficient parameters.");
        }

        // Lookup for the game using the Twitch search API
        $gameSearch = join(' ', $args);
        $updatedInfo = $this->setChannelInfo($ev->channel, null, $gameSearch);

        if(!empty($updatedInfo)) {
            IRC::reply($ev, "Stream game changed to: ". $updatedInfo->game);
        } else {
            IRC::reply($ev, "Failed updating stream game.");
        }        
    }

    /**
     * Starts a raid on the given channel.
     */
    public function CommandRaid(CommandEvent $ev)
    {
        $args = $ev->arguments;
        if (count($args) < 1) {
            return IRC::reply($ev, "Insufficient parameters.");
        }

        IRC::message($ev->channel, ".raid ". $args[0]);
    }

    /**
     * Gets the stream info for the given channel from the Twitch API.
     * 
     * @param string $channel The channel to get info from.
     * @return array|null The channel info, as an array, or null if an error occured.
     */
    public function getChannelInfo(string $channel)
    {
        $channelInfo = Twitch::getClient()->channels->info($channel);
        
        if($channelInfo != false) {   
            return (array) $channelInfo;
        }

        return null;
    }

    /**
     * Sets the stream info for the given channel via the Twitch API.
     * The caller must at least mention either a new title or a new category.
     * 
     * @param string $channel The channel to update the info of.
     * @param string $title The new strean title.
     * @param string $category The new stream category.
     * @return array|bool The new channel info, or False if the request didn't succeed.
     */
    public function setChannelInfo(string $channel, string $title = null, string $category = null)
    {
        $updateParams = [];
        
        if(empty($title) && empty($category)) {
            return false;
        }
        
        if(!empty($category)) {
            $resolvedCategory = $this->resolveGameName($category);

            if(empty($resolvedCategory)) {
                return false;
            }

            $updateParams['game'] = $resolvedCategory;
        }

        if(!empty($title)) {
            $updateParams['status'] = $title;
        }

        $update = Twitch::getClient()->channels->update($channel, $updateParams);
        return $update;
    }
{'fraction_non_alphanumeric': 0.13114523172277787, 'fraction_numerical': 0.0012677841949570363, 'mean_word_length': 2.1895777178796045, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2963742', 'n_tokens_mistral': 2031, 'n_tokens_neox': 1972, 'n_words': 564}