Skip to content

Commit 73de8f6

Browse files
authored
Merge pull request #66 from GedasFX/dev
Dev
2 parents a85e50b + 3cb2877 commit 73de8f6

File tree

79 files changed

+64050
-9649
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+64050
-9649
lines changed

Alderto.Bot/Alderto.Bot.csproj

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<UserSecretsId>c53fe5d3-16e9-400d-a588-4859345371e5</UserSecretsId>
77
<StartupObject>Alderto.Bot.Program</StartupObject>
88
<Configurations>Debug;Release</Configurations>
@@ -39,10 +39,10 @@
3939
</ItemGroup>
4040

4141
<ItemGroup>
42-
<PackageReference Include="Discord.Net" Version="2.1.1" />
43-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
44-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.0.0" />
45-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.0.0" />
42+
<PackageReference Include="Discord.Net" Version="2.2.0" />
43+
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.3" />
44+
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="3.1.3" />
45+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.3" />
4646
</ItemGroup>
4747

4848
<ItemGroup>

Alderto.Bot/Modules/CurrencyModule.cs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Text;
24
using System.Threading.Tasks;
35
using Alderto.Bot.Extensions;
46
using Alderto.Bot.Preconditions;
@@ -14,7 +16,8 @@ public class CurrencyModule : ModuleBase<SocketCommandContext>
1416
private readonly IGuildPreferencesProvider _guildPreferences;
1517
private readonly ICurrencyManager _currencyManager;
1618

17-
public CurrencyModule(IGuildMemberManager guildMemberManager,
19+
public CurrencyModule(
20+
IGuildMemberManager guildMemberManager,
1821
IGuildPreferencesProvider guildPreferences,
1922
ICurrencyManager currencyManager)
2023
{
@@ -131,5 +134,20 @@ public async Task Timely()
131134
// Points were given out.
132135
await this.ReplySuccessEmbedAsync(($"{user.Mention} was given {timelyAmount} {currencySymbol}. New total: **{dbUser.CurrencyCount}**."));
133136
}
137+
138+
[Command("top")]
139+
public async Task Top(int page = 1)
140+
{
141+
if (page < 1)
142+
{
143+
await this.ReplyErrorEmbedAsync("Selected page must be positive.");
144+
return;
145+
}
146+
147+
var topN = await _currencyManager.GetRichestUsersAsync(Context.Guild.Id, 50, (page - 1) * 50);
148+
149+
var res = topN.Aggregate(new StringBuilder(), (c, n) => c.Append($"<@{n.MemberId}>: {n.CurrencyCount}\n"));
150+
await this.ReplyEmbedAsync(res.ToString());
151+
}
134152
}
135153
}
+13-31
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
using System.Linq;
2+
using System.Text;
23
using System.Threading.Tasks;
34
using Alderto.Bot.Extensions;
45
using Alderto.Services;
56
using Alderto.Services.Exceptions;
7+
using Discord;
68
using Discord.Commands;
79
using Microsoft.EntityFrameworkCore;
810

911
namespace Alderto.Bot.Modules
1012
{
11-
[Group, Alias("GuildBank", "GB")]
13+
[Group, Alias("GuildBank", "GB", "GuildBanks", "Banks", "Bank")]
1214
public class GuildBankModule : ModuleBase<SocketCommandContext>
1315
{
1416
private readonly IGuildBankManager _guildBankManager;
@@ -18,44 +20,24 @@ public GuildBankModule(IGuildBankManager guildBankManager)
1820
_guildBankManager = guildBankManager;
1921
}
2022

21-
//[Command("Give"), Alias("Add")]
22-
//public async Task Give(IGuildUser transactor, string bankName, string itemName, double quantity)
23-
//{
24-
// if (itemName == "$")
25-
// {
26-
// // Special case - currency donation.
27-
// await _guildBankManager.ModifyCurrencyCountAsync(Context.Guild.Id, bankName, Context.User.Id, transactor.Id, quantity);
28-
// }
29-
// else
30-
// {
31-
// await _guildBankManager.ModifyItemCountAsync(Context.Guild.Id, bankName, Context.User.Id, transactor.Id, itemName, quantity);
32-
// }
33-
//}
34-
35-
//[Command("Take"), Alias("Remove")]
36-
//public async Task Take(IGuildUser transactor, string bankName, string itemName, double quantity)
37-
//{
38-
// if (itemName == "$")
39-
// {
40-
// // Special case - currency donation.
41-
// await _guildBankManager.ModifyCurrencyCountAsync(Context.Guild.Id, bankName, Context.User.Id, transactor.Id, -quantity);
42-
// }
43-
// else
44-
// {
45-
// await _guildBankManager.ModifyItemCountAsync(Context.Guild.Id, bankName, Context.User.Id, transactor.Id, itemName, -quantity);
46-
// }
47-
//}
23+
[Command("list")]
24+
public async Task List()
25+
{
26+
var banks = await _guildBankManager.GetGuildBanksAsync(Context.Guild.Id);
27+
await this.ReplyEmbedAsync(banks.Aggregate(new StringBuilder(), (c, i) => c.Append($"**{i.Name,32}**\n")).ToString());
28+
}
4829

49-
[Command("Items"), Alias("List")]
30+
[Command("items")]
5031
public async Task Items(string bankName)
5132
{
5233
var bank = await _guildBankManager.GetGuildBankAsync(Context.Guild.Id, bankName,
5334
b => b.Include(g => g.Contents));
5435
if (bank == null)
5536
throw new BankNotFoundException();
5637

57-
var res = bank.Contents.Aggregate(seed: "", (current, item) => current + $"{item.Name} {item.Description}\n");
58-
await this.ReplySuccessEmbedAsync(res);
38+
var res = bank.Contents.Aggregate(new StringBuilder(), (current, item) =>
39+
current.Append($"**{item.Name}**\n{(string.IsNullOrEmpty(item.Description) ? "N/A" : item.Description)}\n*qty*: {item.Quantity} @ {item.Value} ea.\n\n"));
40+
await this.ReplyEmbedAsync(res.ToString());
5941
}
6042
}
6143
}

Alderto.Bot/Modules/HelpModule.cs

+1-14
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,11 @@ namespace Alderto.Bot.Modules
77
[Group("Help")]
88
public class HelpModule : ModuleBase<SocketCommandContext>
99
{
10-
private readonly CommandService _commands;
11-
12-
public HelpModule(CommandService commands)
13-
{
14-
_commands = commands;
15-
}
16-
1710
[Command]
1811
[Summary("Shows help menu")]
1912
public async Task Help()
2013
{
21-
await this.ReplyEmbedAsync(extra: builder =>
22-
{
23-
foreach (var command in _commands.Commands)
24-
{
25-
builder.AddField(command.Name, command.Summary);
26-
}
27-
});
14+
await this.ReplyEmbedAsync("For command reference visit https://alderto.com/documentation");
2815
}
2916
}
3017
}

Alderto.Data/Alderto.Data.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</PropertyGroup>
1919

2020
<ItemGroup>
21-
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0" />
21+
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.3" />
2222
</ItemGroup>
2323

2424
</Project>

Alderto.Services/Alderto.Services.csproj

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="Discord.Net.Core" Version="2.1.1" />
19-
<PackageReference Include="Discord.Net.WebSocket" Version="2.1.1" />
18+
<PackageReference Include="Discord.Net.Core" Version="2.2.0" />
19+
<PackageReference Include="Discord.Net.WebSocket" Version="2.2.0" />
2020
</ItemGroup>
2121

2222
<ItemGroup>

Alderto.Services/Exceptions/ErrorMessage.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ public static class ErrorMessages
7878
public static ErrorMessage BankItemNotFound { get; } =
7979
new ErrorMessage(404, 2201, "The specified bank item was not found.");
8080

81+
/// <summary>
82+
/// NotFound reason. Used when bot was unable to find the preference.
83+
/// </summary>
84+
public static ErrorMessage GuildPreferenceNotFound { get; } =
85+
new ErrorMessage(404, 2301, "The specified preference was not found,");
86+
8187
// === BadRequest ===
8288

8389
/// <summary>
@@ -126,7 +132,7 @@ public static class ErrorMessages
126132

127133
public static ErrorMessage ChannelNotMessageChannel { get; } =
128134
new ErrorMessage(400, 3401, "The specified channel is not a message channel.");
129-
135+
130136
/// <summary>
131137
/// Converts a internal code to ErrorMessage.
132138
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Alderto.Services.Exceptions
2+
{
3+
public class GuildPreferenceNotFoundException : ApiException
4+
{
5+
public GuildPreferenceNotFoundException() : base(ErrorMessages.GuildPreferenceNotFound)
6+
{
7+
}
8+
}
9+
}

Alderto.Services/ICurrencyManager.cs

+10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34
using Alderto.Data.Models;
45

@@ -23,5 +24,14 @@ public interface ICurrencyManager
2324
/// <param name="cooldown">Time (in seconds) between timely claims.</param>
2425
/// <returns>Time remaining until next claim. If null - points were given out.</returns>
2526
Task<TimeSpan?> GrantTimelyRewardAsync(GuildMember guildMember, int amount, int cooldown);
27+
28+
/// <summary>
29+
/// Gets the richest N users of the guild.
30+
/// </summary>
31+
/// <param name="guildId">Id of guild to search.</param>
32+
/// <param name="take">N</param>
33+
/// <param name="skip">Amount of users to ignore (pagination).</param>
34+
/// <returns>Top N richest players after skiping some.</returns>
35+
Task<IEnumerable<GuildMember>> GetRichestUsersAsync(ulong guildId, int take = 10, int skip = 0);
2636
}
2737
}

Alderto.Services/Impl/CurrencyManager.cs

+13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Threading.Tasks;
35
using Alderto.Data;
46
using Alderto.Data.Models;
7+
using Microsoft.EntityFrameworkCore;
58

69
namespace Alderto.Services.Impl
710
{
@@ -36,5 +39,15 @@ public async Task ModifyPointsAsync(GuildMember guildMember, int deltaPoints)
3639

3740
return null;
3841
}
42+
43+
public async Task<IEnumerable<GuildMember>> GetRichestUsersAsync(ulong guildId, int take = 10, int skip = 0)
44+
{
45+
return await _context.GuildMembers.AsQueryable()
46+
.Where(g => g.GuildId == guildId)
47+
.OrderByDescending(g => g.CurrencyCount)
48+
.Skip(skip)
49+
.Take(take)
50+
.ToListAsync();
51+
}
3952
}
4053
}

Alderto.Services/Impl/GuildMemberManager.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public async Task AddRecruitAsync(GuildMember recruitedMember, ulong recruiterMe
101101

102102
public IEnumerable<GuildMember> ListRecruitsAsync(GuildMember member)
103103
{
104-
return _context.GuildMembers
104+
return _context.GuildMembers.AsQueryable()
105105
.Where(g => g.GuildId == member.GuildId && g.RecruiterMemberId == member.MemberId);
106106
}
107107

Alderto.Services/Impl/MessagesManager.cs

+9-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public MessagesManager(IDiscordClient client, AldertoDbContext context)
2323

2424
public Task<List<GuildManagedMessage>> ListMessagesAsync(ulong guildId)
2525
{
26-
return _context.GuildManagedMessages.Where(m => m.GuildId == guildId).ToListAsync();
26+
return _context.GuildManagedMessages.AsQueryable()
27+
.Where(m => m.GuildId == guildId)
28+
.ToListAsync();
2729
}
2830

2931
public async Task<GuildManagedMessage?> GetMessageAsync(ulong guildId, ulong messageId)
@@ -47,8 +49,9 @@ public async Task<GuildManagedMessage> PostMessageAsync(ulong guildId, ulong cha
4749

4850
public async Task<GuildManagedMessage> ImportMessageAsync(ulong guildId, ulong channelId, ulong messageId, ulong? moderatorRoleId = null)
4951
{
50-
var dbMsg = await _context.GuildManagedMessages.SingleOrDefaultAsync(m =>
51-
m.GuildId == guildId && m.MessageId == messageId);
52+
var dbMsg = await _context.GuildManagedMessages.AsQueryable()
53+
.SingleOrDefaultAsync(m => m.GuildId == guildId && m.MessageId == messageId);
54+
5255
if (dbMsg != null)
5356
return dbMsg;
5457

@@ -98,8 +101,9 @@ public async Task RemoveMessageAsync(ulong guildId, ulong messageId)
98101
// User can always delete its own posts.
99102
var message = await GetDiscordBotMessageAsync(dbMsg);
100103
await message.DeleteAsync();
101-
} catch (MessageNotFoundException) { /* Message is already gone from discord. Ignore error. */ }
102-
104+
}
105+
catch (MessageNotFoundException) { /* Message is already gone from discord. Ignore error. */ }
106+
103107

104108
_context.GuildManagedMessages.Remove(dbMsg);
105109
await _context.SaveChangesAsync();

Alderto.Tests/Alderto.Tests.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
</PropertyGroup>
2828

2929
<ItemGroup>
30-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.0.0" />
31-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.3.0" />
32-
<PackageReference Include="Moq" Version="4.13.0" />
30+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="3.1.3" />
31+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
32+
<PackageReference Include="Moq" Version="4.14.1" />
3333
<PackageReference Include="xunit" Version="2.4.1" />
3434
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1">
3535
<PrivateAssets>all</PrivateAssets>

Alderto.Tests/MockedEntities/MockGuild.cs

+22-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Threading.Tasks;
56
using Discord;
67
using Discord.Audio;
7-
using Discord.WebSocket;
8-
using Moq;
98

109
namespace Alderto.Tests.MockedEntities
1110
{
@@ -265,6 +264,9 @@ public Task DeleteEmoteAsync(GuildEmote emote, RequestOptions options = null)
265264
throw new NotImplementedException();
266265
}
267266

267+
public Task<IRole> CreateRoleAsync(string name, GuildPermissions? permissions = null, Color? color = null, bool isHoisted = false, bool isMentionable = false, RequestOptions options = null) => throw new NotImplementedException();
268+
public Task<IReadOnlyCollection<IAuditLogEntry>> GetAuditLogsAsync(int limit = 100, CacheMode mode = CacheMode.AllowDownload, RequestOptions options = null, ulong? beforeId = null, ulong? userId = null, ActionType? actionType = null) => throw new NotImplementedException();
269+
268270
public string Name { get; set; }
269271
public int AFKTimeout { get; set; }
270272
public bool IsEmbeddable { get; set; }
@@ -289,6 +291,24 @@ public Task DeleteEmoteAsync(GuildEmote emote, RequestOptions options = null)
289291
public IReadOnlyCollection<GuildEmote> Emotes { get; set; }
290292
public IReadOnlyCollection<string> Features { get; set; }
291293
public IReadOnlyCollection<IRole> Roles { get; set; }
294+
295+
public PremiumTier PremiumTier => throw new NotImplementedException();
296+
297+
public string BannerId => throw new NotImplementedException();
298+
299+
public string BannerUrl => throw new NotImplementedException();
300+
301+
public string VanityURLCode => throw new NotImplementedException();
302+
303+
public SystemChannelMessageDeny SystemChannelFlags => throw new NotImplementedException();
304+
305+
public string Description => throw new NotImplementedException();
306+
307+
public int PremiumSubscriptionCount => throw new NotImplementedException();
308+
309+
public string PreferredLocale => throw new NotImplementedException();
310+
311+
public CultureInfo PreferredCulture => throw new NotImplementedException();
292312
#nullable restore
293313
}
294314
}

Alderto.Tests/MockedEntities/MockGuildUser.cs

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Collections.Immutable;
34
using System.Threading.Tasks;
45
using Discord;
56

@@ -82,5 +83,11 @@ public Task RemoveRolesAsync(IEnumerable<IRole> roles, RequestOptions options =
8283
public IGuild Guild { get; set; }
8384
public ulong GuildId { get; set; }
8485
public IReadOnlyCollection<ulong> RoleIds { get; set; }
86+
87+
public DateTimeOffset? PremiumSince => throw new NotImplementedException();
88+
89+
public IImmutableSet<ClientType> ActiveClients => throw new NotImplementedException();
90+
91+
public bool IsStreaming => throw new NotImplementedException();
8592
}
8693
}

0 commit comments

Comments
 (0)