Passed
Branch master (d69cc3)
by
unknown
02:45
created

setGame::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\Game;
27
28
/**
29
 * @property  message
30
 */
31
class setGame
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
    /**
34
     * @var
35
     */
36
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
37
    /**
38
     * @var
39
     */
40
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
41
    /**
42
     * @var
43
     */
44
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
45
46
    public $message;
47
48
    /**
49
     * @param $config
50
     * @param $discord
51
     * @param $logger
52
     */
53
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
54
    {
55
        $this->config = $config;
56
        $this->discord = $discord;
57
        $this->logger = $logger;
58
    }
59
60
    /**
61
     *
62
     */
63
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
64
    {
65
    }
66
67
    /**
68
     * @param $msgData
69
     * @param $message
70
     * @return null
71
     */
72
    function onMessage($msgData, $message)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
    {
74
        $this->message = $message;
75
76
        $message = $msgData["message"]["message"];
77
78
        $data = command($message, $this->information()["trigger"], $this->config["bot"]["trigger"]);
79
        if (isset($data["trigger"])) {
80
81
            //Admin Check
82
            $userID = $msgData["message"]["fromID"];
83
            $adminRoles = $this->config["bot"]["adminRoles"];
84
            $id = $this->config["bot"]["guild"];
85
            $guild = $this->discord->guilds->get('id', $id);
86
            $member = $guild->members->get("id", $userID);
87
            $roles = $member->roles;
88
            foreach ($roles as $role) {
89
                if (in_array($role->name, $adminRoles, true)) {
90
                    $newGame = (string)$data["messageString"];
91
                    $game = $this->discord->factory(Game::class, [
92
                        'name' => $newGame,
93
                    ]);
94
                    $this->discord->updatePresence($game);
95
                    setPermCache("botGame", $newGame);
96
97
                    $msg = "Bot is now playing **{$newGame}**";
98
                    $this->logger->addInfo("setGame: Bot game changed to {$newGame} by {$msgData["message"]["from"]}");
99
                    $this->message->reply($msg);
100
                    return null;
101
                }
102
            }
103
            $this->logger->addInfo("setGame: {$msgData["message"]["from"]} attempted to change the bot's game.");
104
            $msg = ":bangbang: You do not have the necessary roles to issue this command :bangbang:";
105
            $this->message->reply($msg);
106
            return null;
107
        }
108
        return null;
109
    }
110
111
    /**
112
     * @return array
113
     */
114 View Code Duplication
    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...
115
    {
116
        return array(
117
            "name" => "game",
118
            "trigger" => array($this->config["bot"]["trigger"] . "game"),
119
            "information" => "Changes the bots game (Admin Role)"
120
        );
121
    }
122
123
}
124