1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Livia |
4
|
|
|
* Copyright 2017-2019 Charlotte Dunois, All Rights Reserved |
5
|
|
|
* |
6
|
|
|
* Website: https://charuru.moe |
7
|
|
|
* License: https://github.com/CharlotteDunois/Livia/blob/master/LICENSE |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace CharlotteDunois\Livia\Types; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* {@inheritdoc} |
14
|
|
|
* @internal |
15
|
|
|
*/ |
16
|
|
|
class RoleArgumentType extends ArgumentType { |
17
|
|
|
/** |
18
|
|
|
* @internal |
19
|
|
|
*/ |
20
|
|
|
function __construct(\CharlotteDunois\Livia\Client $client) { |
21
|
|
|
parent::__construct($client, 'role'); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* {@inheritdoc} |
26
|
|
|
* @return bool|string|\React\Promise\ExtendedPromiseInterface |
27
|
|
|
*/ |
28
|
|
|
function validate(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
29
|
|
|
$prg = \preg_match('/(?:<@&)?(\d{15,})>?/', $value, $matches); |
30
|
|
|
if($prg === 1) { |
31
|
|
|
return $context->message->guild->roles->has($matches[1]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
$search = \mb_strtolower($value); |
35
|
|
|
|
36
|
|
|
$inexactRoles = $context->message->guild->roles->filter(function ($role) use ($search) { |
37
|
|
|
return (\mb_stripos($role->name, $search) !== false); |
38
|
|
|
}); |
39
|
|
|
$inexactLength = $inexactRoles->count(); |
40
|
|
|
|
41
|
|
|
if($inexactLength === 0) { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
if($inexactLength === 1) { |
45
|
|
|
return true; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$exactRoles = $context->message->guild->roles->filter(function ($role) use ($search) { |
49
|
|
|
return ($role->name === $search); |
50
|
|
|
}); |
51
|
|
|
$exactLength = $exactRoles->count(); |
52
|
|
|
|
53
|
|
|
if($exactLength === 1) { |
54
|
|
|
return true; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if($exactLength > 0) { |
58
|
|
|
$roles = $exactRoles; |
59
|
|
|
} else { |
60
|
|
|
$roles = $inexactRoles; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if($roles->count() >= 15) { |
64
|
|
|
return 'Multiple roles found. Please be more specific.'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return \CharlotteDunois\Livia\Utils\DataHelpers::disambiguation($roles, 'roles', null).\PHP_EOL; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
* @return mixed|null|\React\Promise\ExtendedPromiseInterface |
73
|
|
|
*/ |
74
|
|
|
function parse(string $value, \CharlotteDunois\Livia\Commands\Context $context, ?\CharlotteDunois\Livia\Arguments\Argument $arg = null) { |
75
|
|
|
$prg = \preg_match('/(?:<@&)?(\d{15,})>?/', $value, $matches); |
76
|
|
|
if($prg === 1) { |
77
|
|
|
return $context->message->guild->roles->get($matches[1]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$search = \mb_strtolower($value); |
81
|
|
|
|
82
|
|
|
$inexactRoles = $context->message->guild->roles->filter(function ($role) use ($search) { |
83
|
|
|
return (\mb_stripos($role->name, $search) !== false); |
84
|
|
|
}); |
85
|
|
|
$inexactLength = $inexactRoles->count(); |
86
|
|
|
|
87
|
|
|
if($inexactLength === 0) { |
88
|
|
|
return null; |
89
|
|
|
} |
90
|
|
|
if($inexactLength === 1) { |
91
|
|
|
return $inexactRoles->first(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$exactRoles = $context->message->guild->roles->filter(function ($role) use ($search) { |
95
|
|
|
return ($role->name === $search); |
96
|
|
|
}); |
97
|
|
|
$exactLength = $exactRoles->count(); |
98
|
|
|
|
99
|
|
|
if($exactLength === 1) { |
100
|
|
|
return $exactRoles->first(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return null; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|