Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
vdeville committed Apr 30, 2018
2 parents efe96dc + 65f8709 commit a516391
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 115 deletions.
79 changes: 3 additions & 76 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,85 +1,12 @@
![banner](https://cloud.valentin-deville.eu/index.php/apps/files_sharing/ajax/publicpreview.php?x=2880&y=758&a=true&file=twitch-bot.jpg&t=1K94nCX0UVVJ9Oj&scalingup=0)

## Users
## Documention

### Setup the bot
1) Launch setup.sh by typing `bash setup.sh`
2) Edit `bot.php` file and enter your oAuth token, bot name and your channel
**New documentation !**

### Launch the bot
1) Launch `launch.sh` by typing `bash launch.sh`
**https://mythevalentinus.github.io/twitch-bot/en/**


## Developpers

I write this bot to have clean PHP base to create custom Twitch bot in PHP.
This project use modules system with hooks to add your own features. You can see examples of modules in `/modules` folder.

### Available hook:
* onConnect (Execute when bot connect to the channel)
* onMessage (Execute when new message come in the channel)
* onPing (Execute when someone mention the bot (Ex: @YourBot))
* onPong (Execute every 5 minutes when Twitch send PING status)
* onUsernotice (Execute when user subscribe for example)
* onCommand (Execute when someone send message with command symbol, by default `!`)

### Write module

1) Create folder named with the name of your module in `/modules` (Ex: `/modules/responder`)

2) Create php class named with the name of your module (Ex: `/modules/responder/responder.php`)

3) Define class and use TwichtBot module trait like this:
```php
<?php

/**
* Class Responder
*/
class Responder {

use \TwitchBot\Module {
\TwitchBot\Module::__construct as private moduleConstructor;
}

/**
* Responder constructor.
* @param array $infos
* @param \TwitchBot\IrcConnect $client
*/
public function __construct(array $infos, $client)
{
$this->moduleConstructor($infos, $client);
}

/**
* @param \TwitchBot\Message $message
*/
public function onPing($message)
{
$this->getClient()->sendMessage('You ping me @' . $message->getUsername() . ' ?! What do you want ?');
}
}
```

4) Develop your feature using available hook. The function name is `onNameOfTheHook()` (Ex: `onPing($data)`)

5) Refer to the php trait (`/src/class/Module.php`) for informations about functions and parameters

6) Update autoload using this command: `composer dumpautoload`

7) Start your bot in screen or tmux for example ! `php bot.php`

#### Config system

You can use your own config system like mysql storage but the bot have already config system using json file.

1) Create file `config.json` in your module folder

2) Respect json format

3) You can use `getConfig(), setConfig()` methods in your code

## Examples:
https://www.twitch.tv/warths (with custom modules and default)

Expand Down
55 changes: 53 additions & 2 deletions modules/antispam/Antispam.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function __construct(array $infos, $client)
public function onConnect()
{
if ($this->getInfo('connect_message')) {
$this->getClient()->sendMessage('Antispam system is working on !');
$this->getClient()->sendMessage('Antispam system is working on !');
}
}

