Repository: Cog-Creators/Red-DiscordBot

Issue:
[V3] redbot-setup misbehaves with service accounts on linux
Please be sure to read through other issues as well to make sure what you are suggesting/reporting has not already
been suggested/reported

### Type:

- [ ] Suggestion
- [X] Bug

### Brief description of the problem
It creates (or tries to anyway) a home directory for a user which isn't intended to have one (service account)
### Expected behavior
write to `/usr/local/share`
### Actual behavior
```
Traceback (most recent call last):
  File "/usr/lib/python3.6/pathlib.py", line 1226, in mkdir
    self._accessor.mkdir(self, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: '/home/redbot/.config/Red-DiscordBot'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.6/pathlib.py", line 1226, in mkdir
    self._accessor.mkdir(self, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
FileNotFoundError: [Errno 2] No such file or directory: '/home/redbot/.config'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/share/redbot-env/bin/redbot-setup", line 11, in <module>
    load_entry_point('Red-DiscordBot==3.0.0b5', 'console_scripts', 'redbot-setup')()
  File "/usr/local/share/redbot-env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 565, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/local/share/redbot-env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2631, in load_entry_point
    return ep.load()
  File "/usr/local/share/redbot-env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2291, in load
    return self.resolve()
  File "/usr/local/share/redbot-env/lib/python3.6/site-packages/pkg_resources/__init__.py", line 2297, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/local/share/redbot-env/lib/python3.6/site-packages/redbot/setup.py", line 15, in <module>
    config_dir.mkdir(parents=True, exist_ok=True)
  File "/usr/lib/python3.6/pathlib.py", line 1230, in mkdir
    self.parent.mkdir(parents=True, exist_ok=True)
  File "/usr/lib/python3.6/pathlib.py", line 1230, in mkdir
    self.parent.mkdir(parents=True, exist_ok=True)
  File "/usr/lib/python3.6/pathlib.py", line 1226, in mkdir
    self._accessor.mkdir(self, mode)
  File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
    return strfunc(str(pathobj), *args)
PermissionError: [Errno 13] Permission denied: '/home/redbot'
```
(Can't create a home directory for a service account)
### Steps to reproduce
```
sudo useradd -r -s /bin/nologin redbot
sudo python3.6 -m venv /usr/local/share/redbot-env
sudo  /usr/local/share/redbot-env/bin/pip3.6 install -U --process-dependency-links red-discordbot[voice]
sudo -H -u redbot  /usr/local/share/redbot-env/bin/redbot-setup
```
  


Discussion:

Resolving patch:
diff --git a/redbot/core/data_manager.py b/redbot/core/data_manager.py
--- a/redbot/core/data_manager.py
+++ b/redbot/core/data_manager.py
@@ -1,4 +1,5 @@
 import sys
+import os
 from pathlib import Path
 from typing import List
 import hashlib
@@ -30,7 +31,13 @@
     "CORE_PATH_APPEND": "core"
 }
 
-config_dir = Path(appdirs.AppDirs("Red-DiscordBot").user_config_dir)
+config_dir = None
+appdir = appdirs.AppDirs("Red-DiscordBot")
+if sys.platform == 'linux':
+    if os.getuid() < 1000:
+        config_dir = Path(appdir.site_data_dir)
+if not config_dir:
+    config_dir = Path(appdir.user_config_dir)
 config_file = config_dir / 'config.json'
 
 
diff --git a/redbot/setup.py b/redbot/setup.py
--- a/redbot/setup.py
+++ b/redbot/setup.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 
 import sys
+import os
 from copy import deepcopy
 from pathlib import Path
 
@@ -10,9 +11,20 @@
 from redbot.core.data_manager import basic_config_default
 from redbot.core.cli import confirm
 
+config_dir = None
 appdir = appdirs.AppDirs("Red-DiscordBot")
-config_dir = Path(appdir.user_config_dir)
-config_dir.mkdir(parents=True, exist_ok=True)
+if sys.platform == 'linux':
+    if os.getuid() < 1000:
+        config_dir = Path(appdir.site_data_dir)
+if not config_dir:
+    config_dir = Path(appdir.user_config_dir)
+try:
+    config_dir.mkdir(parents=True, exist_ok=True)
+except PermissionError:
+    print(
+        "You don't have permission to write to "
+        "'{}'\nExiting...".format(config_dir))
+    sys.exit(1)
 config_file = config_dir / 'config.json'
 
 


Test patch: