Skip to content

Newly joined guilds are not available to commands in their context

Created by: alexhorner

Summary

System.NullReferenceException occurs when accessing ctx.Guild properties when a command is executed, but only when the bot has joined the guild in which the command was run after startup.

Details

When a bot is started, and a command is run, ctx.Guild can be accessed as expected, but only when the command is called from a guild which the bot was already in prior to starting up.

If the same command is called from a guild which the bot was joined to after it had started up, ctx.Guild will be null.

Steps to reproduce

  1. Create a bot
  2. Join it to a guild
  3. Start it up
  4. Run the provided Guild command in the guild. It succeeds.
  5. Join it to another guild whilst it is running
  6. Run the provided Guild command in the new guild. It throws.
  7. Restart the bot
  8. Run the provided Guild command in the new guild again. It succeeds.

Notes

This issue affects DiscordClient, DiscordShardedClient with 1 shard and DiscordShardedClient with 3 shards.

Here is the provided Guild command:

[Command("guild")]
public async Task Guild(CommandContext ctx)
{
    await ctx.RespondAsync($"You are calling from guild {ctx.Guild.Name} ({ctx.Guild.Id})");
}

I am using DSharpPlus 4.1.0-nightly-00915