Skip to content

Commit f7b2c3b

Browse files
committed
Code Clean.
1 parent 21ebb5d commit f7b2c3b

19 files changed

+265
-339
lines changed

.env.dist

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
BOT_NAME=
66
BOT_MENTION=
77
BOT_TOKEN=
8-
BOT_USAGE_KEY=
8+
BOT_KEY=
99

1010
#
1111
# Ser Aymeric

config/services.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ services:
2323
App\Controller\:
2424
resource: '../src/Controller'
2525
tags: ['controller.service_arguments']
26+
27+
App\EventListener\RequestListener:
28+
tags:
29+
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }

src/Command/MogCommand.php

-54
This file was deleted.

src/Command/RandomRole.php

-44
This file was deleted.

src/Command/RunCommand.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
class RunCommand extends Command
1111
{
1212
/** @var MogNet */
13-
private $mogNet;
13+
private $mog;
1414

15-
public function __construct(MogNet $mogNet)
15+
public function __construct(MogNet $mog)
1616
{
17-
$this->mogNet = $mogNet;
17+
$this->mog = $mog;
1818
parent::__construct();
1919
}
2020

@@ -28,6 +28,6 @@ protected function configure()
2828
protected function execute(InputInterface $input, OutputInterface $output)
2929
{
3030
$output->writeln('<info>Mog Discord Bot</info>');
31-
$this->mogNet->run();
31+
$this->mog->run();
3232
}
3333
}

src/Command/SerAymericCommand.php

-32
This file was deleted.

src/Controller/ControllerTrait.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\Controller;
4+
5+
trait ControllerTrait
6+
{
7+
/**
8+
* Get a discord room from the content in the request
9+
*/
10+
public function getChannelFromRequestContent(\stdClass $content)
11+
{
12+
$channel = $content->channel ?? null;
13+
14+
if ($channel === null) {
15+
throw new \Exception('A channel must be provided in the request.');
16+
}
17+
18+
return $channel;
19+
}
20+
}

src/Controller/MessageController.php

+15-31
Original file line numberDiff line numberDiff line change
@@ -2,59 +2,43 @@
22

33
namespace App\Controller;
44

5-
use App\Service\Directory\Rooms;
5+
use App\Service\Api\Response;
66
use App\Service\MogNet\Messages\Text;
7-
use App\Service\MogNet\MogRest;
7+
use App\Service\MogRest\MogRest;
88
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
99
use Symfony\Component\HttpFoundation\Request;
10-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1110
use Symfony\Component\Routing\Annotation\Route;
1211

1312
class MessageController extends AbstractController
1413
{
14+
use ControllerTrait;
15+
1516
/** @var MogRest */
1617
private $mog;
1718

1819
public function __construct(MogRest $mog)
1920
{
2021
$this->mog = $mog;
2122
}
22-
23-
/**
24-
* @Route("/")
25-
*/
26-
public function home()
27-
{
28-
return $this->json('Mog, The XIVAPI.com Discord Bot');
29-
}
30-
23+
3124
/**
3225
* @Route("/say")
3326
*/
34-
public function post(Request $request)
27+
public function say(Request $request)
3528
{
36-
if ($request->get('key') != getenv('BOT_USAGE_KEY')) {
37-
throw new NotFoundHttpException();
38-
}
39-
40-
$request = \GuzzleHttp\json_decode($request->getContent());
29+
$content = json_decode($request->getContent());
4130

42-
if (!isset($request->message)) {
43-
throw new NotFoundHttpException();
44-
}
45-
46-
$message = trim($request->message);
47-
48-
if (empty($message)) {
49-
return $this->json([ false, 'No message provided' ]);
50-
}
31+
// grab the discord channel from the request
32+
$channel = $this->getChannelFromRequestContent($content);
5133

5234
// grab feedback json
53-
$message = new Text($message);
54-
$room = isset($request->room) ? Rooms::get($request->room) : Rooms::ADMIN_MOG;
35+
$message = new Text($content->message ?? false);
5536

5637
// post it to the chat
57-
$this->mog->message($room, $message);
58-
return $this->json([ true, 'Message sent successfully' ]);
38+
$this->mog->sendMessage($channel, $message);
39+
40+
return $this->json(
41+
(new Response(true, 'Message Sent'))->toArray()
42+
);
5943
}
6044
}

src/Controller/RoleController.php

+1-46
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace App\Controller;
44

5-
use App\Service\MogNet\MogRest;
5+
use App\Service\MogRest\MogRest;
66
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
7-
use Symfony\Component\HttpFoundation\Request;
87
use Symfony\Component\Routing\Annotation\Route;
98

109
class RoleController extends AbstractController
@@ -18,48 +17,4 @@ public function __construct(MogRest $mog)
1817
{
1918
$this->mog = $mog;
2019
}
21-
/**
22-
* @Route("/role/ArcaneDisgea")
23-
*/
24-
public function post(Request $request)
25-
{
26-
return $this->json([
27-
[
28-
[
29-
[
30-
[
31-
[
32-
[
33-
[
34-
['kill joy']
35-
]
36-
]
37-
]
38-
]
39-
]
40-
]
41-
]
42-
]);
43-
44-
$name = strip_tags(trim(substr($request->get('name'), 0, 64)));
45-
46-
if (strtolower($name) == 'example') {
47-
return $this->json(0);
48-
}
49-
50-
$this->mog->client()->guild->modifyGuildRole([
51-
'guild.id' => 474518001173921794,
52-
'role.id' => 516086224738451466,
53-
'name' => $name,
54-
'mentionable' => true,
55-
'hoist' => true,
56-
]);
57-
58-
$this->mog->client()->channel->createMessage([
59-
'channel.id' => 474519195963490305,
60-
'content' => '<@&516086224738451466> Your role was set by some randomer on the internet to: '. $name,
61-
]);
62-
63-
return $this->json(1);
64-
}
6520
}

src/Controller/SerAymericController.php

+16-18
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,33 @@
22

33
namespace App\Controller;
44

5+
use App\Service\Api\Response;
6+
use App\Service\SerAymeric\SerAymeric;
57
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
68
use Symfony\Component\HttpFoundation\Request;
7-
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
89
use Symfony\Component\Routing\Annotation\Route;
910

1011
class SerAymericController extends AbstractController
1112
{
13+
/** @var SerAymeric */
14+
private $serAymeric;
15+
16+
public function __construct(SerAymeric $serAymeric)
17+
{
18+
$this->serAymeric = $serAymeric;
19+
}
20+
1221
/**
13-
* @Route("/say")
22+
* @Route("/ser-aymeric/say")
1423
*/
1524
public function post(Request $request)
1625
{
17-
if ($request->get('key') != getenv('BOT_USAGE_KEY')) {
18-
throw new NotFoundHttpException();
19-
}
20-
21-
$request = \GuzzleHttp\json_decode($request->getContent());
22-
23-
if (!isset($request->message)) {
24-
throw new NotFoundHttpException();
25-
}
26-
27-
$message = trim($request->message);
28-
29-
if (empty($message)) {
30-
return $this->json([ false, 'No message provided' ]);
31-
}
32-
26+
$content = json_decode($request->getContent());
3327

28+
$this->serAymeric->sendDirectMessage(42667995159330816, $content->message);
3429

30+
return $this->json(
31+
(new Response(true, 'Message Sent'))->toArray()
32+
);
3533
}
3634
}

0 commit comments

Comments
 (0)