#How do I get a DiscordMember/DiscordUser correct DisplayName?

1 messages · Page 1 of 1 (latest)

boreal hawk
#

I am using DSharpPlus but I can't access a DiscordMember/DiscordUser's display name.
How should I do it? It seems that the DiscordUser Username returns the unique identifier name, DiscordMember DisplayName does the same and DiscordMember Nickname always returns null.
For example, if I tried to get all these on my User profile I would get my DiscordUser's Username as paganoo, my DiscordMember DisplayName as paganoo and my DiscordMember Nickname as null.
How do I get a return as Pagano?

stray harbor
#

Are you using v4? its only supported in v5

boreal hawk
#

Is v5 already available through NuGet?

stray harbor
#

its a prerelease

boreal hawk
#

My Nuget Package Manager is only showing me up to 4.4.9

#

How do I show prereleases?

stray harbor
#

There should be a checkbox

#

VS?

boreal hawk
#

Yes

stray harbor
#

there is a checkbox right next to the search bar

boreal hawk
#

Should I use the last one then?

#

Although not stable atm

#

How do I do that in the prerelease version anyway?

sage basin
boreal hawk
#

I tried the same code snippet but using the prerelease version and DisplayName is returning me the wrong name

#

Not the actual DisplayName

#

Is there any difference I should be aware of in my code?

sage basin
#

what is it returning

boreal hawk
#

paganoo in my case

sage basin
#

what does GlobalName return

boreal hawk
#

What class should GlobalName be a field of?

#

DiscordMember doesn't have a GlobalName field

sage basin
#

DiscordUser

boreal hawk
#

Let me try

sage basin
boreal hawk
#

Let me check

#

I wasn't but now my whole code is giving errors

stray harbor
#

Thats expected

sage basin
#

##1585

mystic epochBOT
sage basin
#

both of those are going to be useful

boreal hawk
#

I think I managed to change the most part but how can I handle trigger events such as the old DiscordClient.Ready and GuildMemberAdded?

boreal hawk
#

Another question, when I do var dMembers = dClient.GetGuildAsync(GuildId).Result.GetAllMembersAsync();
How do I get a this IAsyncEnumerable into an Enumerable?

#

So I can do a foreach loop on it

sage basin
#

do an await foreach loop instead

boreal hawk
#

Ooh ok

#

The GlobalName worked btw, tysm

#

I still gotta figure out how everything works

#

Does v5 have a release date already?

sage basin
#

DisplayName pulls from Nickname -> GlobalName -> Username depending on which is available

sage basin
boreal hawk
sage basin
#

generally yeah

boreal hawk
#

Ok, thank you

#

Oh wait. Is DiscordConfiguration() still useful?

sage basin
#

some options are yeah

#

not sure how we'll migrate them

boreal hawk
#

My code was like this:

public async Task RunAsync()
{
    var configFile = new ConfigFileReader();
    await configFile.ReadConfigFile();

    var dConfig = new DiscordConfiguration()
    {
        Intents = DiscordIntents.All,
        Token = configFile.BotToken,
        TokenType = TokenType.Bot,
        AutoReconnect = true
    };

    var BotClient = new DiscordClient(dConfig);
    BotClient. += Client_Ready;

    BotClient.GuildMemberAdded += async (client, args) =>
    {
        using (var db = new EvoHubContext())
        {
            db.Users.Add(new User()
            {
                Name = args.Member.Username,
                DiscordId = (long)args.Member.Id
            });
            await db.SaveChangesAsync();
        }
    };

    await BotClient.ConnectAsync();
    await Task.Delay(-1);
}

private static Task Client_Ready(DiscordClient sender, ReadyEventArgs args)
{
    return Task.CompletedTask;
}
sage basin
#

you won't need any of those in DiscordConfiguration

boreal hawk
#

Now it became this:

public async Task RunAsync()
{
    var configFile = new ConfigFileReader();
    await configFile.ReadConfigFile();

    var builder = DiscordClientBuilder.CreateDefault(configFile.BotToken, DiscordIntents.All);

    var BotClient = builder.Build();

    var test = new ConverterTest();
    test.ConvertDMembersToUsers(BotClient);

    await BotClient.ConnectAsync();
    await Task.Delay(-1);
}
sage basin
#

token and intents are now mandatory in registering, tokentype is always bot, autoreconnect defaults to true

boreal hawk
boreal hawk
#

The Event handlers are now on the builder.ConfigureEventHandlers where I have to handle all of them together? Did I get that right?

sage basin
#

wdym by handling them all together?

boreal hawk
#

