Skip to content

🐛 Null pointer risk in nw_server_status forcemonitor command #120

@anthropic-code-agent

Description

@anthropic-code-agent

Description

In nw_server_status/server_status.py, the forcemonitor command does not check if the monitor channel exists before attempting to access its permissions, leading to a potential null pointer exception.

Location

File: nw_server_status/server_status.py
Lines: 209-210

Current Code

async def forcemonitor(self, ctx):
    voice_channel = await self.get_guild_monitor_channel(ctx.guild)
    bot_perms = voice_channel.permissions_for(ctx.me)  # ❌ Null pointer if voice_channel is None

Root Cause

The method get_guild_monitor_channel() can return None (see line 112):

async def get_guild_monitor_channel(self, guild):
    channel_id = await self.config.guild(guild).monitor_channel()
    if not channel_id:
        return None  # ← Returns None if not configured
    
    channel = guild.get_channel(channel_id)
    if not channel:
        return None  # ← Returns None if channel deleted
    
    return channel

However, the caller at line 209 does not check for None before accessing voice_channel.permissions_for().

Impact

  • Bot crash when /forcemonitor is used before a monitor channel is configured
  • AttributeError: 'NoneType' object has no attribute 'permissions_for'
  • Command fails ungracefully without user-friendly error message

Fix Required

Add a null check before accessing the channel:

async def forcemonitor(self, ctx):
    voice_channel = await self.get_guild_monitor_channel(ctx.guild)
    
    if not voice_channel:
        await ctx.send("❌ No monitor channel is configured. Use `[p]monitor <channel>` first.")
        return
    
    bot_perms = voice_channel.permissions_for(ctx.me)
    # ... rest of the code

Priority

HIGH - Causes bot crash and poor user experience.

Testing

After fixing:

  1. Run [p]forcemonitor without setting up a monitor channel first
  2. Verify you get a helpful error message instead of a crash
  3. Set up a monitor channel, then run [p]forcemonitor again
  4. Verify it works correctly

Related

This is part of a comprehensive code review. See other issues for additional improvements.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions