Skip to content

Commit 6c9a3e4

Browse files
authored
Merge pull request #29 from miamibc/pluin-changes
Plugin changes
2 parents 6dfcad6 + 84c2de1 commit 6c9a3e4

12 files changed

+478
-165
lines changed

composer.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
"ext-curl": "*",
1818
"ext-mbstring": "*",
1919
"ext-sockets": "*",
20+
"ext-pcntl": "*",
2021
"vlucas/phpdotenv": "^3.4",
2122
"alrik11es/cowsayphp": "^1.2",
2223
"league/html-to-markdown": "^4.9",
2324
"gabordemooij/redbean": "dev-master",
24-
"cheprasov/php-redis-client": "^1.10"
25+
"cheprasov/php-redis-client": "^1.10",
26+
"mnapoli/silly": "^1.7"
2527
},
2628
"autoload": {
2729
"psr-4": {
@@ -40,4 +42,4 @@
4042
"test": "php vendor/bin/phpunit",
4143
"test-github": "php vendor/bin/phpunit --testdox --exclude-group=exclude-from-github-test"
4244
}
43-
}
45+
}

joker.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
new Joker\Plugin\Log( ['file' =>'data/log/log.json'] ),
1515
new Joker\Plugin\Activity( ['sync_time' => 60] ),
16-
new Joker\Plugin\Kicker(),
16+
new Joker\Plugin\Kicker(['secons_with_emoji' => 0, 'secons_without_emoji' => 600]),
1717
new Joker\Plugin\Forwarder( [
1818
['from' => -343502518, 'text' => ['*покуп*'], 'to' => -343502518, ],
1919
['from' => -343502518, 'text' => ['*прода*', '*сдаё*'], 'to' => -343502518, 'forward' => false ],
@@ -47,7 +47,8 @@
4747

4848
// *** somethingg wide, without triggers, must stay in the end ***
4949

50-
new Joker\Plugin\Quote( ['dir' =>'data/jokes'] ),
50+
new Joker\Plugin\Quote(),
51+
new Joker\Plugin\QuoteAdmin(),
5152
new Joker\Plugin\Pasta( ['minimum_time' => 60 * 60] ),
5253
new Joker\Plugin\Beer( ['minimum_time'=>15*60] ),
5354

src/Bot.php

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Joker\Parser\Update;
1919
use Joker\Parser\User;
2020

21+
declare(ticks=1);
22+
2123
class Bot
2224
{
2325

@@ -53,6 +55,10 @@ public function __construct( $token, $debug = false )
5355
}
5456
echo "\nBot started: "; print_r( $this->me->getData() );
5557

58+
// intercept ctrl+c
59+
pcntl_signal(SIGINT, [$this,'quit']);
60+
pcntl_signal(SIGTERM, [$this,'quit']);
61+
5662
}
5763

5864
/**
@@ -204,6 +210,19 @@ public function plug( array $plugins )
204210
return $this;
205211
}
206212

213+
/** ctrl-c and ctrl+break event processing */
214+
public function quit( $event = null )
215+
{
216+
foreach ($this->plugins as $i=>$plugin)
217+
{
218+
$this->log("Unloading " . get_class($plugin));
219+
unset($this->plugins[$i]);
220+
}
221+
222+
echo "\nBuj :p";
223+
exit();
224+
}
225+
207226

208227
public function getMe()
209228
{

src/Parser/Text.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ public function __toString()
2828
return $this->text.'';
2929
}
3030

31-
public function token( $from = 0, $to = null)
31+
public function token($from = 0, $length = null)
3232
{
33-
return implode( " ", array_slice($this->data, $from, $to));
33+
return implode( " ", array_slice($this->data, $from, $length));
3434
}
3535

36-
public function line( $from, $to = null )
36+
public function line($from = 0, $length = null )
3737
{
3838
$text = explode("\n", $this->text);
39-
return implode( "\n", array_slice($text, $from, $to));
39+
return implode( "\n", array_slice($text, $from, $length));
4040
}
4141

4242
public function trigger()

src/Plugin/Activity.php

+26-9
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,46 @@ public function onEmpty( Update $update )
4545
{
4646
$message = $update->message();
4747

48-
// process only messages having `from` field
48+
// process only messages having `from`and chat field
4949
if ( !$user = $message->from() ) continue;
50+
if ( !$chat = $message->chat() ) continue;
5051

5152
// get custom data for this user from sqlite
52-
$custom = $user->getCustom();
53+
$userCustom = $user->getCustom();
54+
$chatCustom = $chat->getCustom();
5355

5456
// add info
55-
$custom->username = $user->username() ? $user->username() : null;
56-
$custom->name = $user->name() ? $user->name() : null;
57-
$custom->last_messsage_at = $message->date();
58-
$custom->last_messsage_id = $message->id();
57+
$userCustom->username = $user->username() ? $user->username() : null;
58+
$userCustom->name = $user->name() ? $user->name() : null;
59+
60+
$userCustom->last_messsage_at =
61+
$chatCustom->last_messsage_at = $message->date();
62+
63+
$userCustom->last_messsage_id =
64+
$chatCustom->last_messsage_id = $message->id();
5965

6066
if ($message->chat())
61-
$custom->last_messsage_chat_id = $message->chat()->id();
67+
{
68+
$userCustom->last_messsage_chat_id =
69+
$chatCustom->last_messsage_chat_id = $message->chat()->id();
70+
}
6271

6372
if ($message->text())
64-
$custom->all_text_length = $custom->all_text_length + strlen( $message->text().'' );
73+
{
74+
$userCustom->all_text_length += strlen($message->text().'');
75+
$chatCustom->all_text_length += strlen($message->text().'');
76+
}
6577

6678
if ($message->caption())
67-
$custom->all_text_length = $custom->all_text_length + strlen( $message->caption().'' );
79+
{
80+
$userCustom->all_text_length += strlen($message->caption().'');
81+
$chatCustom->all_text_length += strlen($message->caption().'');
82+
}
6883

6984
// save customer data to sqlite
7085
$user->saveCustom();
86+
$chat->saveCustom();
87+
7188
}
7289

7390
// clean pool and sync time

src/Plugin/Carma.php

+31-15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* - `clean_time` (false|integer, optional, default 10) - false, or seconds to remove carma exchange message
99
* - `power_time` (integer, optional, default 600) - number of seconds to have full power (1)
1010
* - `start_carma` (integer, optional, default 10) - points you start with
11+
* - `limit` (integer, optional, default 30) - number of results in carma top
1112
*
1213
* @package joker-telegram-bot
1314
* @author Sergei Miami <[email protected]>
@@ -24,9 +25,11 @@ class Carma extends Base
2425
{
2526

2627
protected
28+
$options = ['clean_time' => false, 'power_time' => 600,'start_carma' => 10, 'limit' => 30], // defaults
2729
$messages_to_clean = [], // array with messages that must be cleaned
2830
$users = []; // array of username/user
2931

32+
3033
/**
3134
* Reply to /carma command with information (for now only rating available)
3235
*
@@ -52,8 +55,8 @@ public function onPublicText( Update $update )
5255
{
5356
$answer = ['Debug carma info:'];
5457
$sum = array_sum( array_map(function ($user) use (&$answer){
55-
$rating = round( $result = $user->carma_rating ?? $this->getOption('start_carma', 10) , 2);
56-
$power = $user->carma_updated ? (time() - $user->carma_updated) / $this->getOption('power_time', 600) : 1.0;
58+
$rating = round( $result = $this->getRating($user) , 2);
59+
$power = round( $this->getPower($user) , 2);
5760
$name = $user->name ?? $user->username;
5861
$answer[] = "- $name has $rating carma and $power power";
5962
return $result;
@@ -63,6 +66,17 @@ public function onPublicText( Update $update )
6366
return false;
6467
}
6568

69+
elseif ($message->text()->token(1) === 'top')
70+
{
71+
$answer = ['Carma top:'] + array_map(function ($user) {
72+
$rating = round( $this->getRating($user) , 2);
73+
$name = $user->name ?? $user->username;
74+
return "- $name has $rating carma";
75+
}, R::findAll('user', ' ORDER BY carma_rating DESC LIMIT ' . $this->getOption('limit')));
76+
$update->answerMessage( trim( implode(PHP_EOL, $answer)) );
77+
return false;
78+
}
79+
6680
// array of answer
6781
$answer = [];
6882

@@ -80,10 +94,10 @@ public function onPublicText( Update $update )
8094
}, $entities));
8195

8296
// make answer from request to database
83-
foreach (R::find('user', ' username IN (' . R::genSlots( $usernames ) . ') ORDER BY carma_rating DESC ', $usernames) as $user)
97+
foreach (R::find('user', ' username IN (' . R::genSlots( $usernames ) . ') ORDER BY carma_rating DESC', $usernames) as $user)
8498
{
85-
$rating = round( $result = $user->carma_rating ?? $this->getOption('start_carma', 10) , 2);
86-
$power = round( $user->carma_updated ? (time() - $user->carma_updated) / $this->getOption('power_time', 600) : 1.0 , 2);
99+
$rating = round( $this->getRating($user) , 2);
100+
$power = round( $this->getPower($user), 2);
87101
$name = $user->name ?? $user->username;
88102
$answer[] = "- $name has $rating carma and $power power";
89103
}
@@ -95,7 +109,7 @@ public function onPublicText( Update $update )
95109
{
96110
$user = $message->from();
97111
$rating = round( $this->getRating( $user ), 2 );
98-
$power = round( $this->getPower( $user ), 1 );
112+
$power = round( $this->getPower( $user ), 2 );
99113
$answer[] = "$user, you have $rating carma available, your power is $power";
100114
}
101115

@@ -217,9 +231,10 @@ public function onEmpty( Update $update )
217231
*
218232
* @return float
219233
*/
220-
private function getPower(User $user ): float
234+
private function getPower($user ): float
221235
{
222-
$time = $user->getCustom()->carma_updated;
236+
if ($user instanceof User) $user = $user->getCustom();
237+
$time = $user->carma_updated;
223238
$power = is_null($time)
224239
? 1.0
225240
: (time() - $time) / $this->getOption('power_time', 600)
@@ -229,31 +244,32 @@ private function getPower(User $user ): float
229244

230245
/**
231246
* Get rating of user
232-
* @param User $user
247+
* @param User|RedBeanPHP\OODBBean $user
233248
*
234249
* @return float
235250
*/
236-
private function getRating( User $user ) : float
251+
private function getRating( $user ) : float
237252
{
238-
$rating = $user->getCustom()->carma_rating;
253+
if ($user instanceof User) $user = $user->getCustom();
254+
$rating = $user->carma_rating;
239255
return is_null($rating)
240256
? (float) $this->getOption('start_carma', 10)
241257
: (float) $rating;
242258
}
243259

244260
/**
245261
* Save rating with updated_date
246-
* @param User $user
262+
* @param User|RedBeanPHP\OODBBean $user
247263
* @param $value
248264
*
249265
* @throws \RedBeanPHP\RedException\SQL
250266
*/
251-
private function setRating( User $user, $value )
267+
private function setRating( $user, $value )
252268
{
253-
$data = $user->getCustom();
269+
$data = $user instanceof User ? $user->getCustom() : $user;
254270
$data->carma_rating = $value;
255271
$data->carma_updated = time();
256-
$user->saveCustom();
272+
if ($user instanceof $user) $user->saveCustom();
257273
}
258274

259275
}

src/Plugin/Hello.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class Hello extends Base
2020
public function onPrivateText( Update $update )
2121
{
2222

23-
if (!preg_match('@^(/start|hello|hi|yo)\b@ui', $update->message()->text())) return;
23+
if (!in_array($update->message()->text()->trigger(), ['start','hello','hi','yo'])) return;
2424

2525
$name = $update->message()->from()->name();
2626

src/Plugin/Kicker.php

+61-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
/**
33
* Kicker plugin for Joker Telegram Bot
44
*
5-
* Checks joined person and kicks if emoji found in nickname.
5+
* This plugin will remove users with emojis in their name instantly, and others after 10 minutes of inactivity after join.
6+
*
7+
* Options:
8+
* - `secons_with_emoji` integer, optional, default is 0 - wait time before remove user with emoji in name
9+
* - `secons_without_emoji` integer, optional, default is 600 - wait time before remove user without emoji in name
610
*
711
* @package joker-telegram-bot
812
* @author Sergei Miami <[email protected]>
@@ -15,31 +19,79 @@
1519
class Kicker extends Base
1620
{
1721

22+
protected $options = [
23+
'secons_with_emoji' => 0,
24+
'secons_without_emoji' => 600,
25+
];
26+
27+
private $waiting_list = [];
28+
29+
/**
30+
* Listen to JOIN event, add to array with time, when to kick this user
31+
* @param Update $update
32+
*/
1833
public function onJoin( Update $update )
1934
{
2035
// new chat member
2136
$user = $update->message()->new_chat_member();
37+
$chat = $update->message()->chat();
2238

2339
// check name for emoji
24-
if (self::containsEmoji($user->name()))
40+
$option = self::containsEmoji($user->name()) ? 'seconds_with_emoji' : 'seconds_without_emoji';
41+
$seconds = $this->getOption( $option );
42+
43+
// add user to waiting list
44+
$this->waiting_list[] = [time() + $seconds, $chat->id(), $user->id() ];
45+
}
46+
47+
/**
48+
* Listen to text messages from user and remove from waiting list
49+
* @param Update $update
50+
*/
51+
public function onPublicText( Update $update )
52+
{
53+
// new chat member
54+
$message = $update->message();
55+
foreach ($this->waiting_list as $i=>$item)
2556
{
57+
list($time, $chat_id, $user_id ) = $item;
58+
if (
59+
$chat_id == $message->chat()->id() &&
60+
$user_id == $message->from()->id()
61+
) unset($this->waiting_list[$i]);
62+
}
63+
}
64+
65+
/**
66+
* Timer for kicking users from kicklist
67+
* @param Update $update
68+
*/
69+
public function onEmpty( Update $update)
70+
{
2671

27-
// quote from Predator
28-
$update->answerMessage('If it bleeds, we can kill it ;p');
72+
foreach ($this->waiting_list as $i => $item)
73+
{
74+
75+
list($time, $chat_id, $user_id ) = $item;
76+
77+
if (time() < $time) continue;
2978

3079
// kick user
3180
$update->customRequest('kickChatMember',[
32-
'chat_id' => $update->message()->chat()->id(),
33-
'user_id' => $user->id(),
81+
'chat_id' => $chat_id,
82+
'user_id' => $user_id,
3483
]);
3584

36-
// delete message about join
37-
$update->deleteMessage();
85+
$update->customRequest('sendMessage', [
86+
'chat_id' => $chat_id,
87+
'text' => 'If it bleeds, we can kill it ;p',
88+
]);
89+
90+
unset($this->waiting_list[$i]);
3891

3992
}
4093

4194
}
42-
4395
/**
4496
* Check text contains emoji.
4597
* Full list got from https://unicode.org/emoji/charts/full-emoji-list.html

0 commit comments

Comments
 (0)