discord-net/Discord.Net

[Bug]: SocketGuild.GetUsersAsync() problem

FemLolStudio opened this issue · 2 comments

Check The Docs

  • I double checked the docs and couldn't find any useful information.

Verify Issue Source

  • I verified the issue was caused by Discord.Net.

Check your intents

  • I double checked that I have the required intents.

Description

I wanted to get all users from a bigger server, but when I trying to iterate the results I get this error.

Version

3.15.3

Working Version

No response

Logs

System.InvalidCastException: Unable to cast object of type 'Discord.Page`1[Discord.Rest.RestGuildUser]' to type 'Discord.IGuildUser'.

Sample

SocketGuild? guild = DiscordShardedClient.Guilds.FirstOrDefault(x => x == server_id);

if (guild is null)
    return;

IAsyncEnumerable<IReadOnlyCollection<IGuildUser>> users = guild.GetUsersAsync();

await foreach (IGuildUser user in users) // <--- error here
{
    //....
}

Packages

	<ItemGroup>
		<PackageReference Include="AWSSDK.S3" Version="3.7.402.2" />
		<PackageReference Include="Betalgo.OpenAI" Version="8.6.1" />
		<PackageReference Include="Discord.Net" Version="3.15.3" />
		<PackageReference Include="Google.Apis.YouTube.v3" Version="1.68.0.3513" />
		<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
		<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="8.0.2" />
		<PackageReference Include="SixLabors.ImageSharp" Version="3.1.5" />
	</ItemGroup>

Environment

  • Docker image: mcr.microsoft.com/dotnet/runtime:8.0 (Unix 5.4.0.192)
  • Architecture: x64
  • SDK: .NET 8.0 (mcr.microsoft.com/dotnet/sdk:8.0)

AND

  • OS: Windows 10 22H2 19045.4780
  • Architecture: x64
  • SDK: .NET 8.0.8

(On both platforms)

You are trying to cast IReadOnlyCollection<IGuildUser> to IGuildUser.

Try this:

await foreach (IGuildUser user in users.Flatten())