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
Add api endpoint (need to test) & a lot of TODOs
  • Loading branch information
tadhgboyle committed Jul 22, 2020
commit 68c3f8420e51f9e5ef1a175ff224d2ac110fb1bb
5 changes: 5 additions & 0 deletions core/installation/views/installer.view.php
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,11 @@
'value' => 0
));

// TODO: verify i have all queries done.
// group discord id
// user discord id
// discord integration enabled

$queries->create('settings', array(
'name' => 'avatar_type',
'value' => 'helmavatar'
Expand Down
1 change: 1 addition & 0 deletions custom/languages/EnglishUK/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -645,4 +645,5 @@
'discord_role_id_length' => 'Discord Role ID must be 18 digits long.',
'discord_guild_id_updated' => 'Discord Server ID was updated successfully.',
'discord_guild_id_required' => 'Please enter your Discord Server ID for Discord integration to work.',
// TODO: All language files
tadhgboyle marked this conversation as resolved.
Show resolved Hide resolved
);
2 changes: 1 addition & 1 deletion custom/languages/EnglishUK/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,6 @@
'not_linked' => 'Not Linked',
'discord_id' => 'Discord User ID',
'discord_id_unlinked' => 'Successfully unlinked your Discord User ID.',
'discord_id_confirm' => 'Please send a private message containing only your username to "NamelessMC Bot#0761" to confirm your Discord User ID.',
'discord_id_confirm' => 'Please send a private message containing only your username to "Nameless Link#0761" to confirm your Discord User ID.',
'pending_link' => 'Pending'
);
48 changes: 42 additions & 6 deletions modules/Core/pages/api/v2/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -632,18 +632,54 @@ private function setDiscordId() {
} catch (Exception $e) {
$this->throwError(23, $this->_language->get('api', 'unable_to_create_report'));
}
} else $this->throwError(1, $this->_language->get('api', 'invalid_api_key'
));
} else $this->throwError(1, $this->_language->get('api', 'invalid_api_key'));
}

// TODO: If they turn off discord integration on website, but the bot is still active on server...
private function setGroupFromDiscord() {
// Param: id = discord user id
// Param: role_id = discord role id
// Param: discord user id
// Param: discord role id
if ($this->_validated === true) {
if (!isset($_POST) || empty($_POST)) {
$this->throwError(6, $this->_language->get('api', 'invalid_post_contents'));
}

$discord_user_id = $_POST['discord_user_id'];
$discord_role_id = $_POST['discord_role_id'];

try {
$this->_db = DB::getInstance();

// Error
$user = $this->_db->get('users', array('discord_id', '=', $discord_user_id));
if (!$user->count()) $this->throwError(16, $this->_language->get('api', 'unable_to_find_user'));
$group = $this->_db->get('groups', array('discord_role_id', '=', $discord_role_id));
if (!$group->count()) $this->throwError(16, $this->_language->get('api', 'unable_to_find_group'));

$user = $user->first()->id;
$group = $group->first()->id;

try {
$this->_db->update('users', $user, array(
'group_id' => $group
));
// TODO: If their main group was set to a group which was previously a secondary group, remove from their secondary groups
} catch (Exception $e) {
$this->throwError(18, $this->_language->get('api', 'unable_to_update_group'));
}

// Success
$this->returnArray(array('message' => $this->_language->get('api', 'group_updated')));
} catch (Exception $e) {
$this->throwError(23, $this->_language->get('api', 'unable_to_create_report'));
}
} else $this->throwError(1, $this->_language->get('api', 'invalid_api_key'));
}

private function removeGroupFromDiscord() {
// Param: id = discord user id
// Param: role_id = discord role id
// Param: discord user id
// Param: discord role id
// TODO: Decide what happens when a role is removed in discord. Does their group get set to the default post-validation group
}

// Create a report
Expand Down
1 change: 0 additions & 1 deletion modules/Core/pages/panel/users_edit.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@
} else if ($group_discord_id != null && $old_group_discord_id != null){
$full_url = $bot_url . '/roleChange?id=' . $user_query->discord_id . '&guild_id=' . $queries->getWhere('settings', array('name', '=', 'discord'))[0]->value . '&role=' . $group_discord_id. '&oldRole=' . $old_group_discord_id;
} else $full_url = null;

file_get_contents($full_url);
}
}
Expand Down
5 changes: 4 additions & 1 deletion modules/Core/pages/user/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,8 @@
'discord_id' => array(
'min' => 18,
'max' => 18,
'numeric' => true
'numeric' => true,
'unique' => 'users'
)
));
if ($validation->passed()) {
Expand Down Expand Up @@ -534,6 +535,8 @@
}

} else {
// TODO: Finish these
// TODO: Default template form
foreach ($validation->errors() as $validation_error) {
if (strpos($validation_error, 'minimum') !== false || strpos($validation_error, 'maximum') !== false) {
$errors[] = $language->get('admin', 'discord_id_length');
Expand Down