Repository: Pycord-Development/pycord

Issue:
discord.utils.get TypeError on Voice Channel Search
### Summary

Using utils.get to fetch a voice channel will fail with a TypeError if a command is executed that contains a VoiceChannel as an option.

### Reproduction Steps

1. Execute a slash command that has an option parameter of type discord.VoiceChannel.

2. Attempt to run any code that uses utils.get to fetch a VoiceChannel afterwards. It will fail, and any subsequent attempts to use utils.get to fetch a VoiceChannel will fail too.


### Minimal Reproducible Code

I reproduced this using the following two bot commands in a bot implementation. These commands were created in a cog:

```python
    @slash_command(name="bug-test", guild_ids=globals.COMMAND_GUILD_ID_REGISTER_LIST, default_permissions=True)
    async def bug_test(self, ctx: discord.ApplicationContext) -> None:
        async def test_task():
            while True:
                channel_name = "Intro 1"
                channel = utils.get(ctx.guild.voice_channels, name=channel_name)
                
                print(f"Channel is: {channel}")
                               
                await asyncio.sleep(1)
                
        self.bot.loop.create_task(test_task())
        
        await ctx.respond("Okay", ephemeral=True)
```

```python
    @slash_command(name="bug-test-two", guild_ids=globals.COMMAND_GUILD_ID_REGISTER_LIST, default_permissions=True)
    async def bug_test2(self, ctx: discord.ApplicationContext, random_voice_channel: discord.VoiceChannel) -> None:
        await ctx.respond(f"Channel is: {random_voice_channel}", ephemeral=True)
```

Use `/bug-test`, which will continually show a fetched channel, then at any point use `/bug-test-two`. The loop in the first command's task will then fail with a traceback, as will any subsequent attempts anywhere to use utils.get() to fetch a VoiceChannel.


### Expected Results

The utils.get() method continues to function after commands with VoiceChannel as an option are used.

### Actual Results


```
07/10/2022 04:24:11 PM - asyncio - ERROR -> Task exception was never retrieved
future: <Task finished name='Task-37' coro=<WaitlistQueue.bug_test.<locals>.test_task() done, defined at C:\Users\Tiffany\dev\bot\cogs\av_fleet_queue\fleet_queue_main.py:736> exception=TypeError("'<' not supported between instances of 'NoneType' and 'int'")>
Traceback (most recent call last):
  File "C:\Users\Tiffany\dev\bot\cogs\av_fleet_queue\fleet_queue_main.py", line 739, in test_task
    channel = utils.get(ctx.guild.voice_channels, name=channel_name)
  File "c:\Users\Tiffany\dev\bot\venv\lib\site-packages\discord\guild.py", line 620, in voice_channels
    r.sort(key=lambda c: (c.position, c.id))
TypeError: '<' not supported between instances of 'NoneType' and 'int'
```


### Intents

All

### System Information

- Python v3.9.2-final
- py-cord v2.0.0-final
- aiohttp v3.8.1
- system info: Windows 8.1 6.3.9600

### Checklist

- [X] I have searched the open issues for duplicates.
- [X] I have shown the entire traceback, if possible.
- [X] I have removed my token from display, if visible.

### Additional Context

This error did not occur in 2.0.0rc1. This causes the stable release of 2.0.0 to be unsuitable for live deployments for any bot that has any meaningful interaction with voice channels.

It would seem whatever fix was applied to keep cached channel data up to date specifically from slash options completely breaks the cache for VCs.


Discussion:
According to your traceback statement, the code you showed isn't even remotely related to the problem. Could you maybe elaborate on how the code you showed is related to the actual error?
> According to your traceback statement, the code you showed isn't even remotely related to the problem. Could you maybe elaborate on how the code you showed is related to the actual error?

Apologies for the hastily thrown together report when I originally filed it. I have updated the initial report with proper information that should allow you to replicate this issue easily.
Potentially caused by #1365

Resolving patch:
diff --git a/discord/channel.py b/discord/channel.py
--- a/discord/channel.py
+++ b/discord/channel.py
@@ -1025,18 +1025,22 @@ def _get_voice_state_pair(self) -> Tuple[int, int]:
         return self.guild.id, self.id
 
     def _update(self, guild: Guild, data: Union[VoiceChannelPayload, StageChannelPayload]) -> None:
+        # This data will always exist
         self.guild = guild
         self.name: str = data["name"]
-        rtc = data.get("rtc_region")
-        self.rtc_region: Optional[VoiceRegion] = try_enum(VoiceRegion, rtc) if rtc is not None else None
-        self.video_quality_mode: VideoQualityMode = try_enum(VideoQualityMode, data.get("video_quality_mode", 1))
         self.category_id: Optional[int] = utils._get_as_snowflake(data, "parent_id")
-        self.last_message_id: Optional[int] = utils._get_as_snowflake(data, "last_message_id")
-        self.position: int = data.get("position")
-        self.bitrate: int = data.get("bitrate")
-        self.user_limit: int = data.get("user_limit")
-        self.flags: ChannelFlags = ChannelFlags._from_value(data.get("flags", 0))
-        self._fill_overwrites(data)
+
+        # This data may be missing depending on how this object is being created/updated
+        if not data.pop("_invoke_flag", False):
+            rtc = data.get("rtc_region")
+            self.rtc_region: Optional[VoiceRegion] = try_enum(VoiceRegion, rtc) if rtc is not None else None
+            self.video_quality_mode: VideoQualityMode = try_enum(VideoQualityMode, data.get("video_quality_mode", 1))
+            self.last_message_id: Optional[int] = utils._get_as_snowflake(data, "last_message_id")
+            self.position: int = data.get("position")
+            self.bitrate: int = data.get("bitrate")
+            self.user_limit: int = data.get("user_limit")
+            self.flags: ChannelFlags = ChannelFlags._from_value(data.get("flags", 0))
+            self._fill_overwrites(data)
 
     @property
     def _sorting_bucket(self) -> int:
@@ -1921,13 +1925,17 @@ def __repr__(self) -> str:
         return f"<CategoryChannel id={self.id} name={self.name!r} position={self.position} nsfw={self.nsfw}>"
 
     def _update(self, guild: Guild, data: CategoryChannelPayload) -> None:
+        # This data will always exist
         self.guild: Guild = guild
         self.name: str = data["name"]
         self.category_id: Optional[int] = utils._get_as_snowflake(data, "parent_id")
-        self.nsfw: bool = data.get("nsfw", False)
-        self.position: int = data.get("position")
-        self.flags: ChannelFlags = ChannelFlags._from_value(data.get("flags", 0))
-        self._fill_overwrites(data)
+
+        # This data may be missing depending on how this object is being created/updated
+        if not data.pop("_invoke_flag", False):
+            self.nsfw: bool = data.get("nsfw", False)
+            self.position: int = data.get("position")
+            self.flags: ChannelFlags = ChannelFlags._from_value(data.get("flags", 0))
+            self._fill_overwrites(data)
 
     @property
     def _sorting_bucket(self) -> int:


Test patch: