|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* The MIT License (MIT) |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (c) 2016 Robert Sardinia |
|
6
|
|
|
* |
|
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
9
|
|
|
* in the Software without restriction, including without limitation the rights |
|
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
12
|
|
|
* furnished to do so, subject to the following conditions: |
|
13
|
|
|
* |
|
14
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
15
|
|
|
* copies or substantial portions of the Software. |
|
16
|
|
|
* |
|
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
23
|
|
|
* SOFTWARE. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
use Discord\Parts\User\Client; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @property message |
|
30
|
|
|
*/ |
|
31
|
|
|
class setAvatar |
|
|
|
|
|
|
32
|
|
|
{ |
|
33
|
|
|
public $config; |
|
34
|
|
|
public $discord; |
|
35
|
|
|
public $logger; |
|
36
|
|
|
public $message; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param $config |
|
40
|
|
|
* @param $discord |
|
41
|
|
|
* @param $logger |
|
42
|
|
|
*/ |
|
43
|
|
|
public function init($config, $discord, $logger) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->config = $config; |
|
46
|
|
|
$this->discord = $discord; |
|
47
|
|
|
$this->logger = $logger; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param $msgData |
|
52
|
|
|
* @param $message |
|
53
|
|
|
* @return null |
|
54
|
|
|
*/ |
|
55
|
|
|
public function onMessage($msgData, $message) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->message = $message; |
|
58
|
|
|
|
|
59
|
|
|
$message = $msgData['message']['message']; |
|
60
|
|
|
|
|
61
|
|
|
$data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']); |
|
62
|
|
|
if (isset($data['trigger'])) { |
|
63
|
|
|
|
|
64
|
|
|
//Admin Check |
|
65
|
|
|
$userID = $msgData['message']['fromID']; |
|
66
|
|
|
$adminRoles = $this->config['bot']['adminRoles']; |
|
67
|
|
|
$adminRoles = array_map('strtolower', $adminRoles); |
|
68
|
|
|
$id = $this->config['bot']['guild']; |
|
69
|
|
|
$guild = $this->discord->guilds->get('id', $id); |
|
70
|
|
|
$member = $guild->members->get('id', $userID); |
|
71
|
|
|
$roles = $member->roles; |
|
72
|
|
|
foreach ($roles as $role) { |
|
73
|
|
|
if (in_array(strtolower($role->name), $adminRoles, true)) { |
|
74
|
|
|
$avatarURL = strtolower((string)$data['messageString']); |
|
75
|
|
|
$ch = curl_init($avatarURL); |
|
76
|
|
|
if (substr($avatarURL, -4) === '.jpg') { |
|
77
|
|
|
$fp = fopen('/tmp/avatar.jpg', 'wb'); |
|
78
|
|
|
$dir = '/tmp/avatar.jpg'; |
|
79
|
|
|
} elseif (substr($avatarURL, -4) === '.png') { |
|
80
|
|
|
$fp = fopen('/tmp/avatar.png', 'wb'); |
|
81
|
|
|
$dir = '/tmp/avatar.png'; |
|
82
|
|
|
} else { |
|
83
|
|
|
return $this->message->reply('Invalid URL. Make sure the URL links directly to a JPG or PNG.'); |
|
84
|
|
|
} |
|
85
|
|
|
curl_setopt($ch, CURLOPT_FILE, $fp); |
|
86
|
|
|
curl_setopt($ch, CURLOPT_HEADER, 0); |
|
87
|
|
|
curl_exec($ch); |
|
88
|
|
|
curl_close($ch); |
|
89
|
|
|
fclose($fp); |
|
90
|
|
|
$this->discord->setAvatar($dir); |
|
91
|
|
|
$this->discord->save(); |
|
92
|
|
|
|
|
93
|
|
|
$msg = 'New avatar set'; |
|
94
|
|
|
$this->logger->addInfo("setGame: Bot avatar changed to {$avatarURL} by {$msgData['message']['from']}"); |
|
95
|
|
|
$this->message->reply($msg); |
|
96
|
|
|
return null; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
$this->logger->addInfo("setGame: {$msgData['message']['from']} attempted to change the bot's avatar."); |
|
100
|
|
|
$msg = ':bangbang: You do not have the necessary roles to issue this command :bangbang:'; |
|
101
|
|
|
$this->message->reply($msg); |
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
return null; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return array |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public function information() |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
|
|
return array( |
|
113
|
|
|
'name' => 'avatar', |
|
114
|
|
|
'trigger' => array($this->config['bot']['trigger'] . 'avatar'), |
|
115
|
|
|
'information' => 'Changes the bots avatar (!avatar url) **(Admin Role Required)**' |
|
116
|
|
|
); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.