forked from Spelako/SpelakoCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpelakoCore.php
120 lines (110 loc) · 3.58 KB
/
SpelakoCore.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
* Copyright (C) 2020-2022 Spelako Project
*
* This file is part of SpelakoCore.
* Permission is granted to use, modify and/or distribute this program
* under the terms of the GNU Affero General Public License version 3.
* You should have received a copy of the license along with this program.
* If not, see <https://www.gnu.org/licenses/agpl-3.0.html>.
*
* 此文件是 SpelakoCore 的一部分.
* 在 GNU Affero 通用公共许可证第三版的约束下,
* 你有权使用, 修改, 复制和/或传播该软件.
* 你理当随同本程序获得了此许可证的副本.
* 如果没有, 请查阅 <https://www.gnu.org/licenses/agpl-3.0.html>.
*
*/
require __DIR__.'/utils/FileSystem.php';
require __DIR__.'/utils/SpelakoUtils.php';
class SpelakoCore {
const VERSION = '22w30a';
const LAST_UPDATED = '2022/7/30';
const WEBSITE = 'https://spelako.github.io';
private array $commands = [];
private array $resources = [];
private array $staffs = [];
private int $cooldown = 0;
private string $cooldownMessage = '';
private array $userLastExecutions = [];
/**
* 创建一个 Spelako 对象
*
* @param mixed $configFile 配置文件的位置
*/
function __construct(string $configFile) {
$config = json_decode(FileSystem::fileRead($configFile));
foreach($config->commands as $command) {
require_once('commands/'.$command->file);
$class = basename($command->file, '.php');
array_push($this->commands, new $class($this, $command?->config));
}
$this->staffs = $config->staffs;
$this->cooldown = $config->cooldown;
$this->cooldownMessage = $config->cooldown_message;
}
/**
* 加载一个 JSON 资源
*
* @param mixed 位于 resource 目录中的文件名
* @return bool 是否成功
*/
public function loadJsonResource($file) : bool {
if(!FileSystem::fileExists(__DIR__.'/resources/'.$file)) return false;
$this->resources[$file] = json_decode(FileSystem::fileRead(__DIR__.'/resources/'.$file));
return true;
}
/**
* 获取已加载的 JSON 资源的值
*
* @param string $file 文件名
* @param string $path 键 (可通过 `.` 拼接)
* @return mixed 结果
*/
public function getJsonValue(string $file, string $path) : mixed {
if(!str_contains($path, '.')) return @$this->resources[$file]->{$path};
$keys = explode('.', $path);
@$pointer = $this->resources[$file]->{$keys[0]};
for($i = 1; $i < count($keys); $i ++) @$pointer = $pointer->{$keys[$i]};
return $pointer;
}
/**
* 获取所有注册的命令
*
* @return array
*/
public function getCommands() : array {
return $this->commands;
}
/**
* 获取所有用户冷却状态
*
* @return array 结果
*/
public function getUserLastExecutions() : array {
return $this->userLastExecutions;
}
/**
* 执行一条命令
*
* @param string $text 命令全文
* @param string $user 执行者
* @return string|null 执行结果
*/
public function execute(string $text, string $user) : string|null {
$args = array_values(array_filter(explode(' ', strtolower($text))));
foreach($this->commands as $command) if(in_array($args[0], $command->getName())) {
if(
$command->hasCooldown()
&& !in_array($user, $this->staffs)
&& isset($this->userLastExecutions[$user])
&& time() - $this->userLastExecutions[$user] < $this->cooldown
) return $this->cooldownMessage;
$args[0] = $text;
$this->userLastExecutions[$user] = time();
return $command->execute($args, in_array($user, $this->staffs));
}
return null;
}
}
?>