prefix.php$0 ➔ run()   C
last analyzed

Complexity

Conditions 13

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 13
dl 0
loc 40
rs 6.6166
c 2
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
return function ($client) {
11
    return (new class($client) extends \CharlotteDunois\Livia\Commands\Command {
12
        function __construct(\CharlotteDunois\Livia\Client $client) {
13
            parent::__construct($client, array(
14
                'name' => 'prefix',
15
                'aliases' => array(),
16
                'group' => 'utils',
17
                'description' => 'Shows or sets the command prefix.',
18
                'details' => 'If no prefix is provided, the current prefix will be shown. If the prefix is "default", the prefix will be reset to the bot\'s default prefix. If the prefix is "none", the prefix will be removed entirely, only allowing mentions to run commands. Only administrators may change the prefix.',
19
                'format' => '[prefix/"default"/"none"]',
20
                'guildOnly' => false,
21
                'throttling' => array(
22
                    'usages' => 2,
23
                    'duration' => 3
24
                ),
25
                'args' => array(
26
                    array(
27
                        'key' => 'prefix',
28
                        'prompt' => 'What would you like to set the bot\'s prefix to?',
29
                        'type' => 'string',
30
                        'max' => 15,
31
                        'default' => ''
32
                    )
33
                ),
34
                'guarded' => true
35
            ));
36
        }
37
        
38
        function run(\CharlotteDunois\Livia\Commands\Context $context, \ArrayObject $args, bool $fromPattern) {
39
            if(empty($args['prefix'])) {
40
                $prefix = $this->client->getGuildPrefix($context->message->guild);
41
                $msg = ($prefix !== null ? 'The command prefix is `'.$prefix.'`.' : 'There is no command prefix set.').\PHP_EOL.'To run commands, use '.\CharlotteDunois\Livia\Commands\Command::anyUsage('command', $prefix, $this->client->user).'.';
42
                return $context->say($msg);
43
            }
44
            
45
            if($context->message->guild !== null) {
46
                if(!$context->message->member->permissions->has('ADMINISTRATOR') && !$this->client->isOwner($context->message->author)) {
47
                    return $context->reply('Only administrators may change the command prefix.');
48
                }
49
            } elseif(!$this->client->isOwner($context->message->author)) {
50
                return $context->reply('Only the bot owner may change the command prefix.');
51
            }
52
            
53
            $prefixLc = \mb_strtolower($args['prefix']);
54
            $prefix = ($prefixLc === 'none' ? null : $args['prefix']);
55
            $guild = $context->message->guild;
56
            
57
            if($prefixLc === 'default') {
58
                if($guild !== null) {
59
                    $this->client->setGuildPrefix($guild, '');
60
                } else {
61
                    $this->client->setCommandPrefix(null);
62
                }
63
                
64
                $prefix = $this->client->commandPrefix;
65
                $current = ($this->client->commandPrefix ? '`'.$this->client->commandPrefix.'`' : 'no prefix');
66
                $response = 'Reset the command prefix to the default (currently '.$current.').';
67
            } else {
68
                if($guild !== null) {
69
                    $this->client->setGuildPrefix($guild, $prefix);
70
                } else {
71
                    $this->client->setCommandPrefix($prefix);
72
                }
73
                
74
                $response = ($prefix ? 'Set the command prefix to `'.$prefix.'`.' : 'Removed the command prefix entirely.');
75
            }
76
            
77
            return $context->reply($response.' To run commands use '.\CharlotteDunois\Livia\Commands\Command::anyUsage('command', $prefix, $this->client->user));
78
        }
79
    });
80
};
81