Passed
Branch dev (7891d7)
by
unknown
02:51
created

setAvatar::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
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
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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
            $id = $this->config['bot']['guild'];
68
            $guild = $this->discord->guilds->get('id', $id);
69
            $member = $guild->members->get('id', $userID);
70
            $roles = $member->roles;
71
            foreach ($roles as $role) {
72
                if (in_array($role->name, $adminRoles, true)) {
73
                    $avatarURL = strtolower((string)$data['messageString']);
74
                    $ch = curl_init($avatarURL);
75
                    if (substr($avatarURL, -4) === '.jpg') {
76
                        $fp = fopen('/tmp/avatar.jpg', 'wb');
77
                        $dir = '/tmp/avatar.jpg';
78
                    } elseif (substr($avatarURL, -4) === '.png') {
79
                        $fp = fopen('/tmp/avatar.png', 'wb');
80
                        $dir = '/tmp/avatar.png';
81
                    } else {
82
                        return $this->message->reply('Invalid URL. Make sure the URL links directly to a JPG or PNG.');
83
                    }
84
                    curl_setopt($ch, CURLOPT_FILE, $fp);
85
                    curl_setopt($ch, CURLOPT_HEADER, 0);
86
                    curl_exec($ch);
87
                    curl_close($ch);
88
                    fclose($fp);
89
                    $this->discord->setAvatar($dir);
90
                    $this->discord->save();
91
92
                    $msg = 'New avatar set';
93
                    $this->logger->addInfo("setGame: Bot avatar changed to {$avatarURL} by {$msgData['message']['from']}");
94
                    $this->message->reply($msg);
95
                    return null;
96
                }
97
            }
98
            $this->logger->addInfo("setGame: {$msgData['message']['from']} attempted to change the bot's avatar.");
99
            $msg = ':bangbang: You do not have the necessary roles to issue this command :bangbang:';
100
            $this->message->reply($msg);
101
            return null;
102
        }
103
        return null;
104
    }
105
106
    /**
107
     * @return array
108
     */
109 View Code Duplication
    public function information()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        return array(
112
            'name' => 'avatar',
113
            'trigger' => array($this->config['bot']['trigger'] . 'avatar'),
114
            'information' => 'Changes the bots avatar (!avatar url) **(Admin Role Required)**'
115
        );
116
    }
117
118
}
119