forked from Valgorithms/Palace-Revived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutelar.php
169 lines (147 loc) · 6.34 KB
/
tutelar.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* This file is a part of the Tutelar project.
*
* Copyright (c) 2022-present Valithor Obsidion <[email protected]>
*/
namespace Tutelar;
class Tutelar
{
public $loop;
public $discord;
public $twitch;
public $browser;
public $logger;
public $stats;
protected $webapi;
public $timers = [];
public $functions = [];
public $command_symbol = [];
public $owner_id = '116927250145869826';
public $tutelar_guild_id = '923969098185068594';
public $files = [];
public $ips = [];
public $ports = [];
public $channel_ids = [];
public $role_ids = [];
/**
* Creates a Tutelar client instance.
*
* @param array $options Array of options.
* @throws IntentException
*/
public function __construct(array $options = [])
{
if (php_sapi_name() !== 'cli') {
trigger_error('DiscordPHP will not run on a webserver. Please use PHP CLI to run a DiscordPHP bot.', E_USER_ERROR);
}
// x86 need gmp extension for big integer operation
if (PHP_INT_SIZE === 4 && ! Discord\Helpers\Bitwise::init()) {
trigger_error('ext-gmp is not loaded. Permissions will NOT work correctly!', E_USER_WARNING);
}
$options = $this->resolveOptions($options);
$this->loop = $options['loop'];
$this->browser = $options['browser'];
$this->logger = $options['logger'];
$this->stats = $options['stats'];
if(isset($options['command_symbol'])) {
if(is_array($options['command_symbol'])) {
foreach ($options['command_symbol'] as $symbol)
$this->command_symbol[] = $symbol;
}
if(is_string($options['command_symbol'])) {
$this->command_symbol[] = $options['command_symbol'];
}
}
if(isset($options['owner_id'])) {
$this->owner_id = $options['owner_id'];
}
if(isset($options['tutelar_guild_id'])) {
$this->tutelar_guild_id = $options['tutelar_guild_id'];
}
if (isset($options['discord']) || isset($options['discord_options'])) {
if(isset($options['discord'])) $this->discord = $options['discord'];
elseif(isset($options['discord_options'])) $this->discord = new \Discord\Discord($options['discord_options']);
}
if (isset($options['twitch']) || isset($options['twitch_options'])) {
if(isset($options['twitch'])) $this->twitch = $options['twitch'];
elseif(isset($options['twitch_options'])) $this->twitch = new \Twitch\Twitch($options['twitch_options']);
}
if(isset($options['functions'])) {
if(isset($options['functions']['ready'])) {
foreach ($options['functions']['ready'] as $key => $func)
$this->functions['ready'][$key] = $func;
}
if(isset($options['functions']['message']))
foreach ($options['functions']['message'] as $key => $func)
$this->functions['message'][$key] = $func;
if(isset($options['functions']['misc']))
foreach ($options['functions']['misc'] as $key => $func)
$this->functions['misc'][$key] = $func;
} else $this->logger->warning('No functions passed in options!');
if(isset($options['files'])) {
foreach ($options['files'] as $key => $path)
$this->files[$key] = $path;
} else $this->logger->warning('No files passed in options!');
if(isset($options['channel_ids'])) {
foreach ($options['channel_ids'] as $key => $id)
$this->channel_ids[$key] = $id;
} else $this->logger->warning('No channel_ids passed in options!');
if(isset($options['role_ids'])) {
foreach ($options['role_ids'] as $key => $id)
$this->role_ids[$key] = $id;
} else $this->logger->warning('No role_ids passed in options!');
$this->afterConstruct();
}
protected function afterConstruct()
{
if(isset($this->discord)) {
$this->discord->once('ready', function () {
$this->command_symbol[] = '<@'.$this->discord->id.'>';
$this->command_symbol[] = '<@!'.$this->discord->id.'>';
if(! empty($this->functions['ready']))
foreach ($this->functions['ready'] as $func)
$func($this);
else $this->logger->debug('No ready functions found!');
$this->discord->on('message', function ($message)
{
if(! empty($this->functions['message']))
foreach ($this->functions['message'] as $func)
$func($this, $message);
else $this->logger->debug('No message functions found!');
});
$this->discord->on('GUILD_MEMBER_ADD', function ($guildmember) {
if(! empty($this->functions['GUILD_MEMBER_ADD']))
foreach ($this->functions['GUILD_MEMBER_ADD'] as $func)
$func($this, $guildmember);
else $this->logger->debug('No message functions found!');
});
});
}
}
/*
* Attempt to catch errors with the user-provided $options early
*/
protected function resolveOptions(array $options = []): array
{
if (is_null($options['logger'])) {
$logger = new Monolog\Logger('Tutelar');
$logger->pushHandler(new Monolog\Handler\StreamHandler('php://stdout', Monolog\Logger::DEBUG));
$options['logger'] = $logger;
}
$options['loop'] = $options['loop'] ?? React\EventLoop\Factory::create();
$options['browser'] = $options['browser'] ?? new \React\Http\Browser($options['loop']);
return $options;
}
public function run(): void
{
$this->logger->info('Starting Discord loop');
if(!(isset($this->discord))) $this->logger->warning('Discord not set!');
else $this->discord->run();
}
public function stop(): void
{
$this->logger->info('Shutting down');
if((isset($this->discord))) $this->discord->stop();
}
}