Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord Integration! Sync your roles between your NamelessMC site and your Discord Guild. #1764

Merged
merged 23 commits into from
Jul 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
af07152
Initial commit, big additions. Linking/verifying discord id is complete
tadhgboyle Jul 21, 2020
36497b7
Update admin.php
KaspianDev Jul 21, 2020
fc1ec5e
Merge pull request #1757 from kacperleague9/patch-1
tadhgboyle Jul 21, 2020
54b2b70
Better validation errors + language updating
tadhgboyle Jul 21, 2020
9e7505e
Apply role change on user edit page - still some things to flush out …
tadhgboyle Jul 21, 2020
5f5b9fd
flush out some edge cases
tadhgboyle Jul 21, 2020
010aee0
Add role in discord upon account validation & more
tadhgboyle Jul 21, 2020
68c3f84
Add api endpoint (need to test) & a lot of TODOs
tadhgboyle Jul 22, 2020
921787b
Merge in announcements branch
tadhgboyle Jul 22, 2020
51ef1ad
fix announcements a bit on Default template
tadhgboyle Jul 22, 2020
b43053a
define bot url for easier move to self-hosting
tadhgboyle Jul 22, 2020
50460e2
Add discord->website (adding) sync & lots of the TODOs
tadhgboyle Jul 22, 2020
f78b6bf
Add discord role removal api endpoint
tadhgboyle Jul 22, 2020
55851bc
Add logging od discord role add/remove & fix log table
tadhgboyle Jul 22, 2020
eb6a7c9
Add api_url to discord request for extra security & add todo's for er…
tadhgboyle Jul 24, 2020
d8206fa
More API error handling for discord bot
tadhgboyle Jul 24, 2020
e2490e1
Add boilerplate error handling if bot throws error
tadhgboyle Jul 24, 2020
4df7177
oops
tadhgboyle Jul 24, 2020
e01722f
Add error messages for all discord linking
tadhgboyle Jul 24, 2020
558c35a
If they have a role on the site, give it to them when they verify the…
tadhgboyle Jul 24, 2020
6ea9787
Resolve Sam's review
tadhgboyle Jul 25, 2020
16239ef
Add info msgs so people know where to get discord IDs
tadhgboyle Jul 25, 2020
8b7eeff
Add all language translations and update NMC version strings
tadhgboyle Jul 25, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Resolve Sam's review
  • Loading branch information
tadhgboyle committed Jul 25, 2020
commit 6ea97874e7738211bb450a206587a7d28a56a72e
3 changes: 2 additions & 1 deletion core/classes/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ class Log{
],
'discord' => [
'role_add' => 'discord_role_add',
'role_remove' => 'discord_role_remove'
'role_remove' => 'discord_role_remove',
'upon_validation_error' => 'upon_validation_error'
]
];

Expand Down
8 changes: 8 additions & 0 deletions core/includes/updates/200-pr7.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@
} catch (Exception $e) {
echo $e->getMessage() . '<br />';
}
try {
$queries->create('settings', array(
'name' => 'discord_bot_url',
'value' => 'http://bot.tadhgboyle.dev:8001'
));
} catch (Exception $e) {
echo $e->getMessage() . '<br />';
}
try {
$queries->alterTable('groups', '`discord_role_id`', "bigint(18) NULL DEFAULT NULL");
} catch (Exception $e) {
Expand Down
9 changes: 5 additions & 4 deletions core/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@
// Friendly URLs?
define('FRIENDLY_URLS', Config::get('core/friendly'));

// Define discord bot url for easy change if they choose to selfhost
define('BOT_URL', 'http://bot.tadhgboyle.dev:8001');

// Set up cache
$cache = new Cache(array('name' => 'nameless', 'extension' => '.cache', 'path' => ROOT_PATH . '/cache/'));

Expand Down Expand Up @@ -158,9 +155,13 @@
// Get the Nameless version
$nameless_version = $queries->getWhere('settings', array('name', '=', 'nameless_version'));
$nameless_version = $nameless_version[0]->value;

define('NAMELESS_VERSION', $nameless_version);

// Get the Bot URL
$bot_url = $queries->getWhere('settings', array('name', '=', 'discord_bot_url'));
$bot_url = $bot_url[0]->value;
define('BOT_URL', $bot_url);

// User initialisation
$user = new User();
// Do they need logging in (checked remember me)?
Expand Down
17 changes: 16 additions & 1 deletion core/installation/views/installer.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,11 @@
'value' => 1
));

tadhgboyle marked this conversation as resolved.
Show resolved Hide resolved
$queries->create('settings', array(
'name' => 'discord_bot_url',
'value' => 'http://bot.tadhgboyle.dev:8001'
));

$queries->create('settings', array(
'name' => 'discord_integration',
'value' => 0
Expand Down Expand Up @@ -1575,7 +1580,7 @@
}

$queries->update('groups', 1, array('permissions' => '{"usercp.messaging":1,"usercp.signature":1,"usercp.nickname":1,"usercp.private_profile":1,"usercp.profile_banner":1}'));
$queries->update('groups', 2, array('permissions' => '{"admincp.core":1,"admincp.core.api":1,"admincp.core.general":1,"admincp.core.avatars":1,"admincp.core.fields":1,"admincp.core.debugging":1,"admincp.core.emails":1,"admincp.core.navigation":1,"admincp.core.reactions":1,"admincp.core.registration":1,"admincp.core.social_media":1,"admincp.core.terms":1,"admincp.errors":1,"admincp.integrations":1,"admincp.minecraft":1,"admincp.minecraft.authme":1,"admincp.minecraft.verification":1,"admincp.minecraft.servers":1,"admincp.minecraft.query_errors":1,"admincp.minecraft.banners":1,"admincp.modules":1,"admincp.pages":1,"admincp.pages.metadata":1,"admincp.security":1,"admincp.security.acp_logins":1,"admincp.security.template":1,"admincp.sitemap":1,"admincp.styles":1,"admincp.styles.panel_templates":1,"admincp.styles.templates":1,"admincp.styles.templates.edit":1,"admincp.styles.images":1,"admincp.update":1,"admincp.users":1,"admincp.users.edit":1,"admincp.groups":1,"admincp.groups.self":1,"admincp.widgets":1,"modcp.ip_lookup":1,"modcp.punishments":1,"modcp.punishments.warn":1,"modcp.punishments.ban":1,"modcp.punishments.banip":1,"modcp.punishments.revoke":1,"modcp.reports":1,"modcp.profile_banner_reset":1,"usercp.messaging":1,"usercp.signature":1,"admincp.forums":1,"usercp.private_profile":1,"usercp.nickname":1,"usercp.profile_banner":1,"profile.private.bypass":1, "admincp.security.all":1,"admincp.core.hooks":1}'));
$queries->update('groups', 2, array('permissions' => '{"admincp.core":1,"admincp.core.api":1,"admincp.core.general":1,"admincp.core.avatars":1,"admincp.core.fields":1,"admincp.core.debugging":1,"admincp.core.emails":1,"admincp.core.navigation":1,"admincp.core.announcements":1,"admincp.core.reactions":1,"admincp.core.registration":1,"admincp.core.social_media":1,"admincp.core.terms":1,"admincp.errors":1,"admincp.integrations":1,"admincp.discord":1,"admincp.minecraft":1,"admincp.minecraft.authme":1,"admincp.minecraft.verification":1,"admincp.minecraft.servers":1,"admincp.minecraft.query_errors":1,"admincp.minecraft.banners":1,"admincp.modules":1,"admincp.pages":1,"admincp.pages.metadata":1,"admincp.security":1,"admincp.security.acp_logins":1,"admincp.security.template":1,"admincp.sitemap":1,"admincp.styles":1,"admincp.styles.panel_templates":1,"admincp.styles.templates":1,"admincp.styles.templates.edit":1,"admincp.styles.images":1,"admincp.update":1,"admincp.users":1,"admincp.users.edit":1,"admincp.groups":1,"admincp.groups.self":1,"admincp.widgets":1,"modcp.ip_lookup":1,"modcp.punishments":1,"modcp.punishments.warn":1,"modcp.punishments.ban":1,"modcp.punishments.banip":1,"modcp.punishments.revoke":1,"modcp.reports":1,"modcp.profile_banner_reset":1,"usercp.messaging":1,"usercp.signature":1,"admincp.forums":1,"usercp.private_profile":1,"usercp.nickname":1,"usercp.profile_banner":1,"profile.private.bypass":1, "admincp.security.all":1,"admincp.core.hooks":1}'));
$queries->update('groups', 3, array('permissions' => '{"modcp.ip_lookup":1,"modcp.punishments":1,"modcp.punishments.warn":1,"modcp.punishments.ban":1,"modcp.punishments.banip":1,"modcp.punishments.revoke":1,"modcp.reports":1,"admincp.users":1,"modcp.profile_banner_reset":1,"usercp.messaging":1,"usercp.signature":1,"usercp.private_profile":1,"usercp.nickname":1,"usercp.profile_banner":1,"profile.private.bypass":1}'));
}
} catch(Exception $e){
Expand Down Expand Up @@ -2507,6 +2512,16 @@
'value' => '1'
));

$queries->create('settings', array(
'name' => 'discord_integration',
'value' => 0,
));

$queries->create('settings', array(
'name' => 'discord_bot_url',
'value' => 'http://bot.tadhgboyle.dev:8001'
));

// Templates
$queries->create('templates', array(
'name' => 'Default',
Expand Down
7 changes: 5 additions & 2 deletions custom/languages/EnglishUK/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,11 @@
'discord_role_id' => 'Discord Role ID',
'discord_role_id_numeric' => 'Discord Role ID must be numeric.',
'discord_role_id_length' => 'Discord Role ID must be 18 digits long.',
'discord_guild_id_updated' => 'Discord Server ID was updated successfully.',
'discord_settings_updated' => 'Your Discord settings were updated successfully.',
'discord_guild_id_required' => 'Please enter your Discord Server ID for Discord integration to work.',
'discord_invalid_api_url' => 'Looks like your API URL has changed. Please get the guild owner to update the URL with the Nameless Link bot.'
'discord_bot_url' => 'Discord Bot URL',
'discord_bot_url_info' => 'The location which your custom instance of the Nameless Link bot. Only change this value if you know what you are doing!',
'discord_bot_url_required' => 'Please enter your Bot URL for Discord integration to work.',
'discord_invalid_api_url' => 'Looks like your API URL has changed. Please get the guild owner to update the URL with the Nameless Link bot.',
// TODO: All language files
tadhgboyle marked this conversation as resolved.
Show resolved Hide resolved
);
3 changes: 2 additions & 1 deletion custom/languages/EnglishUK/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,5 +243,6 @@
'discord_invalid_id' => 'That Discord User ID is invalid.',
'discord_already_pending' => 'You already have a pending verification.',
'discord_database_error' => 'The Nameless Link database is currently down. Please try again later.',
'discord_unknown_error' => 'There was an unknown error while communicating with the bot. Please contact an administrator.',
'discord_communication_error' => 'There was an error while communicating with the Discord Bot. Please ensure the bot is running and your Bot URL is correct.',
'discord_unknown_error' => 'There was an unknown error while syncing Discord roles. Please contact an administrator.',
);
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@

{if $DISCORD_ENABLED eq 1}
<hr>
<form id="guildId" action="" method="post">
<form id="settings" action="" method="post">
<div class="form-group">
<label for="inputGuildId">{$GUILD_ID}</label>
<input type="number" name="guild_id" class="form-control" id="inputGuildId" value="{$GUILD_ID_VALUE}">
<input type="hidden" name="token" value="{$TOKEN}">
</div>
<div class="form-group">
<label for="inputBotUrl">{$BOT_URL} <span class="badge badge-warning" data-toggle="popover" data-title="{$INFO}" data-content="{$BOT_URL_INFO|escape}"><i class="fa fa-exclamation-triangle"></i></label>
<input type="text" name="bot_url" class="form-control" id="inputBotUrl" value="{$BOT_URL_VALUE}">
</div>
<div class="form-group">
<input type="hidden" name="token" value="{$TOKEN}">
<input type="submit" value="{$SUBMIT}" class="btn btn-primary">
</div>
</form>
Expand Down
2 changes: 1 addition & 1 deletion custom/templates/Default/user/settings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
<form action="" method="post">
<div class="form-group">
<label for="discord_id">{$DISCORD_ID}</label>
<input type="number" name="discord_id" id="discord_id" class="form-control" value={$DISCORD_ID_VALUE}>
<input type="number" name="discord_id" id="discord_id" class="form-control" value="{$DISCORD_ID_VALUE}">
</div>
<input type="hidden" name="action" value="discord">
<input type="hidden" name="token" value="{$TOKEN}">
Expand Down
2 changes: 1 addition & 1 deletion custom/templates/DefaultRevamp/user/settings.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
<form action="" method="post" class="ui form">
<div class="field">
<label for="discord_id">{$DISCORD_ID}</label>
<input type="number" name="discord_id" id="discord_id" class="form-control" value={$DISCORD_ID_VALUE}>
<input type="number" name="discord_id" id="discord_id" class="form-control" value="{$DISCORD_ID_VALUE}">
</div>
<input type="hidden" name="action" value="discord">
<input type="hidden" name="token" value="{$TOKEN}">
Expand Down
12 changes: 8 additions & 4 deletions modules/Core/hooks/ValidateHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ public static function validatePromote($params = array()){
$user_query = $queries->getWhere('users', array('id', '=', $params['user_id']));

// Discord integration is enabled
if ($queries->getWhere('settings', array('name', '=', 'discord_integration'))[0]->value == '1') {
$setting = $queries->getWhere('settings', array('name', '=', 'discord_integration'));
if ($setting[0]->value == '1') {
// They have a valid discord Id
if ($user_query->discord_id != null && $user_query->discord_id != 010) {
$group_discord_id = $queries->getWhere('groups', array('id', '=', VALIDATED_DEFAULT))[0]->discord_role_id;
$group_discord_id = $queries->getWhere('groups', array('id', '=', VALIDATED_DEFAULT));
$group_discord_id = $group_discord_id[0]->discord_role_id;

if ($group_discord_id != null) {
$bot_url = BOT_URL;
$api_key = $queries->getWhere('settings', array('name', '=', 'mc_api_key'))[0]->value;
$api_key = $queries->getWhere('settings', array('name', '=', 'mc_api_key'));
$api_key = $api_key[0]->value;
$api_url = rtrim(Util::getSelfURL(), '/') . rtrim(URL::build('/api/v2/' . Output::getClean($api_key), '', 'non-friendly'), '/');
$full_url = $bot_url . '/roleChange?id=' . $user_query->discord_id . '&guild_id=' . $queries->getWhere('settings', array('name', '=', 'discord'))[0]->value . '&ole=' . $group_discord_id;
$guild_id = $queries->getWhere('settings', array('name', '=', 'discord'));
$full_url = $bot_url . '/roleChange?id=' . $user_query->discord_id . '&guild_id=' . $guild_id[0]->value . '&role=' . $group_discord_id . '&api_url=' . $api_url;
file_get_contents($full_url);
tadhgboyle marked this conversation as resolved.
Show resolved Hide resolved
}
}
Expand Down
28 changes: 24 additions & 4 deletions modules/Core/pages/panel/discord.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
'min' => 18,
'max' => 18,
'numeric' => true
),
'bot_url' => array(
'required' => true,
'min' => 10,
'max' => 2048
)
));

Expand All @@ -68,7 +73,12 @@
$queries->update('settings', $discord_id, array(
'value' => Input::get('guild_id')
));
$success = $language->get('admin', 'discord_guild_id_updated');
$bot_url = $queries->getWhere('settings', array('name', '=', 'discord_bot_url'));
$bot_url = $bot_url[0]->id;
$queries->update('settings', $bot_url, array(
'value' => Output::getClean(Input::get('bot_url'))
));
$success = $language->get('admin', 'discord_settings_updated');
} else {
// Validation errors
foreach ($validation->errors() as $validation_error) {
Expand All @@ -77,7 +87,11 @@
} else if (strpos($validation_error, 'numeric') !== false) {
$errors[] = $language->get('admin', 'discord_id_numeric');
} else if (strpos($validation_error, 'required') !== false) {
$errors[] = $language->get('admin', 'discord_guild_id_required');
if (strpos($validation_error, 'guild_id') !== false) {
$errors[] = $language->get('admin', 'discord_guild_id_required');
} else if (strpos($validation_error, 'bot_url') !== false) {
$errors[] = $language->get('admin', 'discord_bot_url_required');
}
}
}
}
Expand All @@ -104,24 +118,30 @@
));

