Skip to content

Commit

Permalink
Add XP image URL to database and update async code
Browse files Browse the repository at this point in the history
Main changes involve adding an XP image URL to the GuildConfigs table in two different database migrations (PostgreSQL and SQLite). Minor changes standardize usage of 'async' and 'await' in various places throughout the codebase. Lastly, a language version update in the Mewdeko.Database.csproj file is also included.
  • Loading branch information
SylveonDeko committed Dec 9, 2023
1 parent a2caf4a commit f0ef232
Show file tree
Hide file tree
Showing 79 changed files with 3,965 additions and 3,727 deletions.
4 changes: 1 addition & 3 deletions Mewdeko.GlobalBanAPI/Common/ApiKeyAuthorize.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
namespace Mewdeko.GlobalBanAPI.Common;

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class ApiKeyAuthorizeAttribute : Attribute
{
}
public class ApiKeyAuthorizeAttribute : Attribute;
6 changes: 2 additions & 4 deletions src/Mewdeko.Coordinator/CoordStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
endpoints.MapGrpcService<CoordinatorService>();

endpoints.MapGet("/",
async context => await context.Response.WriteAsync(
"Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909")
.ConfigureAwait(false));
endpoints.MapGet("/", context => context.Response.WriteAsync(
"Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909"));
});
}
}
8 changes: 5 additions & 3 deletions src/Mewdeko.Database/DbService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public DbService(int shardCount, string token, bool usePostgres, string psqlConn
else
{
if (Environment.OSVersion.Platform == PlatformID.Unix)
builder.DataSource = builder.DataSource = folderpath + $"/.local/share/Mewdeko/{clientId}/data/Mewdeko.db";
builder.DataSource = builder.DataSource =
folderpath + $"/.local/share/Mewdeko/{clientId}/data/Mewdeko.db";
else
builder.DataSource = builder.DataSource = folderpath + $"/Mewdeko/{clientId}/data/Mewdeko.db";
}
Expand All @@ -64,10 +65,11 @@ public async void Setup()
await context.SaveChangesAsync().ConfigureAwait(false);

var env = Assembly.GetExecutingAssembly();
var pmhs = env.GetTypes().Where(t => t.GetInterfaces().Any(i => i == typeof(IPostMigrationHandler))).ToList();
var pmhs = env.GetTypes().Where(t => t.GetInterfaces().Any(i => i == typeof(IPostMigrationHandler)))
.ToList();
foreach (var id in toApply)
{
var pmhToRuns = pmhs?.Where(pmh => pmh.GetCustomAttribute<MigrationAttribute>()?.Id == id).ToList();
var pmhToRuns = pmhs.Where(pmh => pmh.GetCustomAttribute<MigrationAttribute>()?.Id == id).ToList();
foreach (var pmh in pmhToRuns)
{
pmh.GetMethod("PostMigrationHandler")?.Invoke(null, new object[]
Expand Down
8 changes: 4 additions & 4 deletions src/Mewdeko.Database/Extensions/AfkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace Mewdeko.Database.Extensions;

public static class AfkExtensions
{
public static async Task<Afk[]> ForGuild(this DbSet<Afk> set, ulong guildId) =>
await set
public static Task<Afk[]> ForGuild(this DbSet<Afk> set, ulong guildId) =>
set
.AsQueryable()
.AsNoTracking().Where(x => x.GuildId == guildId).ToArrayAsyncEF();

public static async Task<Afk[]> GetAll(this DbSet<Afk> set) =>
await set.AsQueryable().AsNoTracking().ToArrayAsyncEF();
public static Task<Afk[]> GetAll(this DbSet<Afk> set) =>
set.AsQueryable().AsNoTracking().ToArrayAsyncEF();
}
5 changes: 3 additions & 2 deletions src/Mewdeko.Database/Extensions/ChatTriggersExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ await crs
.Where(x => x.GuildId == id)
.ToArrayAsyncEF();

public static async Task<ChatTriggers> GetByGuildIdAndInput(this DbSet<ChatTriggers> crs, ulong? guildId, string input) =>
await AsyncExtensions.FirstOrDefaultAsync(crs, x => x.GuildId == guildId && x.Trigger.ToUpper() == input).ConfigureAwait(false);
public static Task<ChatTriggers> GetByGuildIdAndInput(this DbSet<ChatTriggers> crs, ulong? guildId, string input) =>
AsyncExtensions.FirstOrDefaultAsync(crs,
x => x.GuildId == guildId && x.Trigger.Equals(input, StringComparison.CurrentCultureIgnoreCase));
}
20 changes: 10 additions & 10 deletions src/Mewdeko.Database/Extensions/ClubExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Mewdeko.Database.Extensions;

public static class ClubExtensions
{
private static IQueryable<ClubInfo> Include(this DbSet<ClubInfo> clubs)
private static IQueryable<ClubInfo> Include(this IQueryable<ClubInfo> clubs)
=> clubs.Include(x => x.Owner)
.Include(x => x.Applicants)
.ThenInclude(x => x.User)
Expand All @@ -15,15 +15,15 @@ private static IQueryable<ClubInfo> Include(this DbSet<ClubInfo> clubs)
.Include(x => x.Users)
.AsQueryable();

public static async Task<ClubInfo> GetByOwner(this DbSet<ClubInfo> clubs, ulong userId)
=> await Include(clubs).FirstOrDefaultAsync(c => c.Owner.UserId == userId).ConfigureAwait(false);
public static Task<ClubInfo> GetByOwner(this DbSet<ClubInfo> clubs, ulong userId)
=> Include(clubs).FirstOrDefaultAsync(c => c.Owner.UserId == userId);

public static async Task<ClubInfo> GetByOwnerOrAdmin(this DbSet<ClubInfo> clubs, ulong userId)
=> await Include(clubs).FirstOrDefaultAsync(c => c.Owner.UserId == userId
|| c.Users.Any(u => u.UserId == userId && u.IsClubAdmin == 1)).ConfigureAwait(false);
public static Task<ClubInfo> GetByOwnerOrAdmin(this DbSet<ClubInfo> clubs, ulong userId)
=> Include(clubs).FirstOrDefaultAsync(c => c.Owner.UserId == userId
|| c.Users.Any(u => u.UserId == userId && u.IsClubAdmin == 1));

public static async Task<ClubInfo> GetByMember(this DbSet<ClubInfo> clubs, ulong userId)
=> await Include(clubs).FirstOrDefaultAsync(c => c.Users.Any(u => u.UserId == userId)).ConfigureAwait(false);
public static Task<ClubInfo> GetByMember(this DbSet<ClubInfo> clubs, ulong userId)
=> Include(clubs).FirstOrDefaultAsync(c => c.Users.Any(u => u.UserId == userId));

public static ClubInfo GetByName(this DbSet<ClubInfo> clubs, string name, int discrim)
=> Include(clubs).FirstOrDefault(c => c.Name.ToUpper() == name.ToUpper() && c.Discrim == discrim);
Expand All @@ -35,8 +35,8 @@ public static async Task<int> GetNextDiscrim(this DbSet<ClubInfo> clubs, string
.DefaultIfEmpty()
.MaxAsync().ConfigureAwait(false) + 1;

public static async Task<List<ClubInfo>> GetClubLeaderboardPage(this DbSet<ClubInfo> clubs, int page) =>
await clubs
public static Task<List<ClubInfo>> GetClubLeaderboardPage(this DbSet<ClubInfo> clubs, int page) =>
clubs
.AsNoTracking()
.OrderByDescending(x => x.Xp)
.Skip(page * 9)
Expand Down
7 changes: 4 additions & 3 deletions src/Mewdeko.Database/Extensions/DbExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mewdeko.Database.Extensions;

public static class DbExtensions
{
public static async Task<T> GetById<T>(this DbSet<T> set, int id) where T : DbEntity
=> await set.FirstOrDefaultAsync(x => x.Id == id);
public static Task<T> GetById<T>(this DbSet<T> set, int id) where T : DbEntity
=> set.FirstOrDefaultAsync(x => x.Id == id);

public static IEnumerable<GuildConfig> GetActiveConfigs(this DbSet<GuildConfig> set, IEnumerable<ulong> guildIds)
{
Expand All @@ -23,7 +23,8 @@ public static IEnumerable<GuildConfig> GetActiveConfigs(this DbSet<GuildConfig>
return $"@p{index}";
});

var sqlQuery = $"SELECT * FROM \"GuildConfigs\" WHERE \"GuildId\" = ANY (ARRAY[{string.Join(",", ids)}]::bigint[])";
var sqlQuery =
$"SELECT * FROM \"GuildConfigs\" WHERE \"GuildId\" = ANY (ARRAY[{string.Join(",", ids)}]::bigint[])";

return set.FromSqlRaw(sqlQuery, parameters.ToArray());
}
Expand Down
11 changes: 7 additions & 4 deletions src/Mewdeko.Database/Extensions/DiscordUserExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ public static async Task<DiscordUser> GetOrCreateUser(
string avatarId)
{
await ctx.EnsureUserCreated(userId, username, discrim, avatarId);
return await ctx.DiscordUser.Include(x => x.Club).FirstOrDefaultAsyncEF(u => u.UserId == userId).ConfigureAwait(false);
return await ctx.DiscordUser.Include(x => x.Club).FirstOrDefaultAsyncEF(u => u.UserId == userId)
.ConfigureAwait(false);
}

public static async Task<DiscordUser> GetOrCreateUser(this MewdekoContext ctx, IUser original) =>
await ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId).ConfigureAwait(false);
public static Task<DiscordUser> GetOrCreateUser(this MewdekoContext ctx, IUser original) =>
ctx.GetOrCreateUser(original.Id, original.Username, original.Discriminator, original.AvatarId);

public static async Task<int> GetUserGlobalRank(this DbSet<DiscordUser> users, ulong id) =>
await users.AsQueryable().CountAsyncEF(x => x.TotalXp > users.AsQueryable().Where(y => y.UserId == id).Select(y => y.TotalXp).FirstOrDefault()).ConfigureAwait(false)
await users.AsQueryable().CountAsyncEF(x =>
x.TotalXp > users.AsQueryable().Where(y => y.UserId == id).Select(y => y.TotalXp).FirstOrDefault())
.ConfigureAwait(false)
+ 1;

public static DiscordUser[] GetUsersXpLeaderboardFor(this DbSet<DiscordUser> users, int page) =>
Expand Down
9 changes: 5 additions & 4 deletions src/Mewdeko.Database/Extensions/MiniWarningsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@ public static async Task<bool> Forgive(this DbSet<Warning2> set, ulong guildId,
return true;
}

public static async Task ForgiveAll(this DbSet<Warning2> set, ulong guildId, ulong userId, string mod) =>
await set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId)
public static Task ForgiveAll(this DbSet<Warning2> set, ulong guildId, ulong userId, string mod) =>
set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId)
.ForEachAsync(x =>
{
if (x.Forgiven == 1) return;
x.Forgiven = 1;
x.ForgivenBy = mod;
}).ConfigureAwait(false);
});

public static Warning2[] GetForGuild(this DbSet<Warning2> set, ulong id) => set.AsQueryable().Where(x => x.GuildId == id).ToArray();
public static Warning2[] GetForGuild(this DbSet<Warning2> set, ulong id) =>
set.AsQueryable().Where(x => x.GuildId == id).ToArray();
}
4 changes: 2 additions & 2 deletions src/Mewdeko.Database/Extensions/RoleGreetExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ namespace Mewdeko.Database.Extensions;

public static class RoleGreetExtensions
{
public static async Task<RoleGreet[]> ForRoleId(this DbSet<RoleGreet> set, ulong roleId)
=> await set.AsQueryable().Where(x => x.RoleId == roleId).ToArrayAsyncEF();
public static Task<RoleGreet[]> ForRoleId(this DbSet<RoleGreet> set, ulong roleId)
=> set.AsQueryable().Where(x => x.RoleId == roleId).ToArrayAsyncEF();
}
4 changes: 2 additions & 2 deletions src/Mewdeko.Database/Extensions/StarboardExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Mewdeko.Database.Extensions;

public static class StarboardExtensions
{
public static async Task<StarboardPosts> ForMsgId(this DbSet<StarboardPosts> set, ulong msgid)
=> await set.AsQueryable().FirstOrDefaultAsyncEF(x => x.MessageId == msgid);
public static Task<StarboardPosts> ForMsgId(this DbSet<StarboardPosts> set, ulong msgid)
=> set.AsQueryable().FirstOrDefaultAsyncEF(x => x.MessageId == msgid);

public static async Task<IEnumerable<StarboardPosts>> All(this DbSet<StarboardPosts> set)
=> await set.AsQueryable().ToArrayAsyncEF();
Expand Down
8 changes: 4 additions & 4 deletions src/Mewdeko.Database/Extensions/SuggestionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ namespace Mewdeko.Database.Extensions;

public static class SuggestionsExtensions
{
public static async Task<SuggestionsModel[]> ForId(this DbSet<SuggestionsModel> set, ulong guildId, ulong sugid)
=> await set.AsQueryable().Where(x => x.GuildId == guildId && x.SuggestionId == sugid).ToArrayAsyncEF();
public static Task<SuggestionsModel[]> ForId(this DbSet<SuggestionsModel> set, ulong guildId, ulong sugid)
=> set.AsQueryable().Where(x => x.GuildId == guildId && x.SuggestionId == sugid).ToArrayAsyncEF();

public static async Task<SuggestionsModel[]> ForUser(this DbSet<SuggestionsModel> set, ulong guildId, ulong userId)
=> await set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId).ToArrayAsyncEF();
public static Task<SuggestionsModel[]> ForUser(this DbSet<SuggestionsModel> set, ulong guildId, ulong userId)
=> set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId).ToArrayAsyncEF();
}
11 changes: 6 additions & 5 deletions src/Mewdeko.Database/Extensions/WaifuExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ public class WaifuInfoStats

public static class WaifuExtensions
{
public static async Task<WaifuInfo> ByWaifuUserId(this DbSet<WaifuInfo> waifus, ulong userId, Func<DbSet<WaifuInfo>, IQueryable<WaifuInfo>> includes = null)
public static Task<WaifuInfo> ByWaifuUserId(this DbSet<WaifuInfo> waifus, ulong userId,
Func<DbSet<WaifuInfo>, IQueryable<WaifuInfo>> includes = null)
{
if (includes is null)
{
return await waifus.Include(wi => wi.Waifu)
return waifus.Include(wi => wi.Waifu)
.Include(wi => wi.Affinity)
.Include(wi => wi.Claimer)
.Include(wi => wi.Items)
.FirstOrDefaultAsyncEF(wi => wi.Waifu.UserId == userId);
}

return await includes(waifus)
return includes(waifus)
.AsQueryable()
.FirstOrDefaultAsyncEF(wi => wi.Waifu.UserId == userId);
}
Expand Down Expand Up @@ -67,8 +68,8 @@ await waifus
.Where(x => x.ClaimerId != null)
.SumAsyncLinqToDB(x => x.Price);

public static async Task<ulong> GetWaifuUserId(this DbSet<WaifuInfo> waifus, ulong ownerId, string name) =>
await waifus
public static Task<ulong> GetWaifuUserId(this DbSet<WaifuInfo> waifus, ulong ownerId, string name) =>
waifus
.AsQueryable()
.AsNoTracking()
.Where(x => x.Claimer.UserId == ownerId
Expand Down
6 changes: 3 additions & 3 deletions src/Mewdeko.Database/Extensions/WarningExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public static async Task<bool> Forgive(this DbSet<Warning> set, ulong guildId, u
return true;
}

public static async Task ForgiveAll(this DbSet<Warning> set, ulong guildId, ulong userId, string mod) =>
await set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId)
public static Task ForgiveAll(this DbSet<Warning> set, ulong guildId, ulong userId, string mod) =>
set.AsQueryable().Where(x => x.GuildId == guildId && x.UserId == userId)
.ForEachAsync(x =>
{
if (x.Forgiven == 1) return;
x.Forgiven = 1;
x.ForgivenBy = mod;
}).ConfigureAwait(false);
});

