Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit 3abba19

Browse files
Merge pull request #64 from InfinityGhost/nullable
Resolve nullability issues
2 parents cb0ad54 + 9634394 commit 3abba19

13 files changed

Lines changed: 44 additions & 56 deletions

File tree

TabletBot.Discord/Commands/CommandModule.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ namespace TabletBot.Discord.Commands
99
public class CommandModule : ModuleBase
1010
{
1111
protected override Task<IUserMessage> ReplyAsync(
12-
string message = null,
12+
string? message = null,
1313
bool isTTS = false,
14-
Embed embed = null,
15-
RequestOptions options = null,
16-
AllowedMentions allowedMentions = null,
17-
MessageReference messageReference = null,
18-
MessageComponent components = null,
19-
ISticker[] stickers = null,
20-
Embed[] embeds = null
14+
Embed? embed = null,
15+
RequestOptions? options = null,
16+
AllowedMentions? allowedMentions = null,
17+
MessageReference? messageReference = null,
18+
MessageComponent? components = null,
19+
ISticker[]? stickers = null,
20+
Embed[]? embeds = null
2121
)
2222
{
2323
messageReference ??= Context.Message.ToReference();

TabletBot.Discord/Embeds/GitHubEmbeds.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ public static Embed GetPullRequestEmbed(PullRequest pr)
2626
{
2727
var embed = new EmbedBuilder
2828
{
29-
Title = string.Format("{0} #{1}", pr.Title, pr.Number),
29+
Title = $"{pr.Title} #{pr.Number}",
3030
Timestamp = pr.UpdatedAt,
3131
Url = pr.HtmlUrl,
3232
Footer = new EmbedFooterBuilder
3333
{
34-
Text = string.Format("{0} opened this pull request on {1}", pr.User?.Login, pr.CreatedAt),
35-
IconUrl = pr.User.AvatarUrl
34+
Text = $"{pr.User?.Login} opened this pull request on {pr.CreatedAt}",
35+
IconUrl = pr.User?.AvatarUrl
3636
},
3737
Description = pr.Body ?? string.Empty
3838
};

TabletBot.Discord/Embeds/SnippetEmbeds.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace TabletBot.Discord.Embeds
88
{
99
public static class SnippetEmbeds
1010
{
11-
public static bool GetSnippetEmbed(Settings settings, string prefix, out EmbedBuilder embed)
11+
public static bool GetSnippetEmbed(Settings settings, string? prefix, out EmbedBuilder embed)
1212
{
13-
if (settings.Snippets.FirstOrDefault(s => s.Snippet == prefix) is SnippetStore snippet)
13+
if (settings.Snippets.FirstOrDefault(s => s.Snippet == prefix!) is SnippetStore snippet)
1414
{
1515
embed = new EmbedBuilder
1616
{

TabletBot.Discord/Extensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static IServiceCollection AddSlashCommandModule<T>(this IServiceCollectio
4242
public static T FirstOfType<TSource, T>(this IEnumerable<TSource> enumerable)
4343
where T : class, TSource
4444
{
45-
return enumerable.First(i => i is T) as T;
45+
return (T)enumerable.First(i => i is T)!;
4646
}
4747

4848
public static IEnumerable<Type> OfType<T>(this IEnumerable<Type> types)

TabletBot.Discord/SlashCommands/Extensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ namespace TabletBot.Discord.SlashCommands
55
{
66
internal static class Extensions
77
{
8-
public static T GetValue<T>(this SocketSlashCommand command, string option, T fallback = default(T))
8+
public static T? GetValue<T>(this SocketSlashCommand command, string option, T? fallback = default)
99
{
10-
var value = command.Data.Options?.FirstOrDefault(o => o.Name == option)?.Value;
11-
return value is T ? (T)value : fallback;
10+
var obj = command.Data.Options?.FirstOrDefault(o => o.Name == option)?.Value;
11+
return obj is T value ? value : fallback;
1212
}
1313
}
1414
}

TabletBot.Discord/SlashCommands/ModerationSlashCommands.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ private async Task Delete(SocketSlashCommand command)
170170
var amount = (int)command.GetValue<long>("amount", 1);
171171

172172
var messages = await command.Channel.GetMessagesAsync(amount).FlattenAsync();
173-
await (command.Channel as ITextChannel).DeleteMessagesAsync(messages);
173+
await (command.Channel as ITextChannel)!.DeleteMessagesAsync(messages);
174174
await command.FollowupAsync($"Deleted {amount} messages.");
175175
}
176176

@@ -198,7 +198,7 @@ private async Task Ban(SocketSlashCommand command)
198198
var userId = command.GetValue<ulong>("user");
199199
var reason = command.GetValue<string>("reason");
200200

201-
var user = await (command.Channel as IGuildChannel).GetUserAsync(userId);
201+
var user = await (command.Channel as IGuildChannel)!.GetUserAsync(userId);
202202

203203
if (user is IGuildUser)
204204
{

TabletBot.Discord/SlashCommands/SlashCommand.cs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,20 @@ namespace TabletBot.Discord.SlashCommands
88
{
99
public class SlashCommand
1010
{
11-
public string Name { set; get; }
12-
public SlashCommandBuilder Builder { set; get; }
13-
public Func<SocketSlashCommand, Task> Handler { set; get; }
11+
public string Name { set; get; } = string.Empty;
12+
public SlashCommandBuilder? Builder { set; get; }
13+
public Func<SocketSlashCommand, Task> Handler { set; get; } = _ => Task.CompletedTask;
1414
public GuildPermissions? MinimumPermissions { set; get; }
1515
public bool Ephemeral { set; get; }
1616

17-
public SlashCommandProperties Build() => Builder.Build();
17+
public SlashCommandProperties Build() => Builder!.Build();
1818

1919
public async Task Invoke(SocketSlashCommand command)
2020
{
2121
await command.DeferAsync(Ephemeral);
22-
if (MinimumPermissions is GuildPermissions permissions)
22+
if (MinimumPermissions != null)
2323
{
24-
var user = command.User as IGuildUser;
25-
if (HasCorrectPermissions(user))
24+
if (command.User as IGuildUser is IGuildUser user && HasCorrectPermissions(user))
2625
{
2726
await Handler(command);
2827
}
@@ -39,7 +38,7 @@ public async Task Invoke(SocketSlashCommand command)
3938

4039
private bool HasCorrectPermissions(IGuildUser user)
4140
{
42-
var guildPermissions = MinimumPermissions.Value;
41+
var guildPermissions = MinimumPermissions!.Value;
4342
var userPermissions = user.GuildPermissions;
4443

4544
return userPermissions.Administrator ||

TabletBot.Discord/SlashCommands/SlashCommandModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace TabletBot.Discord.SlashCommands
88
{
99
public abstract class SlashCommandModule
1010
{
11-
public event Func<SlashCommandModule, Task> Update;
11+
public event Func<SlashCommandModule, Task>? Update;
1212

1313
public void OnUpdate() => Update?.Invoke(this);
1414

@@ -23,7 +23,7 @@ public async Task HandleInteraction(SocketInteraction interaction)
2323
}
2424
}
2525

26-
public IList<SlashCommand> CommandHandlers { set; get; }
26+
public IList<SlashCommand> CommandHandlers { set; get; } = Array.Empty<SlashCommand>();
2727

2828
protected abstract IEnumerable<SlashCommand> GetSlashCommands();
2929

TabletBot.Discord/SlashCommands/SnippetSlashCommands.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private async Task SetSnippet(SocketSlashCommand command)
153153
{
154154
var snippet = command.GetValue<string>("snippet");
155155
var title = command.GetValue<string>("title");
156-
var content = command.GetValue<string>("content").Replace(@"\n", Environment.NewLine);
156+
var content = command.GetValue<string>("content")?.Replace(@"\n", Environment.NewLine);
157157

158158
if (Snippets.FirstOrDefault(s => s.Snippet == snippet) is SnippetStore store)
159159
{

TabletBot.Discord/SlashCommands/UserSlashCommands.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ private async Task SetTablet(SocketSlashCommand command)
3838
{
3939
var tablet = command.GetValue<string>("tablet");
4040
var user = command.User as IGuildUser;
41+
42+
if (user is not IGuildUser)
43+
return;
4144

4245
if (tablet != null)
4346
{

0 commit comments

Comments
 (0)