Expand Down Expand Up @@ -127,7 +127,58 @@ private function isBlacklist($message)
*/
private function asLink($message)
{
$regex = "#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si";
if ($this->getConfig('use_new_regex_link')) {
/**
* Thanks to PhantomBot project for this regex
* Link: https://github.com/PhantomBot/PhantomBot
* Link: https://github.com/PhantomBot/PhantomBot/blob/master/javascript-source/core/patternDetector.js#L9-L49
*/
$regex = '#((?:(http|https|rtsp):\\/\\/(?:(?:[a-z0-9\\$\\-\\_\\.\\+\\!\\*\\\'\\(\\)'
. '\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,64}(?:\\:(?:[a-z0-9\\$\\-\\_'
. '\\.\\+\\!\\*\\\'\\(\\)\\,\\;\\?\\&\\=]|(?:\\%[a-fA-F0-9]{2})){1,25})?\\@)?)?'
. '((?:(?:[a-z0-9][a-z0-9\\-]{0,64}\\.)+'
. '(?:'
. '(?:aero|a[cdefgilmnoqrstuwxz])'
. '|(?:biz|b[abdefghijmnorstvwyz])'
. '|(?:com|c[acdfghiklmnoruvxyz])'
. '|d[ejkmoz]'
. '|(?:edu|e[cegrstu])'
. '|(?:fyi|f[ijkmor])'
. '|(?:gov|g[abdefghilmnpqrstuwy])'
. '|(?:how|h[kmnrtu])'
. '|(?:info|i[delmnoqrst])'
. '|(?:jobs|j[emop])'
. '|k[eghimnrwyz]'
. '|l[abcikrstuvy]'
. '|(?:mil|mobi|moe|m[acdeghklmnopqrstuvwxyz])'
. '|(?:name|net|n[acefgilopruz])'
. '|(?:org|om)'
. '|(?:pro|p[aefghklmnrstwy])'
. '|qa'
. '|(?:r[eouw])'
. '|(?:s[abcdeghijklmnortuvyz])'
. '|(?:t[cdfghjklmnoprtvwz])'
. '|u[agkmsyz]'
. '|(?:vote|v[ceginu])'
. '|(?:xxx)'
. '|(?:watch|w[fs])'
. '|y[etu]'
. '|z[amw]))'
. '|(?:(?:25[0-5]|2[0-4]'
. '[0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])\\.(?:25[0-5]|2[0-4][0-9]'
. '|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1]'
. '[0-9]{2}|[1-9][0-9]|[1-9]|0)\\.(?:25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}'
. '|[1-9][0-9]|[0-9])))'
. '(?:\\:\\d{1,5})?)'
. '(\\/(?:(?:[a-z0-9\\;\\/\\?\\:\\@\\&\\=\\#\\~'
. '\\-\\.\\+\\!\\*\\\'\\(\\)\\,\\_])|(?:\\%[a-fA-F0-9]{2}))*)?'
. '(?:\\b|$)'
. '|(\\.[a-z]+\\/|magnet:\/\/|mailto:\/\/|ed2k:\/\/|irc:\/\/|ircs:\/\/|skype:\/\/|ymsgr:\/\/|xfire:\/\/|steam:\/\/|aim:\/\/|spotify:\/\/)#si';

} else {
$regex = "#[-a-zA-Z0-9@:%_\+.~\#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~\#?&//=]*)?#si";
}

if (preg_match($regex, $message, $matches)) {
if (preg_match('#http#', $matches[0])) {
$domain = parse_url($matches[0], PHP_URL_HOST);
Expand Down
75 changes: 38 additions & 37 deletions modules/antispam/config.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
{
"blacklisted_word": {
"level-1": [
"level1",
"level11"
"blacklisted_word": {
"level-1": [
"level1",
"level11"
],
"level-2": [
"level2",
"level22"
],
"level-3": [
"level3",
"level33"
]
},
"whitelist_domain": [
"google.com",
"google.fr"
],
"level-2": [
"level2",
"level22"
"max_lenght": 300,
"message_blacklistedword": "%1$s was timeout, he say blacklisted word in category: %2$s",
"timeout_blacklistedword": 60,
"timeout_link": 120,
"message_timeout_link": "%1$s was timeout for link publishing",
"timeout_toolong": 30,
"message_timeout_toolong": "%1$s was timeout, message too long",
"timeout_toomanycaps": 45,
"message_timeout_toomanycaps": "%1$s was timeout, too many CAPS",
"pourcent_caps": 60,
"authorized_people": [
"v_deville"
],
"level-3": [
"level3",
"level33"
]
},
"whitelist_domain": [
"google.com",
"google.fr"
],
"max_lenght": 300,
"message_blacklistedword": "%1$s was timeout, he say blacklisted word in category: %2$s",
"timeout_blacklistedword": 60,
"timeout_link": 120,
"message_timeout_link": "%1$s was timeout for link publishing",
"timeout_toolong": 30,
"message_timeout_toolong": "%1$s was timeout, message too long",
"timeout_toomanycaps": 45,
"message_timeout_toomanycaps": "%1$s was timeout, too many CAPS",
"pourcent_caps": 60,
"authorized_people": [
"v_deville"
],
"message_link_add": "%1$s is now authorized to post link",
"message_link_already_add": "%1$s is already authorized to post link",
"message_link_remove": "%1$s is now not able to send link",
"message_link_already_remove": "%1$s is already not authorized",
"enable_blacklisterwords": true,
"enable_linkdetection": true,
"enable_toolong": true,
"enable_toomanycaps": true
"message_link_add": "%1$s is now authorized to post link",
"message_link_already_add": "%1$s is already authorized to post link",
"message_link_remove": "%1$s is now not able to send link",
"message_link_already_remove": "%1$s is already not authorized",
"enable_blacklisterwords": true,
"enable_linkdetection": true,
"enable_toolong": true,
"enable_toomanycaps": true,
"use_new_regex_link": true
}

0 comments on commit a516391

Please sign in to comment.