Repository: Cog-Creators/Red-DiscordBot

Issue:
[Trivia] Obnoxious filenames break custom upload
### What Red version are you using?

3.5.0.dev323+g23d539c4

### Cog name

Trivia

### Command name

triviaset custom upload

### What did you expect to happen?

It to work.

### What actually happened?

The command errored:
```py
Traceback (most recent call last):
  File "venv\lib\site-packages\discord\ext\commands\core.py", line 229, in wrapped
    ret = await coro(*args, **kwargs)
  File "venv\lib\site-packages\redbot\cogs\trivia\trivia.py", line 279, in trivia_upload
    await self._save_trivia_list(ctx=ctx, attachment=parsedfile)
  File "venv\lib\site-packages\redbot\cogs\trivia\trivia.py", line 693, in _save_trivia_list
    with file.open("wb") as fp:
  File "Python\Python38\lib\pathlib.py", line 1218, in open
    return io.open(self, mode, buffering, encoding, errors, newline,
  File "Python\Python38\lib\pathlib.py", line 1074, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: 'datapath\\cogs\\Trivia\\aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.yaml'
```

### How can we reproduce this error?

1. Create a valid trivia file with the name `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.yaml`
2. Attempt to upload this file with `[p]triviaset custom upload`


### Anything else?

This bug occurred on Windows, which has a limit to the length of the entire filepath, rather than a limit to the length of the filename. My datapath happens to be nested deeper than the place I made the file from, allowing me to give it a longer name than it could have in my datapath.

To fix this bug, the error should probably be caught with a message about the path being invalid. Limiting the filename technically will not fix this issue alone, as even a filename that is 1 character long *could* cause this error in extreme cases.


Discussion:
Technically we could support paths longer than 256 characters, but not sure if Python itself allows this, but if I do not misremember Python asks if you wanna increase max path size, or maybe it was a different program.

https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation?tabs=registry#enable-long-paths-in-windows-10-version-1607-and-later

So it's a case if we wanna enable this if it's supported and keep it in our guide, or maybe just reference it incase the error is hit and the user wants to resolve it in the traceback rather. Then again enabling this may have drawbacks if other apps do not support longer path and there is no way to delete the file outside of Red. 
Python is asking about max path length.
https://docs.python.org/3/using/windows.html

Resolving patch:
diff --git a/redbot/cogs/trivia/trivia.py b/redbot/cogs/trivia/trivia.py
--- a/redbot/cogs/trivia/trivia.py
+++ b/redbot/cogs/trivia/trivia.py
@@ -690,8 +690,18 @@ async def _save_trivia_list(
         TRIVIA_LIST_SCHEMA.validate(trivia_dict)
 
         buffer.seek(0)
-        with file.open("wb") as fp:
-            fp.write(buffer.read())
+        try:
+            with file.open("wb") as fp:
+                fp.write(buffer.read())
+        except FileNotFoundError as e:
+            await ctx.send(
+                _(
+                    "There was an error saving the file.\n"
+                    "Please check the filename and try again, as it could be longer than your system supports."
+                )
+            )
+            return
+
         await ctx.send(_("Saved Trivia list as {filename}.").format(filename=filename))
 
     def _get_trivia_session(


Test patch: