question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Newly joined guilds are not available to commands in their context

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:25 (25 by maintainers)

github_iconTop GitHub Comments

1reaction
Lulalabycommented, Jul 5, 2021

Workaround:

        // client.GuildCreated += Client_GuildCreated;

        private static Task Client_GuildCreated(DiscordClient sender, GuildCreateEventArgs e)
        {
            Console.WriteLine("Joined Guild: " + e.Guild.Name);
            Console.WriteLine("Reloading");
            sender.ReconnectAsync(true);
            return Task.CompletedTask;
        }
0reactions
Lulalabycommented, Jul 6, 2021

Can validate. This revert/fix works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't create guild command · Issue #136
Summary I'm trying to create a guild command but it doesn't work. It worked before when I had all commands only being guild...
Read more >
Python Discord 'Context' object has no attribute 'guild'
As M. I. Wright had wrote in their answer, you're using the async branch of discord.py. In the async branch, ctx.guild does not...
Read more >
Registering slash commands
Slash commands can be registered in two ways; in one specific guild, or for every guild the bot is in. We're going to...
Read more >
API Reference - discord.py - Read the Docs
A decorator that converts the provided method into a Command, adds it to the bot ... If the invite is for a guild...
Read more >
Interactions API Reference - discord.py
If the interaction is not an application command related interaction or the command is not found in the client's attached tree then None...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found