Skip to content

Commit

Permalink
Merge pull request #12 from CriticalFlaw/dev
Browse files Browse the repository at this point in the history
ReSharper Code Refactors
  • Loading branch information
CriticalFlaw authored Jul 27, 2020
2 parents c3b67cd + f49286e commit c400be3
Show file tree
Hide file tree
Showing 70 changed files with 1,146 additions and 768 deletions.
51 changes: 28 additions & 23 deletions src/FlawBOT.Core/Common/HelpFormatter.cs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
using DSharpPlus;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Converters;
using DSharpPlus.CommandsNext.Entities;
using DSharpPlus.Entities;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FlawBOT.Common
{
public sealed class HelpFormatter : BaseHelpFormatter
{
private string name;
private string description;
private readonly DiscordEmbedBuilder output;
private readonly DiscordEmbedBuilder _output;
private string _description;
private string _name;

public HelpFormatter(CommandContext ctx) : base(ctx)
{
output = new DiscordEmbedBuilder()
_output = new DiscordEmbedBuilder()
.WithColor(DiscordColor.Turquoise)
.WithUrl(SharedData.GitHubLink + "wiki");
}

public override CommandHelpMessage Build()
{
string desc = $"Listing all commands and groups. Use {Formatter.InlineCode(".help <command>")} for more details.";
if (!string.IsNullOrWhiteSpace(name))
var desc = $"Listing all commands and groups. Use {Formatter.InlineCode(".help <command>")} for more details.";
if (!string.IsNullOrWhiteSpace(_name))
{
output.WithTitle(name);
desc = description ?? "No description provided.";
_output.WithTitle(_name);
desc = _description ?? "No description provided.";
}
output.WithDescription(desc);
return new CommandHelpMessage(embed: output);

_output.WithDescription(desc);
return new CommandHelpMessage(embed: _output);
}

public override BaseHelpFormatter WithCommand(Command cmd)
{
name = ((cmd is CommandGroup) ? "Group: " : "Command: ") + cmd.QualifiedName;
description = cmd.Description;
_name = (cmd is CommandGroup ? "Group: " : "Command: ") + cmd.QualifiedName;
_description = cmd.Description;

if (cmd.Overloads?.Any() ?? false)
{
foreach (var overload in cmd.Overloads.OrderByDescending(o => o.Priority))
{
var args = new StringBuilder();
Expand All @@ -51,24 +51,29 @@ public override BaseHelpFormatter WithCommand(Command cmd)
args.Append(arg.Description ?? "No description provided.");
if (arg.IsOptional)
{
args.Append(" (def: ").Append(Formatter.InlineCode(arg.DefaultValue is null ? "None" : arg.DefaultValue.ToString())).Append(")");
args.Append(" (def: ")
.Append(Formatter.InlineCode(arg.DefaultValue is null
? "None"
: arg.DefaultValue.ToString())).Append(")");
args.Append(" (optional)");
}

args.AppendLine();
}
output.AddField($"{(cmd.Overloads.Count > 1 ? $"Overload #{overload.Priority}" : "Arguments")}", args.ToString() ?? "No arguments.");

_output.AddField($"{(cmd.Overloads.Count > 1 ? $"Overload #{overload.Priority}" : "Arguments")}", args.ToString() ?? "No arguments.");
}
}

if (cmd.Aliases?.Any() ?? false)
output.AddField("Aliases", string.Join(", ", cmd.Aliases.Select(a => Formatter.InlineCode(a))), inline: true);
_output.AddField("Aliases", string.Join(", ", cmd.Aliases.Select(Formatter.InlineCode)), true);
return this;
}

public override BaseHelpFormatter WithSubcommands(IEnumerable<Command> subcommands)
{
if (subcommands.Any())
output.AddField((name is null) ? "Commands" : "Subcommands", string.Join(", ", subcommands.Select(c => Formatter.InlineCode(c.Name))));
var enumerable = subcommands.ToList();
if (enumerable.Any())
_output.AddField(_name is null ? "Commands" : "Subcommands", string.Join(", ", enumerable.Select(c => Formatter.InlineCode(c.Name))));
return this;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/FlawBOT.Core/Common/SharedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace FlawBOT.Common
public class SharedData
{
public static string Name { get; } = "FlawBOT";
public static string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version.ToString();
public static string Version { get; } = Assembly.GetExecutingAssembly().GetName().Version?.ToString();
public static string GitHubLink { get; set; } = "https://github.com/CriticalFlaw/FlawBOT/";
public static string InviteLink { get; } = "https://discordapp.com/oauth2/authorize?client_id=339833029013012483&scope=bot&permissions=66186303";
public static DiscordColor DefaultColor { get; set; } = new DiscordColor("#00FF7F");
Expand Down
14 changes: 7 additions & 7 deletions src/FlawBOT.Core/FlawBOT.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
<ApplicationIcon>icon.ico</ApplicationIcon>
<StartupObject>FlawBOT.Program</StartupObject>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>2.6.0</Version>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>2.6.1</Version>
<Win32Resource />
<LangVersion>7.2</LangVersion>
<AssemblyVersion>2.6.0.0</AssemblyVersion>
<FileVersion>2.6.0.0</FileVersion>
<AssemblyVersion>2.6.1.0</AssemblyVersion>
<FileVersion>2.6.1.0</FileVersion>
<AssemblyName>FlawBOT.Core</AssemblyName>
<RootNamespace>FlawBOT.Core</RootNamespace>
<PackageTags />
Expand Down Expand Up @@ -40,9 +40,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="DSharpPlus" Version="4.0.0-nightly-00697" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-nightly-00697" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.0.0-nightly-00697" />
<PackageReference Include="DSharpPlus" Version="4.0.0-nightly-00709" />
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.0.0-nightly-00709" />
<PackageReference Include="DSharpPlus.Interactivity" Version="4.0.0-nightly-00709" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 12 additions & 11 deletions src/FlawBOT.Core/Modules/Games/PokemonModule.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using DSharpPlus.CommandsNext;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using FlawBOT.Core.Properties;
using FlawBOT.Framework.Models;
using FlawBOT.Framework.Services;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlawBOT.Modules
{
Expand All @@ -23,20 +23,22 @@ public async Task Pokemon(CommandContext ctx,
{
var results = await PokemonService.GetPokemonCardsAsync(query).ConfigureAwait(false);
if (results.Cards.Count == 0)
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing).ConfigureAwait(false);
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing)
.ConfigureAwait(false);
else
{
foreach (var dex in results.Cards)
{
var card = PokemonService.GetExactPokemon(dex.ID);
var card = PokemonService.GetExactPokemon(dex.Id);
var output = new DiscordEmbedBuilder()
.WithTitle(card.Name + $" (#{card.NationalPokedexNumber})")
.AddField("Series", card.Series ?? "Unknown", true)
.AddField("Rarity", card.Rarity ?? "Unknown", true)
.AddField("HP", card.Hp ?? "Unknown", true)
.AddField("Ability", (card.Ability != null) ? card.Ability.Name : "Unknown", true)
.AddField("Ability", card.Ability != null ? card.Ability.Name : "Unknown", true)
.WithImageUrl(card.ImageUrlHiRes ?? card.ImageUrl)
.WithFooter(!card.Equals(results.Cards.Last()) ? "Type 'next' within 10 seconds for the next Pokémon" : "This is the last found Pokémon on the list.")
.WithFooter(!string.Equals(card.Id, results.Cards.Last().Id)
? "Type 'next' within 10 seconds for the next Pokémon"
: "This is the last found Pokémon on the list.")
.WithColor(DiscordColor.Gold);

var types = new StringBuilder();
Expand All @@ -53,10 +55,9 @@ public async Task Pokemon(CommandContext ctx,
if (results.Cards.Count == 1) continue;
var interactivity = await BotServices.GetUserInteractivity(ctx, "next", 10).ConfigureAwait(false);
if (interactivity.Result is null) break;
if (!card.Equals(results.Cards.Last()))
if (!string.Equals(card.Id, results.Cards.Last().Id))
await BotServices.RemoveMessage(interactivity.Result).ConfigureAwait(false);
}
}
}

#endregion COMMAND_POKEMON
Expand Down
21 changes: 9 additions & 12 deletions src/FlawBOT.Core/Modules/Games/SpeedrunModule.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using DSharpPlus.CommandsNext;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using FlawBOT.Core.Properties;
using FlawBOT.Framework.Models;
using FlawBOT.Framework.Services;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlawBOT.Modules
{
Expand All @@ -24,9 +24,9 @@ public async Task Speedrun(CommandContext ctx,
if (!BotServices.CheckUserInput(query)) return;
var results = SpeedrunService.GetSpeedrunGameAsync(query).Result.Data;
if (results is null || results.Count == 0)
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing).ConfigureAwait(false);
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing)
.ConfigureAwait(false);
else
{
foreach (var game in results)
{
var output = new DiscordEmbedBuilder()
Expand All @@ -35,19 +35,17 @@ public async Task Speedrun(CommandContext ctx,
.AddField("Developers", SpeedrunService.GetSpeedrunExtraAsync(game.Developers, SpeedrunExtras.Developers).Result ?? "Unknown", true)
.AddField("Publishers", SpeedrunService.GetSpeedrunExtraAsync(game.Publishers, SpeedrunExtras.Publishers).Result ?? "Unknown", true)
.AddField("Platforms", SpeedrunService.GetSpeedrunExtraAsync(game.Platforms, SpeedrunExtras.Platforms).Result ?? "Unknown")
.WithFooter($"ID: {game.ID} - Abbreviation: {game.Abbreviation}")
.WithThumbnailUrl(game.Assets.CoverLarge.URL ?? game.Assets.Icon.URL)
.WithFooter($"ID: {game.Id} - Abbreviation: {game.Abbreviation}")
.WithThumbnail(game.Assets.CoverLarge.Url ?? game.Assets.Icon.Url)
.WithUrl(game.WebLink)
.WithColor(new DiscordColor("#0F7A4D"));

var link = game.Links.Where(x => x.REL == "categories").First().URL;
var link = game.Links.First(x => x.Rel == "categories").Url;
var categories = SpeedrunService.GetSpeedrunCategoryAsync(link).Result.Data;
var category = new StringBuilder();
if (categories != null || categories.Count > 0)
{
foreach (var x in categories)
category.Append($"[{x.Name}]({x.Weblink}) **|** ");
}
output.AddField("Categories", category.ToString() ?? "Unknown", true);
await ctx.RespondAsync(embed: output.Build()).ConfigureAwait(false);

Expand All @@ -58,7 +56,6 @@ public async Task Speedrun(CommandContext ctx,
if (!game.Equals(results.Last()))
await BotServices.RemoveMessage(interactivity.Result).ConfigureAwait(false);
}
}
}

#endregion COMMAND_SPEEDRUN
Expand Down
66 changes: 41 additions & 25 deletions src/FlawBOT.Core/Modules/Games/SteamModule.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
using DSharpPlus.CommandsNext;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using FlawBOT.Core.Properties;
using FlawBOT.Framework.Models;
using FlawBOT.Framework.Services;
using Steam.Models.SteamCommunity;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using UserStatus = Steam.Models.SteamCommunity.UserStatus;

namespace FlawBOT.Modules
Expand All @@ -30,21 +30,28 @@ public async Task SteamGame(CommandContext ctx,
{
var app = SteamService.GetSteamAppAsync(query).Result;
if (app is null)
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing).ConfigureAwait(false);
{
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing)
.ConfigureAwait(false);
}
else
{
var output = new DiscordEmbedBuilder()
.WithTitle(app.Name)
.WithDescription((Regex.Replace(app.DetailedDescription.Length <= 500 ? app.DetailedDescription : app.DetailedDescription.Substring(0, 250) + "...", "<[^>]*>", "")) ?? "Unknown")
.AddField("Release Date", app.ReleaseDate.Date ?? "Unknown", true)
.AddField("Developers", app.Developers[0] ?? "Unknown", true)
.AddField("Publisher", app.Publishers[0] ?? "Unknown", true)
.AddField("Price", app.IsFree ? "Free" : (app.PriceOverview.FinalFormatted ?? "Unknown"), true)
.AddField("Metacritic", (app.Metacritic != null) ? app.Metacritic.Score.ToString() : "Unknown", true)
.WithThumbnailUrl(app.HeaderImage)
.WithUrl("http://store.steampowered.com/app/" + app.SteamAppId.ToString())
.WithFooter("App ID: " + app.SteamAppId.ToString())
.WithColor(new DiscordColor("#1B2838"));
.WithTitle(app.Name)
.WithDescription(
Regex.Replace(
app.DetailedDescription.Length <= 500
? app.DetailedDescription
: app.DetailedDescription.Substring(0, 250) + "...", "<[^>]*>", "") ?? "Unknown")
.AddField("Release Date", app.ReleaseDate.Date ?? "Unknown", true)
.AddField("Developers", app.Developers[0] ?? "Unknown", true)
.AddField("Publisher", app.Publishers[0] ?? "Unknown", true)
.AddField("Price", app.IsFree ? "Free" : app.PriceOverview.FinalFormatted ?? "Unknown", true)
.AddField("Metacritic", app.Metacritic != null ? app.Metacritic.Score.ToString() : "Unknown", true)
.WithThumbnail(app.HeaderImage)
.WithUrl("http://store.steampowered.com/app/" + app.SteamAppId)
.WithFooter("App ID: " + app.SteamAppId)
.WithColor(new DiscordColor("#1B2838"));

var genres = new StringBuilder();
foreach (var genre in app.Genres.Take(3))
Expand Down Expand Up @@ -74,18 +81,24 @@ public async Task SteamUser(CommandContext ctx,
var profile = SteamService.GetSteamProfileAsync(query).Result;
var summary = SteamService.GetSteamSummaryAsync(query).Result;
if (profile is null && summary is null)
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing).ConfigureAwait(false);
{
await BotServices.SendEmbedAsync(ctx, Resources.NOT_FOUND_GENERIC, EmbedType.Missing)
.ConfigureAwait(false);
}
else
{
if (summary.Data.ProfileVisibility != ProfileVisibility.Public)
await BotServices.SendEmbedAsync(ctx, "This profile is private...", EmbedType.Warning).ConfigureAwait(false);
{
await BotServices.SendEmbedAsync(ctx, "This profile is private...", EmbedType.Warning)
.ConfigureAwait(false);
}
else
{
var output = new DiscordEmbedBuilder()
.WithTitle(summary.Data.Nickname)
.WithDescription(Regex.Replace(profile.Summary, "<[^>]*>", "") ?? string.Empty)
.WithDescription(Regex.Replace(profile?.Summary ?? string.Empty, "<[^>]*>", "") ?? string.Empty)
.AddField("Member since", summary.Data.AccountCreatedDate.ToUniversalTime().ToString(CultureInfo.CurrentCulture), true)
.WithThumbnailUrl(profile.AvatarFull.ToString() ?? profile.Avatar.ToString())
.WithThumbnail(profile?.AvatarFull.ToString() ?? profile.Avatar.ToString())
.WithColor(new DiscordColor("#1B2838"))
.WithUrl("http://steamcommunity.com/profiles/" + profile.SteamID)
.WithFooter("Steam ID: " + profile.SteamID);
Expand All @@ -104,6 +117,7 @@ public async Task SteamUser(CommandContext ctx,
output.AddField("Game Server IP", profile.InGameServerIP, true);
output.WithImageUrl(profile.InGameInfo.GameLogoSmall);
}

await ctx.RespondAsync(embed: output.Build()).ConfigureAwait(false);
}
}
Expand All @@ -115,15 +129,17 @@ public async Task SteamUser(CommandContext ctx,

[Command("connect")]
[Aliases("link")]
[Description("Format a game connection string into a clickable link")]
[Description("Format a game connection string into a link")]
public async Task SteamServerLink(CommandContext ctx,
[Description("Connection string")] [RemainingText] string link)
{
var regex = new Regex(@"\s*(?'ip'\S+)\s*", RegexOptions.Compiled).Match(link);
if (regex.Success)
await ctx.RespondAsync(string.Format($"steam://connect/{regex.Groups["ip"].Value}/{regex.Groups["pw"].Value}")).ConfigureAwait(false);
await ctx.RespondAsync(string.Format($"steam://connect/{regex.Groups["ip"].Value}/{regex.Groups["pw"].Value}"))
.ConfigureAwait(false);
else
await BotServices.SendEmbedAsync(ctx, Resources.ERR_STEAM_CONNECT_FORMAT, EmbedType.Warning).ConfigureAwait(false);
await BotServices.SendEmbedAsync(ctx, Resources.ERR_STEAM_CONNECT_FORMAT, EmbedType.Warning)
.ConfigureAwait(false);
}

#endregion COMMAND_CONNECT
Expand Down
Loading

0 comments on commit c400be3

Please sign in to comment.