Skip to content

Instantly share code, notes, and snippets.

@Animeshz
Created August 16, 2018 14:17
Show Gist options
  • Save Animeshz/0c4ab74da9a4d26332e12f7183793f8e to your computer and use it in GitHub Desktop.
Save Animeshz/0c4ab74da9a4d26332e12f7183793f8e to your computer and use it in GitHub Desktop.
<?php
/**
* apply on a form
*
* @package ClusterPlus
* @author Animesh Sahu <animeshsahu19@yahoo.com>
*/
return function ($client) {
return (new class($client) extends \CharlotteDunois\Livia\Commands\Command {
function __construct(\CharlotteDunois\Livia\LiviaClient $client) {
parent::__construct($client, [
'name' => 'apply',
'alias' => [ 'apply-form' ],
'group' => 'custom',
'description' => 'Shows forms',
'details' => 'Form stores an array of data filled by a user. This command must be run in a guild/server',
'examples' => [ 'apply', 'apply-forms' ],
'guildOnly' => true,
'guarded' => true
]);
}
function run(\CharlotteDunois\Livia\CommandMessage $message, \ArrayObject $args, bool $fromPattern) {
$forms = $message->client->provider->get( $message->guild, 'forms' );
if( $forms !== null ) {
$q = 1;
foreach ($forms as $title => $questions) {
$titles[] = $q . '. ' . $title;
$q++;
}
$title = \implode( PHP_EOL, $titles );
$message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed( [
'description' => 'Form you want to fill, reply with number of form.' . PHP_EOL . $title,
'color' => '3447003'
])
]);
$q = 0;
$title = '';
$questions = [];
$answers = [];
$listener = function( \CharlotteDunois\Yasmin\Models\Message $msg ) use ( $message, &$title, $titles, $forms, &$q, &$questions, &$answers, &$listener )
{
if( $msg->author->__toString() === $message->message->author->__toString() && $msg->channel->__toString() === $message->message->channel->__toString() ){
if( $q === 0 ) {
$formNo = (int) trim($msg->content);
if( !empty( $formNo ) ){
if(isset($titles[($formNo-1)])) {
$title = $titles[($formNo-1)];
$title = str_replace("$formNo. ", '', $title);
$questions = $forms[$title];
$message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed([
'description' => 'TIP: For cancelling the form, send "cancel".' . PHP_EOL . $questions[0],
'color' => '3447003'
])
]);
++$q;
} else {
$message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed( [
'description' => 'Wrong argument, Cancelled command',
'color' => '3447003'
])
]);
return $message->client->removeListener( 'message', $listener );
}
} else {
$message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed( [
'description' => 'Wrong argument, Cancelled command',
'color' => '3447003'
])
]);
return $message->client->removeListener( 'message', $listener );
}
} else {
if( $q === count($questions) ) {
$answers[] = $msg->content;
$embed = new \CharlotteDunois\Yasmin\Models\MessageEmbed([
'color' => '3447003',
'description' => 'Are these information correct? say yes for submit or no for cancel.'
]);
for( $i=0; $i<count($questions); $i++ ) {
$embed->addField($questions[$i], $answers[$i]);
}
$message->say( '', [
'embed' => $embed
])
->done( function() use ( &$q ) {
++$q;
});
} elseif ( $q === (count($questions)+1) ) {
if( $msg->content === 'yes' ) {
$FormData = $message->client->provider->getFormData( $message->guild, $message->author );
if( $FormData === null ) {
$FormData = [$title => $answers];
} else {
$FormData[$title] = $answers;
}
$message->client->provider->setFormData( $message->guild, $message->author, $FormData );
return $message->client->removeListener( 'message', $listener );
} else {
$message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed( [
'description' => 'Cancelled form',
'color' => '3447003'
])
]);
return $message->client->removeListener( 'message', $listener );
}
} else {
if( $msg->content === 'cancel' ) {
unset( $q, $questions, $answers );
$message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed( [
'description' => 'Cancelled form',
'color' => '3447003'
])
]);
return $message->client->removeListener( 'message', $listener );
} else {
$answers[] = $msg->content;
return $message->say( '', [
'embed' => new \CharlotteDunois\Yasmin\Models\MessageEmbed( [
'description' => 'TIP: For cancelling the form, send "cancel".' . PHP_EOL . $questions[($q)],
'color' => '3447003'
])
])
->done( function() use ( &$q ) {
++$q;
});
}
}
}
}
};
$message->client->on( 'message', $listener );
}
}
});
};
<?php
/**
* You don't need to know what this does.
*
* @package ClusterPlus
* @author Animesh Sahu <animeshsahu19@yahoo.com>
*/
namespace ClusterPlus\custom;
/**
* Create custom mysql using asyncronous approach
*
*/
class MySQLExtender extends \CharlotteDunois\Livia\Providers\MySQLProvider
{
protected $formdata;
function __construct(\React\MySQL\ConnectionInterface $db)
{
$this->formdata = new \CharlotteDunois\Yasmin\Utils\Collection();
parent::__construct($db);
}
function init(\CharlotteDunois\Livia\LiviaClient $client) {
$this->runQuery('CREATE TABLE IF NOT EXISTS `formdata` (`guild` VARCHAR(20) NOT NULL, `values` TEXT NOT NULL, PRIMARY KEY (`guild`));')->then(function () {
return $this->runQuery('SELECT * FROM `formdata`')->then(function ($result) {
foreach($result->resultRows as $row) {
$this->loadFormDataRow($row);
}
return null;
});
});
return parent::init($client);
}
function formdataCreate($guild, &$formdata = array()) {
$guild = $this->getGuildID($guild);
return (new \React\Promise\Promise(function (callable $resolve, callable $reject) use ($guild, &$formdata) {
$this->runQuery('SELECT * FROM `formdata` WHERE `guild` = ?', array($guild))->then(function ($command) use ($guild, &$formdata, $resolve, $reject) {
if(empty($command->resultRows)) {
$this->formdata->set($guild, $formdata);
$this->runQuery('INSERT INTO `formdata` (`guild`, `value`) VALUES (?, ?)', array($guild, \json_encode($formdata)))->done($resolve, $reject);
} else {
$this->loadFormDataRow($command->resultRows[0]);
$resolve();
}
});
}));
}
function setupFormdataGuild($guild) {
$guild = $this->getGuildID($guild);
$formdata = $this->formdata->get($guild);
if(!$formdata) {
$this->formdataCreate($guild)->done(null, array($this->client, 'handlePromiseRejection'));
return;
}
}
protected function loadFormDataRow(array $row)
{
$values = \json_decode($row['value'], true);
if($values === null) {
$this->client->emit('warn', 'MySQLProvider couldn\'t parse the values stored for guild(in form data) "'.$row['guild'].'". Error: '.\json_last_error_msg());
return;
}
$values = new \ArrayObject($values, \ArrayObject::ARRAY_AS_PROPS);
$this->formdata->set($row['guild'], $values);
try {
$this->setupFormdataGuild($row['guild']);
} catch (\InvalidArgumentException $e) {
$this->formdata->delete($row['guild']);
$this->runQuery('DELETE FROM `formdata` WHERE `guild` = ?', array($row['guild']))->done(null, array($this->client, 'handlePromiseRejection'));
}
}
function getFormData($guild, string $key, $defaultValue = null) {
$guild = $this->getGuildID($guild);
if($this->formdata->get($guild) === null) {
$this->client->emit('warn', 'Formdata of specified guild is not loaded - loading row - returning default value');
$this->formdataCreate($guild);
return $defaultValue;
}
$formdata = $this->formdata->get($guild);
if(\array_key_exists($key, $formdata)) {
return $formdata[$key];
}
return $defaultValue;
}
function setFormData($guild, string $key, $value) {
$guild = $this->getGuildID($guild);
if($this->formdata->get($guild) === null) {
return $this->formdataCreate($guild)->then(function () use ($guild, $key, $value) {
$formdata = $this->formdata->get($guild);
$formdata[$key] = $value;
return $this->runQuery('UPDATE `formdata` SET `value` = ? WHERE `guild` = ?', array(\json_encode($formdata), $guild));
});
}
return (new \React\Promise\Promise(function (callable $resolve, callable $reject) use ($guild, $key, $value) {
$formdata = $this->formdata->get($guild);
$formdata[$key] = $value;
$this->runQuery('UPDATE `formdata` SET `value` = ? WHERE `guild` = ?', array(\json_encode($formdata), $guild))->done($resolve, $reject);
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment