vzgcoders / twitchphp
此软件包的最新版本(2.0.2)没有提供许可信息。
一个非官方的API,用于与Twitch使用的IRC聊天服务交互。
2.0.2
2022-10-24 16:49 UTC
Requires
- evenement/evenement: ^3.0 || ^2.0
- monolog/monolog: ^3.2
- react/event-loop: ^1.3
- react/socket: ^1.11
- react/stream: ^1.1
Suggests
- ext-event: For a faster, and more performant loop
- ext-libev: For a faster, and more performant loop
- ext-mbstring: For accurate calculations of string length when handling non-english characters.
- ext-uv: For a faster, and more performant loop. PHP >=7 only. Preferred.
- team-reflex/discord-php: Required to relay chat to Discord
README
一个使用ReactPHP构建的聊天自机器人,用于官方Twitch TV互联网中继聊天(IRC)界面。
开始之前
在开始使用此库之前,您需要了解PHP的工作原理,您需要了解语言,您需要了解事件循环和承诺的工作原理。这是开始前的基本要求。没有这些知识,您只会遭受。
常见问题解答
- 我可以在Web服务器(例如Apache、nginx)上运行TwitchPHP吗?
- 不,TwitchPHP只能在CLI中运行。如果您想为您的机器人提供一个界面,可以将react/http与您的机器人集成并通过CLI运行。
- PHP内存不足?
- 尝试使用
ini_set('memory_limit', '-1');增加内存限制。
- 尝试使用
入门
要求
- PHP 8
- 此库正在考虑PHP8支持。不再支持PHP7,但可以轻松地分叉并修改以实现此功能。
- Composer
- DiscordPHP(可选)
Windows和SSL
遗憾的是,Windows上的PHP无法访问Windows证书存储。这是一个问题,因为TLS被使用,因此证书验证被应用(关闭此选项不是一种选择)。
您将注意到此问题,您的脚本在经过一次循环后立即退出而没有任何错误。遗憾的是,没有错误或异常。
因此,此库的用户需要从cURL网站下载证书颁发机构提取。
必须将caextract的路径设置为php.ini中的openssl.cafile。
推荐扩展
- 最新的PHP版本。
- 一个
ext-uv(首选)、ext-libev或evt-event扩展,用于更快的、更高效的事件循环。 - 如果处理非英语字符,则需要
ext-mbstring。
安装TwitchPHP
TwitchPHP使用Composer安装。
- 运行
composer require VZGCoders/TwitchPHP。这将安装最新版本。 - 在主文件顶部包含Composer自动加载文件
include __DIR__.'/vendor/autoload.php';
- 创建一个机器人!
配置
- 添加所需的"secret"和"nick"值。
- 自定义您的命令和响应。
基本示例
<?php ignore_user_abort(1); set_time_limit(0); // Don't time out the script ini_set('max_execution_time', 0); // Don't time out the script ini_set('memory_limit', '-1'); //Unlimited memory usage require 'vendor/autoload.php'; $nick = 'ValZarGaming'; // Twitch username (Case sensitive) //$loop = \React\EventLoop\Loop::get(); $logger = new Monolog\Logger('New logger'); $logger->pushHandler(new Monolog\Handler\StreamHandler('php://stdout')); require 'secret.php'; //$secret $options = array( //Required 'secret' => $secret, // Client secret 'nick' => $nick, // Twitch username //Optional //'discord' => $discord, // Pass your own instance of DiscordPHP (https://github.com/discord-php/DiscordPHP) //'discord_output' => true, // Output Twitch chat to a Discord server's channel //'loop' => $loop, // Pass your own instance of $loop to share with other ReactPHP applications 'socket_options' => [ 'dns' => '8.8.8.8', // Can change DNS provider ], 'verbose' => true, // Additional output to console (useful for debugging TwitchPHP) 'debug' => false, // Additional output to console (useful for debugging communications with Twitch) 'logger' => $logger, //Custom commands 'commandsymbol' => [ // Process commands if a message starts with a prefix in this array "@$nick", //Users can mention your channel instead of using a command symbol prefix '!s', ], 'whitelist' => [ // Users who are allowed to use restricted functions strtolower($nick), 'shriekingechodanica', ], 'badwords' => [ // List of blacklisted words or phrases in their entirety; User will be immediately banned with reason 'badword' if spoken in chat 'Buy followers, primes and viewers', 'bigfollows . com', 'stearncomminuty', 'Get viewers, followers and primes on', ], 'responses' => [ // Whenever a message is sent matching a key and prefixed with a command symbol, reply with the defined value 'ping' => 'Pong!', 'github' => 'https://github.com/VZGCoders/TwitchPHP', 'discord' => 'https://discord.gg/NU4BS5P36g', ], 'functions' => [ // Enabled functions usable by anyone 'help', // Send a list of commands as a chat message ], 'restricted_functions' => [ // Enabled functions usable only by whitelisted users 'join', //Joins another user's channel 'leave', //Leave the current user's channel 'ban', // Ban someone from the channel, takes a username and an optional reason ], 'private_functions' => [ // Enabled functions usable only by the bot owner sharing the same username as the bot 'stop', //Kills the bot 'php', //Outputs the current version of PHP as a message ], ); //include 'commands.php'; //$options['commands'] => $commands; // Import your own Twitch/Commands object to add additional functions //Twitch channels to join that do not need to be relayed to Discord, formatted ['channels']['twitch_username'][''] = '' $twitch_options['channels']['shriekingechodanica'][''] = ''; //Twitch channels to join and relay chat for, formatted ['channels']['twitch_username']['discord_guild_id'] = 'discord_channel_id' $twitch_options['channels']['shriekingechodanica']['923969098185068594'] = '924019611534503996'; $twitch = new Twitch\Twitch($options); $twitch->run(); ?>
DiscordPHP示例
在$discord->on('message'函数中,使用use ($twitch, $twitch_relay)
$message_content = $message->content; $message_content_lower = strtolower($message->content); if ($message->user_id != $discord->id) $twitch_relay($message, $message_content, $message_content_lower); if (str_starts_with($message_content_lower, 'join #')) return $twitch->joinChannel(trim(str_replace('join #', '', $message_content_lower)), $message->guild_id, $message->channel_id); if (str_starts_with($message_content_lower, 'leave #')) return $twitch->leaveChannel(trim(str_replace('leave #', '', $message_content_lower)), $message->guild_id, $message->channel_id);
$twitch_relay = function ($message, string $message_content, string $message_content_lower) use ($discord, $twitch): void { if ($channels = $twitch->getChannels()) foreach ($channels as $twitch_channel => $arr) foreach ($arr as $guild_id => $channel_id) { if (!($message->guild_id == $guild_id && $message->channel_id == $channel_id)) continue; $channel = ''; if (str_starts_with($message_content_lower, "#$twitch_channel")) { $message_content = trim(substr($message_content, strlen("#$twitch_channel"))); $channel = $twitch_channel; } //else $channel = $twitch->getLastChannel(); //Only works reliably if only relaying chat for a single Twitch chat if (! $channel) continue; if (! $twitch->sendMessage("{$message->author->displayname} => $message_content", $channel)) $twitch->logger->warning('[FAILED TO SEND MESSAGE TO TWITCH]'); } };
文档
原始文档可以在代码中找到。