// Check if Discord integration is enabled
$discord_enabled = $queries->getWhere('settings', array('name', '=', 'discord_integration'))[0]->value;
$discord_enabled = $queries->getWhere('settings', array('name', '=', 'discord_integration'));
$discord_enabled = $discord_enabled[0]->value;

$smarty->assign(array(
'PARENT_PAGE' => PARENT_PAGE,
'DASHBOARD' => $language->get('admin', 'dashboard'),
'INTEGRATIONS' => $language->get('admin', 'integrations'),
'DISCORD' => $language->get('admin', 'discord'),
'PAGE' => PANEL_PAGE,
'INFO' => $language->get('general', 'info'),
'TOKEN' => Token::get(),
'SUBMIT' => $language->get('general', 'submit'),
'ENABLE_DISCORD_INTEGRATION' => $language->get('admin', 'enable_discord_integration'),
'DISCORD_ENABLED' => $discord_enabled
));

if ($discord_enabled == 1) {
$guild_id = $queries->getWhere('settings', array('name', '=', 'discord'));
$smarty->assign(array(
'GUILD_ID' => $language->get('admin', 'discord_id'),
'GUILD_ID_VALUE' => $queries->getWhere('settings', array('name', '=', 'discord'))[0]->value,
'GUILD_ID_VALUE' => $guild_id[0]->value,
'BOT_URL' => $language->get('admin', 'discord_bot_url'),
'BOT_URL_VALUE' => BOT_URL,
'BOT_URL_INFO' => $language->get('admin', 'discord_bot_url_info')
));
}

Expand Down
9 changes: 4 additions & 5 deletions modules/Core/pages/panel/groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@
$page_title = $language->get('admin', 'groups');
require_once(ROOT_PATH . '/core/templates/backend_init.php');

$discord_integration = $queries->getWhere('settings', array('name', '=', 'discord_integration'))[0]->value;
$discord_integration = $queries->getWhere('settings', array('name', '=', 'discord_integration'));
$discord_integration = $discord_integration[0]->value;

// Load modules + template
Module::loadPage($user, $pages, $cache, $smarty, array($navigation, $cc_nav, $mod_nav), $widgets);
Expand Down Expand Up @@ -166,7 +167,7 @@
'NAME' => $language->get('admin', 'name'),
'GROUP_HTML' => $language->get('admin', 'group_html'),
'GROUP_USERNAME_COLOUR' => $language->get('admin', 'group_username_colour'),
'DISCORD_INTEGRATION' => false,
'DISCORD_INTEGRATION' => $discord_integration ? true : false,
'GROUP_ORDER' => $language->get('admin', 'group_order'),
'STAFF_GROUP' => $language->get('admin', 'group_staff'),
'STAFF_CP' => $language->get('admin', 'can_view_staffcp'),
Expand All @@ -175,7 +176,6 @@

if ($discord_integration) {
$smarty->assign(array(
'DISCORD_INTEGRATION' => true,
'DISCORD_ROLE_ID' => $language->get('admin', 'discord_role_id'),
'DISCORD_ROLE_ID_VALUE' => $group->discord_role_id
));
Expand Down Expand Up @@ -342,7 +342,7 @@
'GROUP_HTML_VALUE' => Output::getClean($group->group_html),
'GROUP_USERNAME_COLOUR' => $language->get('admin', 'group_username_colour'),
'GROUP_USERNAME_COLOUR_VALUE' => Output::getClean($group->group_username_css),
'DISCORD_INTEGRATION' => false,
'DISCORD_INTEGRATION' => $discord_integration ? true : false,
'STAFF_GROUP' => $language->get('admin', 'group_staff'),
'STAFF_GROUP_VALUE' => $group->staff,
'STAFF_CP' => $language->get('admin', 'can_view_staffcp'),
Expand All @@ -362,7 +362,6 @@

if ($discord_integration) {
$smarty->assign(array(
'DISCORD_INTEGRATION' => true,
'DISCORD_ROLE_ID' => $language->get('admin', 'discord_role_id'),
'DISCORD_ROLE_ID_VALUE' => $group->discord_role_id
));
Expand Down
Loading