public static async Task<IEnumerable<Warning>> GetForGuild(this DbSet<Warning> set, ulong id)
=> await set.AsQueryable().Where(x => x.GuildId == id).ToArrayAsyncEF();
Expand Down
8 changes: 4 additions & 4 deletions src/Mewdeko.Database/Extensions/XpExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public static async Task<UserXpStats> GetOrCreateUser(this DbSet<UserXpStats> se
return usr;
}

public static async Task<List<UserXpStats>> GetUsersFor(this DbSet<UserXpStats> set, ulong guildId, int page) =>
await set.AsQueryable().AsNoTracking().Where(x => x.GuildId == guildId).OrderByDescending(x => x.Xp + x.AwardedXp)
public static Task<List<UserXpStats>> GetUsersFor(this DbSet<UserXpStats> set, ulong guildId, int page) =>
set.AsQueryable().AsNoTracking().Where(x => x.GuildId == guildId).OrderByDescending(x => x.Xp + x.AwardedXp)
.Skip(page * 9).Take(9).ToListAsyncEF();

public static async Task<List<UserXpStats>> GetTopUserXps(this DbSet<UserXpStats> set, ulong guildId) =>
await set.AsQueryable().AsNoTracking().Where(x => x.GuildId == guildId).OrderByDescending(x => x.Xp + x.AwardedXp)
public static Task<List<UserXpStats>> GetTopUserXps(this DbSet<UserXpStats> set, ulong guildId) =>
set.AsQueryable().AsNoTracking().Where(x => x.GuildId == guildId).OrderByDescending(x => x.Xp + x.AwardedXp)
.ToListAsyncEF();

public static int GetUserGuildRanking(this DbSet<UserXpStats> set, ulong userId, ulong guildId) =>
Expand Down
2 changes: 1 addition & 1 deletion src/Mewdeko.Database/Mewdeko.Database.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<EnablePreviewFeatures>True</EnablePreviewFeatures>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
<RootNamespace>Mewdeko.Database</RootNamespace>
<TargetFramework>net8.0</TargetFramework>
</PropertyGroup>
Expand Down
18 changes: 18 additions & 0 deletions src/Mewdeko.Database/Migrations/PostgreSql/AddXpBanner.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/Mewdeko.Database/Migrations/PostgreSql/AddXpBanner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace Mewdeko.Database.Migrations.PostgreSql;

public partial class AddXpImage : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "XpImgUrl",
table: "GuildConfigs",
type: "text",
nullable: true);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "XpImgUrl",
table: "GuildConfigs");
}
}
18 changes: 18 additions & 0 deletions src/Mewdeko.Database/Migrations/SQLite/AddXpBanner.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/Mewdeko.Database/Migrations/SQLite/AddXpBanner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore.Migrations;

namespace Mewdeko.Database.Migrations.SQLite;

public partial class AddXpBanner : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>("XpImgUrl", "GuildConfigs", "text", nullable: true);
}
}
2 changes: 2 additions & 0 deletions src/Mewdeko.Database/Models/GuildConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class GuildConfig : DbEntity
public string? WarnMessage { get; set; } = "-";
public HashSet<DelMsgOnCmdChannel> DelMsgOnCmdChannels { get; set; } = new();
public string? AutoAssignRoleId { get; set; } = "0";

public string XpImgUrl { get; set; }
public long StatsOptOut { get; set; } = 0;
public string CurrencyName { get; set; } = "Coins";

Expand Down
4 changes: 2 additions & 2 deletions src/Mewdeko.Votes/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public Task<IActionResult> TopggWebhook([FromBody] VoteModel data)
data.User,
data.Bot,
"top.gg");
_ = Task.Run(async () =>
_ = Task.Run(() =>
{
await Events.InvokeTopGg(data, Request.Headers.Authorization);
return Events.InvokeTopGg(data, Request.Headers.Authorization);
});
return Task.FromResult<IActionResult>(Ok());
}
Expand Down
Loading

0 comments on commit f0ef232

Please sign in to comment.