Discord.net |如何将属于我数据库中人员的用户ID转换为可用于我的排行榜的用户名?

问题描述 投票:-1回答:3

我正在制作我的Discord bot所具有的调平系统的排行榜。该列表将列出具有最多XP的人员,并从最高到最低金额订购。我已经实现了这个目标,但我只能在排行榜的XP数量旁边显示用户的ID。如何将此用户ID转换为用户名?

foreach (ulong n in DbContext.Experiences.OrderByDescending(x => 
x.XP).Select(x => x.ID))
{
    Context.Guild.GetUser(n).ToString()
}

var leaderboard = string.Concat(DbContext.Experiences.OrderByDescending(x => 
x.XP).Select(x => $"Level {x.LevelNumber} with {x.XP} xp 
    {//username must be here}\n"));

await ReplyAsync(leaderboard.ToString());
c# bots discord leaderboard discord.net
3个回答
0
投票

查看代码中的注释:

//What exactly is this for loop meant to do? 
//You appear to be getting a user based on ID and doing nothing with that value.
//This should be removed
foreach (ulong n in DbContext.Experiences.OrderByDescending(x => 
x.XP).Select(x => x.ID))
{
   //While this is how to get a user by ID, you aren't actually doing anything with this once it's retrieved.
    Context.Guild.GetUser(n).ToString()
}

//You can simply fetch the username here, given that you have access to the ID
var leaderboard = string.Concat(DbContext.Experiences.OrderByDescending(x => 
x.XP).Select(x => $"Level {x.LevelNumber} with {x.XP} xp 
    {//username must be here}\n"));

await ReplyAsync(leaderboard.ToString());

您修改后的代码应如下所示:

var leaderboard = string.Join("\n", DbContext.Experiences
                                             .OrderByDescending(x => x.XP)
                                             .Select(x => $"Level {x.LevelNumber} with {x.XP} xp {Context.Guild.GetUser(x.ID).ToString()}"));
await ReplyAsync(leaderboard);

注意:我用string.Concat替换了string.Join,因为它是一种更有效的String构建方法。


0
投票

假设您已拥有所需的用户ID

Context.Guild.GetUser(THE_USER_ID_HERE).Username 如果存在,则返回用户的用户名。

如果您的排行榜是全局的(用户可能不在执行命令的服务器中) 你可以使用client.GetUser(THE_USER_ID_HERE).Username,其中client是你机器人当前的套接字客户端。

(或者,如果您希望显示绑定到服务器的用户名,请访问Nickname属性)


0
投票

我期望看到的内容(从未看过Discord,如果这是数据库结构的来源)将是一个表:

  • 公会
  • 经验
  • 用户

我希望dbContext.Users有类似的东西:

UserId | UserName 

然后一个公会表有类似的东西:

GuildId | GuildName | UserId 

和经验看起来像:

ExperienceId | UserId 

我将继续在这里做一些假设:

Context.Guild.GetUser(n).ToString(); 

对我来说,这看起来像一个转换为SQL的EF Core查询:

select UserId from Guild

通过这种方式,我看到了一些潜在的问题:

首先,仅返回字符串或长的Guild方法很奇怪。如果那是您的实现,则返回一个对象。

更重要的是,您可以在1个查询中执行此操作:

在Sql中:

Select g.GuildId, e.Experiece, u.UserId, u.UserName from Guild g
   left join Users u on g.UserId = u.UserId
   left join Experiences e on u.UserId = e.UserId
where g.GuildId = @myGuildId
order by e.Experience Desc

这会给你回行如下:1 | 1500 | 10 | BitesSpoons 1 | 1450 | 51 | LostElbows 1 | 1121 | 98 | EarthSkyFire 1 | 990 | 15 | GoldenGoose

我会做什么:创建一个视图类,并使用查询中的AutoMapper或者实现实例时映射它:

上课:

public class LeaderView 
{
    public string UserName {get; set;}
    public long UserId {get; set;}
    public long GuildId {get; set; }
    public int Experience { get; set; }
}

然后一个linq到Sql查询如:

var leaders = from g in context.Guilds
join u in context.Users on g.UserId = u.UserId
join e in context.Experience on u.UserId  = e.UserId
select new LeaderView
{
    UserName = u.UserName,
    UserId = u.UserId,
    GuildId = g.UserId,
    Experience = e.Experience
};  

leaders = leaders.OrderByDescending(o => o.Experience);
return leaders.ToList();
© www.soinside.com 2019 - 2024. All rights reserved.