Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.28 KB | None | 0 0
  1.     private function cmdRoll($message, $args)
  2.     {
  3.         $random = [];
  4.         $response = [];
  5.         $embed = new MessageEmbed();
  6.  
  7.         $embed
  8.             ->setTitle('Roll some numbers!')
  9.             ->setColor($this->settings['discord']['colors']['grey'])
  10.             ->setTimestamp();
  11.  
  12.         if (!isset($args[0])) {
  13.             $message->channel
  14.                 ->send('', [
  15.                     'embed' => $embed
  16.                         ->setColor($this->settings['discord']['colors']['red'])
  17.                         ->setDescription('Missing the *range* parameter.'),
  18.                 ])
  19.                 ->done(
  20.                     null,
  21.                     function ($e) use ($message, $embed) {
  22.                         $this->channelMessageSendError($e, $message->channel, null, $embed);
  23.                     }
  24.                 );
  25.             return;
  26.         }
  27.  
  28.         if (strstr($args[0], '-')) {
  29.             list($start, $stop) = explode('-', $args[0]);
  30.         }
  31.  
  32.         if (isset($args[1])) {
  33.             $rolls = intval($args[1]) ?: 1;
  34.         }
  35.  
  36.         $start = $start ?? 1;
  37.         $stop = $stop ?? $args[0];
  38.         $rolls = $rolls ?? 1;
  39.  
  40.         if ($start >= $stop) {
  41.             $message->channel
  42.                 ->send('', [
  43.                     'embed' => $embed
  44.                         ->setColor($this->settings['discord']['colors']['red'])
  45.                         ->setDescription('The *start* digit cannot be higher than the *end* digit of a *range*.'),
  46.                 ])
  47.                 ->done(
  48.                     null,
  49.                     function ($e) use ($message, $embed) {
  50.                         $this->channelMessageSendError($e, $message->channel, null, $embed);
  51.                     }
  52.                 );
  53.             return;
  54.         }
  55.  
  56.         if (($stop - $start) <= $rolls) {
  57.             $message->channel
  58.                 ->send('', [
  59.                     'embed' => $embed
  60.                         ->setColor($this->settings['discord']['colors']['red'])
  61.                         ->setDescription('You cannot have more *rolls* than the *range* would allow.'),
  62.                 ])
  63.                 ->done(
  64.                     null,
  65.                     function ($e) use ($message, $embed) {
  66.                         $this->channelMessageSendError($e, $message->channel, null, $embed);
  67.                     }
  68.                 );
  69.             return;
  70.         }
  71.  
  72.         for ($i = 1; $i <= $rolls; $i++) {
  73.             $selected = false;
  74.  
  75.             while ($selected === false) {
  76.                 $int = random_int($start, $stop);
  77.  
  78.                 if (in_array($int, $random)) {
  79.                     continue;
  80.                 }
  81.  
  82.                 $selected = $int;
  83.             }
  84.  
  85.             $random[$i] = $selected;
  86.         }
  87.  
  88.         foreach ($random as $pos => $int) {
  89.             $response[] = "{$pos}. Rolled *{$int}*";
  90.         }
  91.  
  92.         $message->channel
  93.             ->send('', [
  94.                 'embed' => $embed
  95.                     ->addField('Rolls', implode("\n", $response), false)
  96.             ])
  97.             ->done(
  98.                 null,
  99.                 function ($e) use ($message, $embed) {
  100.                     $this->channelMessageSendError($e, $message->channel, null, $embed);
  101.                 }
  102.             );
  103.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement