Skip to content

Commit 7aaf090

Browse files
authored
Merge pull request #55 from GedasFX/dev
1.0.0rc1
2 parents 4d91beb + 9362f1e commit 7aaf090

File tree

135 files changed

+4836
-2561
lines changed

Some content is hidden

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

135 files changed

+4836
-2561
lines changed

Alderto.Bot.Lua/Alderto.Bot.Lua.csproj

+10-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
<OutputType>Library</OutputType>
55
<TargetFramework>netstandard2.1</TargetFramework>
66
<LangVersion>8.0</LangVersion>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
11+
<CodeAnalysisRuleSet>..\Custom Ruleset.ruleset</CodeAnalysisRuleSet>
12+
</PropertyGroup>
13+
14+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
15+
<CodeAnalysisRuleSet>..\Custom Ruleset.ruleset</CodeAnalysisRuleSet>
716
</PropertyGroup>
817

918
<ItemGroup>
10-
<PackageReference Include="NLua" Version="1.4.18" />
19+
<PackageReference Include="NLua" Version="1.4.24" />
1120
</ItemGroup>
1221

1322
<ItemGroup>

Alderto.Bot.Lua/CustomCommandProvider.cs

+14-16
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ public async Task<object[]> RunCommandAsync(string functionName, params object[]
6666
throw new LuaCommandNotFoundException($"Function {functionName} does not exist or is not registered.");
6767

6868
// Function exists. Execute it.
69-
using (var c = new CancellationTokenSource())
70-
{
71-
// Safeguard against infinite loops and such.
72-
c.CancelAfter(CustomCommandExecTimeout);
7369

74-
// Wrap an array of objects into a new array so the function in Lua has args parameter as an array.
75-
return await Task.Run(NewMethod(args, func), c.Token);
76-
}
70+
// Safeguard against infinite loops and such.
71+
using var c = new CancellationTokenSource();
72+
c.CancelAfter(CustomCommandExecTimeout);
73+
74+
// Wrap an array of objects into a new array so the function in Lua has args parameter as an array.
75+
return await Task.Run(NewMethod(args, func), c.Token);
7776
}
7877

7978
private static System.Func<object[]> NewMethod(object[] args, LuaFunction func) => () => func.Call(new object[] { args });
@@ -91,7 +90,7 @@ public async Task ReloadCommands(ulong guildId)
9190

9291
if (/* guild.PremiumUntil != null */ /* premium feature. For now free */ true)
9392
{
94-
foreach (var cmd in guild.CustomCommands)
93+
foreach (var cmd in guild.CustomCommands!)
9594
{
9695
await RegisterCommand($"_{guildId}_{cmd.TriggerKeyword}", cmd.LuaCode);
9796
}
@@ -106,15 +105,14 @@ public async Task ReloadCommands(ulong guildId)
106105
/// <returns></returns>
107106
public async Task RegisterCommand(string functionName, string code)
108107
{
109-
using (var c = new CancellationTokenSource())
108+
using var c = new CancellationTokenSource();
109+
c.CancelAfter(CustomCommandExecTimeout);
110+
111+
await Task.Run(() =>
110112
{
111-
c.CancelAfter(CustomCommandExecTimeout);
112-
await Task.Run(() =>
113-
{
114-
_luaState.DoString($"function {functionName} (args) {code} end");
115-
_commands[functionName] = _luaState.GetFunction(functionName);
116-
}, c.Token);
117-
}
113+
_luaState.DoString($"function {functionName} (args) {code} end");
114+
_commands[functionName] = _luaState.GetFunction(functionName);
115+
}, c.Token);
118116
}
119117
}
120118
}

Alderto.Bot/Alderto.Bot.csproj

+12-3
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,21 @@
77
<StartupObject>Alderto.Bot.Program</StartupObject>
88
<Configurations>Debug;Release</Configurations>
99
<LangVersion>8.0</LangVersion>
10+
<Nullable>enable</Nullable>
1011
</PropertyGroup>
1112

1213
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Testing|AnyCPU'">
1314
<DefineConstants>TRACE;TEST</DefineConstants>
1415
</PropertyGroup>
1516

17+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
18+
<CodeAnalysisRuleSet>..\Custom Ruleset.ruleset</CodeAnalysisRuleSet>
19+
</PropertyGroup>
20+
21+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
22+
<CodeAnalysisRuleSet>..\Custom Ruleset.ruleset</CodeAnalysisRuleSet>
23+
</PropertyGroup>
24+
1625
<ItemGroup>
1726
<None Remove="appsettings.json" />
1827
<None Remove="commands.json" />
@@ -31,9 +40,9 @@
3140

3241
<ItemGroup>
3342
<PackageReference Include="Discord.Net" Version="2.1.1" />
34-
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
35-
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.2.0" />
36-
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="2.2.0" />
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" />
3746
</ItemGroup>
3847

3948
<ItemGroup>

Alderto.Bot/DependencyInjection.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public static class DependencyInjection
2020
/// <param name="botToken">Token of the bot.</param>
2121
/// <param name="config">Additional options to configure bot with.</param>
2222
public static IServiceCollection AddDiscordSocketClient(this IServiceCollection services,
23-
string botToken, Action<DiscordSocketConfig> config = null) =>
24-
services.AddSingleton<DiscordSocketClient, DiscordSocketClientWrapper>()
23+
string botToken, Action<DiscordSocketConfig>? config = null) =>
24+
services.AddSingleton<IDiscordClient, DiscordSocketClientWrapper>()
2525
.Configure<DiscordSocketConfigWrapper>(localConfig =>
2626
{
2727
localConfig.BotToken = botToken;
@@ -30,7 +30,7 @@ public static IServiceCollection AddDiscordSocketClient(this IServiceCollection
3030

3131
private class DiscordSocketConfigWrapper : DiscordSocketConfig
3232
{
33-
public string BotToken { get; set; }
33+
public string? BotToken { get; set; }
3434
}
3535
private class DiscordSocketClientWrapper : DiscordSocketClient
3636
{
@@ -48,7 +48,7 @@ public DiscordSocketClientWrapper(ILogger<DiscordSocketClient> logger,
4848
return Task.CompletedTask;
4949
};
5050

51-
Run(config.Value.BotToken).ConfigureAwait(false);
51+
Run(config.Value.BotToken!).ConfigureAwait(false);
5252
}
5353

5454
private async Task Run(string token)
@@ -64,23 +64,23 @@ private async Task Run(string token)
6464
/// <param name="services"><see cref="IServiceCollection"/> to add to.</param>
6565
/// <param name="config">Additional options to configure bot with.</param>
6666
public static IServiceCollection AddCommandService(this IServiceCollection services,
67-
Action<CommandServiceConfig> config = null) =>
67+
Action<CommandServiceConfig>? config = null) =>
6868
services.AddSingleton<CommandService, CommandServiceWrapper>().Configure(config);
6969

7070
private class CommandServiceWrapper : CommandService
7171
{
72-
public CommandServiceWrapper(DiscordSocketClient client, ILogger<CommandService> logger,
72+
public CommandServiceWrapper(IDiscordClient client, ILogger<CommandService> logger,
7373
IOptions<CommandServiceConfig> cmdServiceConfig, IConfiguration config) : base(cmdServiceConfig.Value)
7474
{
7575
if (ulong.TryParse(config["LoggingChannelId"], out var loggingChannelId))
7676
{
77+
var channel = (IMessageChannel)client.GetChannelAsync(loggingChannelId).Result;
7778
Log += async message =>
7879
{
7980
if (message.Exception is CommandException commandException)
8081
{
81-
await ((IMessageChannel)client.GetChannel(loggingChannelId))
82-
.SendMessageAsync(
83-
$"```{commandException.Message}\n{commandException.InnerException}```");
82+
await channel.SendMessageAsync(
83+
$"```{commandException.Message}\n{commandException.InnerException}```");
8484
}
8585
};
8686
}

Alderto.Bot/Extensions/EmbedBuilderExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ public static class EmbedBuilderExtensions
1616
/// <param name="extra">Additional actions to apply to the builder.</param>
1717
/// <returns><see cref="builder"/></returns>
1818
public static EmbedBuilder WithDefault(this EmbedBuilder builder,
19-
string description = null,
19+
string? description = null,
2020
Color embedColor = default,
21-
IUser author = null,
22-
Action<EmbedBuilder> extra = null)
21+
IUser? author = null,
22+
Action<EmbedBuilder>? extra = null)
2323
{
2424
builder.WithColor(embedColor == default ? EmbedColor.Info : embedColor);
2525

Alderto.Bot/Extensions/ModuleBaseExtensions.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ public static class ModuleBaseExtensions
1818
/// <param name="extra">Additional actions to apply to the builder.</param>
1919
/// <returns></returns>
2020
public static async Task ReplyEmbedAsync<T>(this ModuleBase<T> module,
21-
string description = null,
21+
string? description = null,
2222
Color color = default,
23-
IUser author = null,
24-
Action<EmbedBuilder> extra = null) where T : class, ICommandContext
23+
IUser? author = null,
24+
Action<EmbedBuilder>? extra = null) where T : class, ICommandContext
2525
{
2626
var embed = new EmbedBuilder().WithDefault(description, color, author ?? module.Context.Message.Author, extra).Build();
2727
await module.Context.Channel.SendMessageAsync(embed: embed);
@@ -36,8 +36,8 @@ public static async Task ReplyEmbedAsync<T>(this ModuleBase<T> module,
3636
/// <param name="extra">Additional actions to apply to the builder.</param>
3737
/// <returns></returns>
3838
public static Task ReplySuccessEmbedAsync<T>(this ModuleBase<T> module,
39-
string description = null,
40-
Action<EmbedBuilder> extra = null) where T : class, ICommandContext
39+
string? description = null,
40+
Action<EmbedBuilder>? extra = null) where T : class, ICommandContext
4141
{
4242
return ReplyEmbedAsync(module, description, EmbedColor.Success, extra: extra);
4343
}
@@ -51,8 +51,8 @@ public static Task ReplySuccessEmbedAsync<T>(this ModuleBase<T> module,
5151
/// <param name="extra">Additional actions to apply to the builder.</param>
5252
/// <returns></returns>
5353
public static Task ReplyErrorEmbedAsync<T>(this ModuleBase<T> module,
54-
string description = null,
55-
Action<EmbedBuilder> extra = null) where T : class, ICommandContext
54+
string? description = null,
55+
Action<EmbedBuilder>? extra = null) where T : class, ICommandContext
5656
{
5757
return ReplyEmbedAsync(module, description, EmbedColor.Error, extra: extra);
5858
}

Alderto.Bot/Modules/CurrencyModule.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private async Task<Embed> ModifyAsyncExec(int qty, IEnumerable<IGuildUser> guild
8383
var no = 1;
8484
foreach (var user in guildUsers)
8585
{
86-
var member = await _guildMemberManager.GetGuildMemberAsync(user);
86+
var member = (await _guildMemberManager.GetGuildMemberAsync(user))!;
8787
await _currencyManager.ModifyPointsAsync(member, qty);
8888

8989
// Format a nice output.
@@ -96,13 +96,13 @@ private async Task<Embed> ModifyAsyncExec(int qty, IEnumerable<IGuildUser> guild
9696
[Command("$")]
9797
[Summary("Checks the amount of points a given user has.")]
9898
public async Task CheckAsync(
99-
[Summary("Person to check. If no user was provided, checks personal points.")] IGuildUser user = null)
99+
[Summary("Person to check. If no user was provided, checks personal points.")] IGuildUser? user = null)
100100
{
101101
if (user == null)
102102
user = (IGuildUser)Context.Message.Author;
103103

104104
var currencySymbol = (await _guildPreferences.GetPreferencesAsync(user.GuildId)).CurrencySymbol;
105-
var dbUser = await _guildMemberManager.GetGuildMemberAsync(user);
105+
var dbUser = (await _guildMemberManager.GetGuildMemberAsync(user))!;
106106

107107
await this.ReplyEmbedAsync($"{user.Mention} has {dbUser.CurrencyCount} {currencySymbol}");
108108
}
@@ -112,7 +112,7 @@ public async Task CheckAsync(
112112
public async Task Timely()
113113
{
114114
var user = (IGuildUser)Context.User;
115-
var dbUser = await _guildMemberManager.GetGuildMemberAsync(user.GuildId, user.Id);
115+
var dbUser = (await _guildMemberManager.GetGuildMemberAsync(user.GuildId, user.Id))!;
116116

117117
var preferences = await _guildPreferences.GetPreferencesAsync(user.GuildId);
118118
var timelyCooldown = preferences.TimelyCooldown;

Alderto.Bot/Modules/GuildBankModule.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Linq;
32
using System.Threading.Tasks;
43
using Alderto.Bot.Extensions;
5-
using Alderto.Data.Models.GuildBank;
6-
using Alderto.Services.GuildBankManagers;
7-
using Discord;
4+
using Alderto.Services;
5+
using Alderto.Services.Exceptions;
86
using Discord.Commands;
97
using Microsoft.EntityFrameworkCore;
108

@@ -13,7 +11,6 @@ namespace Alderto.Bot.Modules
1311
[Group, Alias("GuildBank", "GB")]
1412
public class GuildBankModule : ModuleBase<SocketCommandContext>
1513
{
16-
1714
private readonly IGuildBankManager _guildBankManager;
1815

1916
public GuildBankModule(IGuildBankManager guildBankManager)
@@ -54,6 +51,8 @@ public async Task Items(string bankName)
5451
{
5552
var bank = await _guildBankManager.GetGuildBankAsync(Context.Guild.Id, bankName,
5653
b => b.Include(g => g.Contents));
54+
if (bank == null)
55+
throw new BankNotFoundException();
5756

5857
var res = bank.Contents.Aggregate(seed: "", (current, item) => current + $"{item.Name} {item.Description}\n");
5958
await this.ReplySuccessEmbedAsync(res);

Alderto.Bot/Modules/RecruitmentModule.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public async Task Recruited(
2828
var recruiterId = recruiter.Id;
2929
foreach (var member in recruited)
3030
{
31-
var dbUser = await _guildMemberManager.GetGuildMemberAsync(recruiter.GuildId, member.Id);
31+
var dbUser = (await _guildMemberManager.GetGuildMemberAsync(recruiter.GuildId, member.Id))!;
3232
await _guildMemberManager.AddRecruitAsync(dbUser, recruiterId, DateTimeOffset.UtcNow);
3333
}
3434

@@ -38,12 +38,12 @@ public async Task Recruited(
3838
[Command("List")]
3939
[Summary("Lists all member recruited by the person.")]
4040
public async Task List(
41-
[Summary("Recruiter. Not specifying a user will list your own recruits.")] IGuildUser user = null)
41+
[Summary("Recruiter. Not specifying a user will list your own recruits.")] IGuildUser? user = null)
4242
{
4343
if (user == null)
4444
user = (IGuildUser)Context.User;
4545

46-
var recruits = _guildMemberManager.ListRecruitsAsync(await _guildMemberManager.GetGuildMemberAsync(user));
46+
var recruits = _guildMemberManager.ListRecruitsAsync((await _guildMemberManager.GetGuildMemberAsync(user))!);
4747

4848
await this.ReplyEmbedAsync(extra: builder =>
4949
{
@@ -59,7 +59,7 @@ await this.ReplyEmbedAsync(extra: builder =>
5959
[Command("By")]
6060
[Summary("Displays the member this person was recruited by.")]
6161
public async Task By(
62-
[Summary("Recruit. Not specifying a user will display your recruiter.")] IGuildUser member = null)
62+
[Summary("Recruit. Not specifying a user will display your recruiter.")] IGuildUser? member = null)
6363
{
6464
if (member == null)
6565
member = (IGuildUser)Context.User;
@@ -71,7 +71,7 @@ public async Task By(
7171
return;
7272
}
7373

74-
var recruiter = await _guildMemberManager.GetGuildMemberAsync(member.GuildId, (ulong)dbMember.RecruiterMemberId);
74+
var recruiter = (await _guildMemberManager.GetGuildMemberAsync(member.GuildId, (ulong)dbMember.RecruiterMemberId))!;
7575
await this.ReplyEmbedAsync($"{member.Mention} was recruited by <@{recruiter.MemberId}>.");
7676
}
7777
}

Alderto.Bot/Modules/UserManagementModule.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public UserManagementModule(IGuildMemberManager guildMemberManager, IGuildPrefer
2626
[RequireBotPermission(GuildPermission.ManageNicknames | GuildPermission.ManageRoles)]
2727
public async Task Accept(
2828
[Summary("User")] IGuildUser user,
29-
[Remainder] [Summary("Nickname. Does not change nickname if none was specified.")] string nickname = null)
29+
[Remainder] [Summary("Nickname. Does not change nickname if none was specified.")] string? nickname = null)
3030
{
3131
var pref = await _guildPreferencesProvider.GetPreferencesAsync(user.GuildId);
3232

Alderto.Bot/Services/CommandHandler.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@ public class CommandHandler
1818
private readonly IGuildPreferencesProvider _guildPreferences;
1919

2020
public CommandHandler(
21-
DiscordSocketClient client,
21+
IDiscordClient client,
2222
CommandService commands,
2323
IServiceProvider services,
2424
IGuildPreferencesProvider guildPreferences)
2525
{
26-
_client = client;
26+
if (!(client is DiscordSocketClient socketClient))
27+
throw new ArgumentException("Discord client must be of Socket type to use Command Handler.");
28+
29+
_client = socketClient;
2730
_commands = commands;
2831
_services = services;
2932
_guildPreferences = guildPreferences;
@@ -52,7 +55,6 @@ public async Task StartAsync()
5255
// See Dependency Injection guide for more information.
5356
using var scope = _services.CreateScope();
5457
await _commands.AddModulesAsync(Assembly.GetExecutingAssembly(), scope.ServiceProvider);
55-
5658
}
5759

5860
public async Task HandleCommandAsync(SocketMessage messageParam)

Alderto.Bot/Startup.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Threading.Tasks;
44
using Alderto.Bot.Services;
55
using Alderto.Data;
6-
using Alderto.Services;
76
using Discord;
87
using Discord.Commands;
98
using Microsoft.EntityFrameworkCore;
@@ -61,7 +60,7 @@ public async Task RunAsync()
6160
using (var scope = services.CreateScope())
6261
{
6362
Console.Out.WriteLine("Initializing database...");
64-
using (var context = scope.ServiceProvider.GetRequiredService<AldertoDbContext>())
63+
await using (var context = scope.ServiceProvider.GetRequiredService<AldertoDbContext>())
6564
{
6665
await context.Database.MigrateAsync();
6766
}

Alderto.Data/Alderto.Data.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,19 @@
44
<OutputType>Library</OutputType>
55
<TargetFramework>netstandard2.1</TargetFramework>
66
<LangVersion>8.0</LangVersion>
7+
<Nullable>enable</Nullable>
78
<ApplicationIcon />
89
<StartupObject />
910
</PropertyGroup>
1011

12+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
13+
<CodeAnalysisRuleSet>..\Custom Ruleset.ruleset</CodeAnalysisRuleSet>
14+
</PropertyGroup>
15+
16+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
17+
<CodeAnalysisRuleSet>..\Custom Ruleset.ruleset</CodeAnalysisRuleSet>
18+
</PropertyGroup>
19+
1120
<ItemGroup>
1221
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.0.0" />
1322
</ItemGroup>

0 commit comments

Comments
 (0)