benbot/src/Commands/Emails.php

128 lines
4.0 KiB
PHP
Raw Normal View History

2017-04-10 05:27:58 +00:00
<?php
2017-05-01 19:33:37 +00:00
2017-04-10 05:27:58 +00:00
namespace BenBot\Commands;
use BenBot\Utils;
2017-04-29 22:32:40 +00:00
final class Emails
{
2017-04-10 05:27:58 +00:00
private static $bot;
public static function register(&$that)
{
self::$bot = $that;
2017-04-24 02:52:19 +00:00
$emailcmd = self::$bot->registerCommand('email', [__CLASS__, 'sendMail'], [
'description' => 'send an email',
2017-05-01 19:33:37 +00:00
'usage' => '[@user] <message>',
'aliases' => [
2017-04-24 02:52:19 +00:00
'mail',
],
]);
2017-05-01 19:33:37 +00:00
$emailcmd->registerSubCommand('save', [__CLASS__, 'saveEmailAddress'], [
2017-04-24 02:52:19 +00:00
'description' => 'save an email address',
2017-05-01 19:33:37 +00:00
'usage' => '[@user] <your_email_here@example.com>',
2017-04-24 02:52:19 +00:00
]);
2017-05-01 19:33:37 +00:00
$emailcmd->registerSubCommand('show', [__CLASS__, 'showEmail'], [
2017-04-24 04:13:56 +00:00
'description' => 'show a saved email address',
2017-05-01 19:33:37 +00:00
'usage' => '[@user]',
2017-04-24 04:13:56 +00:00
]);
2017-05-01 19:33:37 +00:00
$emailcmd->registerSubCommand('remove', [__CLASS__, 'rmEmail'], [
2017-04-24 06:05:43 +00:00
'description' => 'remove your saved email',
2017-05-01 19:33:37 +00:00
'aliases' => [
2017-04-24 06:05:43 +00:00
'rm',
'clear',
'stop',
'delete',
'del',
],
]);
2017-04-10 05:27:58 +00:00
2017-05-01 19:33:37 +00:00
echo __CLASS__.' registered', PHP_EOL;
2017-04-10 05:27:58 +00:00
}
2017-04-24 02:52:19 +00:00
public static function sendMail($msg, $args)
{
$recipients = [];
if (count($msg->mentions) === 0) {
$recipients[] = $msg->author->id;
} else {
foreach ($msg->mentions as $mention) {
$recipients[] = $mention->id;
}
}
2017-05-01 19:33:37 +00:00
$to = '';
2017-04-24 02:52:19 +00:00
foreach ($recipients as $recipient) {
if (isset(self::$bot->emails[$recipient])) {
2017-05-01 19:33:37 +00:00
$to .= self::$bot->emails[$recipient].';';
2017-04-24 02:52:19 +00:00
} else {
return "no email found for <@$recipient>. you can save an email with `;email save <@user>`";
}
}
2017-05-01 19:33:37 +00:00
$body = implode(' ', $args);
2017-04-24 02:52:19 +00:00
$from = "From: {$msg->channel->guild->name} {$msg->author->username} benbot <{$msg->author->username}@{$msg->channel->guild->name}.benbot>";
if (mail($to, 'BenBot message', $body, $from)) {
2017-05-01 19:33:37 +00:00
return 'message sent successfully';
2017-04-24 02:52:19 +00:00
}
}
public static function saveEmailAddress($msg, $args)
{
if (count($msg->mentions) === 0) {
$id = Utils::getUserIDFromMsg($msg);
} elseif (count($msg->mentions) === 1) {
foreach ($msg->mentions as $mention) {
$id = $mention->id;
}
array_shift($args);
} else {
2017-05-01 19:33:37 +00:00
return 'you can set the email for only one person.';
2017-04-24 02:52:19 +00:00
}
self::$bot->emails[$id] = $args[0];
2017-05-01 19:33:37 +00:00
2017-04-24 02:52:19 +00:00
return "email for <@$id> set to {$args[0]}";
}
2017-04-24 04:13:56 +00:00
public static function showEmail($msg, $args)
{
if (count($msg->mentions) === 0) {
$id = Utils::getUserIDFromMsg($msg);
if (isset(self::$bot->emails[$id])) {
2017-05-01 19:33:37 +00:00
return 'your email is '.self::$bot->emails[$id];
2017-04-24 04:13:56 +00:00
} else {
return "you don't have an email set. use `;email save <your_email_here@domain.tld>`";
}
} else {
2017-05-01 19:33:37 +00:00
$response = '';
2017-04-24 04:13:56 +00:00
foreach ($msg->mentions as $mention) {
if (isset(self::$bot->emails[$mention->id])) {
2017-05-01 19:33:37 +00:00
$response .= "{$mention}'s email is ".self::$bot->emails[$mention->id]."\n";
2017-04-24 04:13:56 +00:00
} else {
2017-04-24 05:07:54 +00:00
$response .= "no email found for {$mention}\n";
2017-04-24 04:13:56 +00:00
}
}
2017-05-01 19:33:37 +00:00
2017-04-24 04:13:56 +00:00
return $response;
}
}
2017-04-24 06:05:43 +00:00
public static function rmEmail($msg, $args)
{
if (count($msg->mentions) === 0) {
$id = Utils::getUserIDFromMsg($msg);
if (isset(self::$bot->emails[$id])) {
unset(self::$bot->emails[$id]);
2017-05-01 19:33:37 +00:00
return 'your email has been removed';
2017-04-24 06:05:43 +00:00
} else {
return "you didn't have an email saved";
}
} else {
return "you can't remove someone else's email";
}
}
2017-04-10 05:27:58 +00:00
}