So my ClientReady and GuildMemberAdded are now in builder.ConfigureEventHandlers

sage basin
#

yes

boreal hawk
#

This is the example on the website:

builder.ConfigureEventHandlers
(
    b => b.HandleMessageCreated(async (s, e) => 
    {
        if (e.Message.Content.ToLower().StartsWith("spiderman"))
        {
            await e.Message.RespondAsync("I want pictures of Spiderman!");
        }
    })
    .HandleGuildMemberAdded(OnGuildMemberAdded)
);

private Task OnGuildMemberAdded(DiscordClient sender, GuildMemberAddedEventArgs args)
{
    // non-asynchronous code here
    return Task.CompletedTask;
}
sage basin
#

yes

boreal hawk
#

The rest should all be the same then? Or did something else important change?

sage basin
#

DSharpPlus.Lavalink is defunct, if you were using that

boreal hawk
#

That was for voice right? I am not

sage basin
#

SlashCommands is deprecated in favor of Commands, but it's not terribly urgent

boreal hawk
#

But are you changing the way it works or are we not having SlashCommands and MenuContext commands anymore?

sage basin
#

it looks pretty similar

still cometBOT
sage basin
#

heres the differences

boreal hawk
#

Hmm, ok

#

I will give it all a try and see how it goes

#

Wait, what would the Handler be for ClientReady now?

sage basin
#

SessionCreated

#

though,

still cometBOT
#

🏷 ready-event:

The READY event refers to Discord, not the client

tl;dr this event is fired when Discord is ready to start sending gateway events; cache (members/guilds) are populated as these events come in.

You probably want GuildDownloadCompleted instead

The Ready event on the client only signals that your client has completed a handshake with the gateway, and it's ready to start streaming payloads to you.

This isn't to say you should never use it, but for a vast majority of uses, there is no point in using this event, and it simply exists for completeness and correctness.

Gateway documentation: https://discord.dev/topics/gateway-events#ready

boreal hawk
#

So I should check if GuildDownloadCompleted was triggered before doing anything, right?

#

Not the Ready one

#

One more question, is there anything I could do to simplify my code as I will be using my Bot in only one server?

#

Because looping around all Guilds to just find a single one isn't very efficient right?

boreal hawk
#

Ok

boreal hawk
#

What's more efficient?
client.GetGuildAsync(id).Result;
or
client.Guilds[id];

stray harbor
#

second

boreal hawk
#

May I know the reason? Just for knowledge

#

And is the same true for guild.Members[id] vs guild.GetMemberAsync(id).Result; and every other search?

stray harbor
#

First of all you should use proper async/await

#

so no .Result

boreal hawk
#

True, sorry, I was typing through Discord only, not what I usually do

sage basin
#

guild.Members[id] will get one from cache

#

await guild.GetMemberAsync(id) will try to get one from cache (and is marginally slower if it does), but then make a rest request if it wasn't cached

#

so you're guaranteed a result, it just might take longer

stray harbor
boreal hawk
#

Sorry, is there a way to get the DiscordMember of Bot through the DiscordClient? I couldn't find any but just asking

sage basin
#

no, you need a guild to get a guild member object

boreal hawk
#

What about the DiscordUser of Bot?

mystic epochBOT
#

DSharpPlus.BaseDiscordClient.CurrentUser

Summary

Gets the current user.

Declaration

public DiscordUser CurrentUser { get; internal set; }
boreal hawk
#

That returns me the Bot DiscordUser then?

sage basin
#

yes

boreal hawk
#

Oh nvm

#

But the DiscordClient.CurrentUser does not contain a field called DisplayName

sage basin
#

because it's not a DiscordMember

boreal hawk
#

Gotcha

#

Ty

#

So I just use the Username in this case or cast to DiscordMember and use DisplayName? If so, which one is more recommended?

sage basin
#

it's fundamentally not a DiscordMember object

#

you can get one if you know the guild

boreal hawk
#

But isn't DiscordUser castable to DiscordMember?

sage basin
#

yes, but only if it's actually a DiscordMember object under the hood

#

member is always castable to user, but not vice versa

boreal hawk
#

Oh I see, okay, thank you

boreal hawk
#

Is there a way to do an embed with four fields with 2 columns and 2 rows?

boreal hawk
#

May I ask for a tip on how to do a certain thing?
If so, for my project's purpose, I need to register DiscordMembers to a Database but which way do you recommend?

  • A SlashCommand like /register @boreal hawk [params]
  • A ContextMenuCommand like click on me > Apps > Register User (without params right?)
  • Reacting to an emoji (the emoji being the param)
  • Clicking a button (the button being the param)