Skip to content

Commit 3623061

Browse files
committed
First commit
1 parent 3b60aad commit 3623061

File tree

6 files changed

+294
-1
lines changed

6 files changed

+294
-1
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Emeric
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,19 @@
1-
# Module
1+
# Module Plugin
2+
3+
|StyleCI|Stable Version|Downloads|License|
4+
|:------:|:------:|:------:|:------:|
5+
|[![StyleCI](https://styleci.io/repos/73554440/shield)](https://styleci.io/repos/73554440)|[![Latest Stable Version](https://img.shields.io/packagist/v/SkinnyBot/Module.svg?style=flat-square)](https://packagist.org/packages/skinnybot/module)|[![Total Downloads](https://img.shields.io/packagist/dt/skinnybot/module.svg?style=flat-square)](https://packagist.org/packages/skinnybot/module)|[![License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](https://packagist.org/packages/skinnybot/module)|
6+
27
The Module plugin for Skinny.
8+
9+
This plugin is used to manage Modules with commands. Especially useful when developing news modules.
10+
11+
## Commands
12+
|Command|Description|
13+
|------|-------|
14+
|`!module load <module>`|Load the specified module.|
15+
|`!module unload <module>`|Unload the specified module.|
16+
|`!module reload <module>`|Reload the specified module.|
17+
|`!module reload all`|Reload all the loaded modules.|
18+
|`!module time <module>`|Display the time from when the module is loaded.|
19+
|`!module loaded`|Show the list of the loaded modules.|

composer.json

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "skinnybot/module",
3+
"description": "The Module plugin for Skinny.",
4+
"homepage": "https://github.com/SkinnyBot/Module",
5+
"keywords": ["discord", "bot", "php", "discordphp", "botphp", "skinny", "plugin", "module"],
6+
"type": "skinny-plugin",
7+
"license": "MIT",
8+
"support": {
9+
"source":"https://github.com/SkinnyBot/Module",
10+
"issues":"https://github.com/SkinnyBot/Module/issues"
11+
},
12+
"authors": [
13+
{
14+
"name": "Xety",
15+
"email": "[email protected]",
16+
"homepage": "https://github.com/Xety"
17+
}
18+
],
19+
"require": {
20+
"php": ">=5.6",
21+
"skinnybot/skinny": "~1.0"
22+
},
23+
"require-dev": {
24+
"phpunit/phpunit": "*",
25+
"squizlabs/php_codesniffer": "3.0.x-dev"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Module\\": "src"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"ModuleTest\\": "tests"
35+
}
36+
},
37+
"minimum-stability": "stable"
38+
}

config/bootstrap.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
use Skinny\Core\Configure;
3+
4+
/**
5+
* Load the commands file configuration to register the commands.
6+
*/
7+
try {
8+
Configure::load('Module.commands');
9+
} catch (\Exception $e) {
10+
die($e->getMessage() . "\n");
11+
}

config/commands.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
return [
3+
'Commands' => [
4+
'module' => [
5+
'params' => 1,
6+
'syntax' => 'Module [Load|Unload|Reload|Time|Loaded] Optional : [Module]',
7+
'admin' => true
8+
]
9+
]
10+
];

src/Module/Modules/Module.php

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
<?php
2+
namespace Module\Module\Modules;
3+
4+
use DateTime;
5+
use Skinny\Core\Configure;
6+
use Skinny\Module\ModuleInterface;
7+
use Skinny\Network\Wrapper;
8+
use Skinny\Utility\Command;
9+
use Skinny\Utility\Inflector;
10+
11+
class Module implements ModuleInterface
12+
{
13+
/**
14+
* {@inheritDoc}
15+
*
16+
* @param \Skinny\Network\Wrapper $wrapper The Wrapper instance.
17+
* @param array $message The message array.
18+
*
19+
* @return void
20+
*/
21+
public function onChannelMessage(Wrapper $wrapper, $message)
22+
{
23+
}
24+
25+
/**
26+
* {@inheritDoc}
27+
*
28+
* @param \Skinny\Network\Wrapper $wrapper The Wrapper instance.
29+
* @param array $message The message array.
30+
*
31+
* @return void
32+
*/
33+
public function onPrivateMessage(Wrapper $wrapper, $message)
34+
{
35+
}
36+
37+
/**
38+
* {@inheritDoc}
39+
*
40+
* @param \Skinny\Network\Wrapper $wrapper The Wrapper instance.
41+
* @param array $message The message array.
42+
*
43+
* @return void
44+
*/
45+
public function onCommandMessage(Wrapper $wrapper, $message)
46+
{
47+
if ($message['command'] !== 'module') {
48+
return;
49+
}
50+
51+
if (!isset($message['arguments'][1]) && $message['arguments'][0] !== 'loaded') {
52+
$wrapper->Message->reply(Command::syntax($message));
53+
54+
return;
55+
}
56+
57+
switch ($message['arguments'][0]) {
58+
case 'load':
59+
//Load the Module.
60+
$module = $wrapper->ModuleManager->load($message['arguments'][1]);
61+
62+
switch ($module) {
63+
//AlreadyLoaded
64+
case 'AL':
65+
$wrapper->Channel->sendMessage('The Module `' . $message['arguments'][1] .
66+
'` is already loaded.');
67+
break;
68+
69+
//Loaded
70+
case 'L':
71+
$wrapper->Channel->sendMessage('Module `' . $message['arguments'][1] .
72+
'` loaded successfully.');
73+
break;
74+
75+
//NotFound
76+
case 'NF':
77+
$wrapper->Channel->sendMessage('The Module `' . $message['arguments'][1] . '` was not found.');
78+
break;
79+
}
80+
break;
81+
82+
case 'unload':
83+
//Prevent for loading a file in the memory for nothing.
84+
if (Configure::read('debug') === false) {
85+
$wrapper->Channel->sendMessage('You can\'t unload a Module when the debug is `false`.');
86+
break;
87+
}
88+
89+
//Unload the Module.
90+
$module = $wrapper->ModuleManager->unload($message['arguments'][1]);
91+
92+
//AlreadyUnloaded
93+
if ($module === 'AU') {
94+
$wrapper->Channel->sendMessage('The Module `' . $message['arguments'][1] .
95+
'` is already unloaded or doesn\'t exist.');
96+
} else {
97+
$wrapper->Channel->sendMessage('Module `' . $message['arguments'][1] . '` unloaded successfully.');
98+
}
99+
break;
100+
101+
case 'reload':
102+
//Prevent for loading a file in the memory for nothing.
103+
if (Configure::read('debug') === false) {
104+
$wrapper->Channel->sendMessage('You can\'t reload a Module when the debug is `false`.');
105+
break;
106+
}
107+
108+
//Check if we must reload all Modules.
109+
if ($message['arguments'][1] == "all") {
110+
//Get the list of the loaded Modules.
111+
$loadedModules = $wrapper->ModuleManager->getLoadedModules();
112+
113+
//For each Modules, we reload it.
114+
foreach ($loadedModules as $module) {
115+
$this->reloadModule($wrapper, $module);
116+
117+
//To avoid spam.
118+
usleep(500000);
119+
}
120+
121+
break;
122+
}
123+
124+
//Else there is just one Module to reload.
125+
$this->reloadModule($wrapper, $message['arguments'][1]);
126+
break;
127+
128+
case 'time':
129+
//Get the UNIX time.
130+
$time = $wrapper->ModuleManager->timeLoaded($message['arguments'][1]);
131+
132+
//If $time is false, that mean the Module is not loaded and/or doesn't exist.
133+
if ($time === false) {
134+
$wrapper->Channel->sendMessage('This Module is not loaded.');
135+
break;
136+
}
137+
138+
$seconds = floor(microtime(true) - $time);
139+
140+
$start = new DateTime("@0");
141+
$end = new DateTime("@$seconds");
142+
143+
$wrapper->Channel->sendMessage('The Module `' . Inflector::camelize($message['arguments'][1]) .
144+
'` is loaded since ' . $start->diff($end)->format('%a days, %h hours, %i minutes and %s seconds.'));
145+
break;
146+
147+
case 'loaded':
148+
//Get the loaded Modules and implode the array as a string.
149+
$modules = $wrapper->ModuleManager->getLoadedModules();
150+
$modules = implode("`, `", $modules);
151+
152+
$wrapper->Channel->sendMessage('Modules loaded : `' . Inflector::camelize($modules) . '`.');
153+
break;
154+
155+
default:
156+
$wrapper->Channel->sendMessage(Command::unknown($message));
157+
}
158+
}
159+
160+
/**
161+
* Function to reload a Module and send the response.
162+
*
163+
* @param \Skinny\Network\Wrapper $wrapper The Wrapper instance.
164+
* @param string $module The module to reload.
165+
*
166+
* @return void
167+
*/
168+
protected function reloadModule(Wrapper $wrapper, $module)
169+
{
170+
$moduleStatus = $wrapper->ModuleManager->reload($module);
171+
172+
$module = Inflector::camelize($module);
173+
174+
switch ($moduleStatus) {
175+
//AlreadyUnloaded
176+
case 'AU':
177+
$wrapper->Channel->sendMessage('The Module `' . $module . '` doesn\'t exist and cannot be reloaded.');
178+
break;
179+
180+
//AlreadyLoaded
181+
case 'AL':
182+
$wrapper->Channel->sendMessage('The Module `' . $module . '` is already loaded.');
183+
break;
184+
185+
//Loaded
186+
case 'L':
187+
$wrapper->Channel->sendMessage('Module `' . $module . '` reloaded successfully.');
188+
break;
189+
190+
//NotFound
191+
case 'NF':
192+
$wrapper->Channel->sendMessage('Failed to reload the Module `' . $module . '`.');
193+
break;
194+
}
195+
}
196+
}

0 commit comments

Comments
 (0)