Skip to content

Commit

Permalink
Version 3.0
Browse files Browse the repository at this point in the history
- Fixed the .help command not retrieving info on subcommands.
- Fixed the news module not being registered.
- Added the latest lavalink.jar file in the build directory.
- Updated the README to include instuctions for running lavalink.
  • Loading branch information
CriticalFlaw committed Nov 22, 2020
1 parent f416fc5 commit fd46701
Show file tree
Hide file tree
Showing 37 changed files with 203 additions and 218 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -357,5 +357,4 @@ MigrationBackup/
# Ionide (cross platform F# VS Code tools) working folder
.ionide/

# End of https://www.toptal.com/developers/gitignore/api/visualstudio
src/FlawBOT/Lavalink.jar
# End of https://www.toptal.com/developers/gitignore/api/visualstudio
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ A Discord bot written in C# using the [DSharpPlus](https://github.com/DSharpPlus
## Installation
To run your own instance of FlawBOT, [clone this repository](https://github.com/CriticalFlaw/FlawBOT/archive/master.zip) or [download the release package](https://github.com/CriticalFlaw/FlawBOT/releases) and modify the provided **config.json** file, adding the API tokens. If you're cloning the repository, use [Visual Studio 2019](https://www.visualstudio.com/downloads/) to compile the project.

## Running FlawBOT
As of version 3.0, FlawBOT requires a [Lavalink](https://github.com/Frederikam/Lavalink#requirements) node to be running in order to play music and not display lavalink-related errors in console. `lavalink.jar` and `application.yml` are included in the project directory, so all you have to do is open the command prompt and launch lavalink with the command `java -jar Lavalink.jar` (be sure to install [Java 13 or later](https://www.oracle.com/java/technologies/javase-jdk13-downloads.html)).

## API Tokens
* [Discord](https://discordapp.com/developers/applications/me) (*required*)
* [Steam](https://steamcommunity.com/dev/apikey)
Expand Down
56 changes: 28 additions & 28 deletions src/FlawBOT/Common/Exceptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,78 +17,78 @@ public static async Task Process(CommandErrorEventArgs e, EventId eventId)
{
switch (e.Exception)
{
case CommandNotFoundException _:
await BotServices.SendEmbedAsync(e.Context, e.Exception.Message, EmbedType.Missing)
case CommandNotFoundException:
await BotServices.SendResponseAsync(e.Context, e.Exception.Message, ResponseType.Missing)
.ConfigureAwait(false);
break;

case InvalidOperationException _:
await BotServices.SendEmbedAsync(e.Context, e.Exception.Message, EmbedType.Warning)
case InvalidOperationException:
await BotServices.SendResponseAsync(e.Context, e.Exception.Message, ResponseType.Warning)
.ConfigureAwait(false);
break;

case ChecksFailedException cfe:
await BotServices.SendEmbedAsync(e.Context,
$"Command **{e.Command.QualifiedName}** could not be executed.", EmbedType.Error)
await BotServices.SendResponseAsync(e.Context,
$"Command {Formatter.Bold(e.Command.QualifiedName)} could not be executed.", ResponseType.Error)
.ConfigureAwait(false);
foreach (var check in cfe.FailedChecks)
switch (check)
{
case RequirePermissionsAttribute perms:
await BotServices.SendEmbedAsync(e.Context,
await BotServices.SendResponseAsync(e.Context,
$"- One of us does not have the required permissions ({perms.Permissions.ToPermissionString()})!",
EmbedType.Error).ConfigureAwait(false);
ResponseType.Error).ConfigureAwait(false);
break;

case RequireUserPermissionsAttribute perms:
await BotServices.SendEmbedAsync(e.Context,
await BotServices.SendResponseAsync(e.Context,
$"- You do not have sufficient permissions ({perms.Permissions.ToPermissionString()})!",
EmbedType.Error).ConfigureAwait(false);
ResponseType.Error).ConfigureAwait(false);
break;

case RequireBotPermissionsAttribute perms:
await BotServices.SendEmbedAsync(e.Context,
await BotServices.SendResponseAsync(e.Context,
$"- I do not have sufficient permissions ({perms.Permissions.ToPermissionString()})!",
EmbedType.Error).ConfigureAwait(false);
ResponseType.Error).ConfigureAwait(false);
break;

case RequireOwnerAttribute _:
await BotServices.SendEmbedAsync(e.Context,
"- This command is reserved only for the bot owner.", EmbedType.Error)
case RequireOwnerAttribute:
await BotServices.SendResponseAsync(e.Context,
"- This command is reserved only for the bot owner.", ResponseType.Error)
.ConfigureAwait(false);
break;

case RequirePrefixesAttribute pa:
await BotServices.SendEmbedAsync(e.Context,
await BotServices.SendResponseAsync(e.Context,
$"- This command can only be invoked with the following prefixes: {string.Join(" ", pa.Prefixes)}.",
EmbedType.Error).ConfigureAwait(false);
ResponseType.Error).ConfigureAwait(false);
break;

default:
await BotServices.SendEmbedAsync(e.Context,
await BotServices.SendResponseAsync(e.Context,
"Unknown check triggered. Please notify the developer using the command *.bot report*",
EmbedType.Error).ConfigureAwait(false);
ResponseType.Error).ConfigureAwait(false);
break;
}

break;

case ArgumentNullException _:
case ArgumentException _:
await BotServices.SendEmbedAsync(e.Context,
case ArgumentNullException:
case ArgumentException:
await BotServices.SendResponseAsync(e.Context,
$"Invalid or missing parameters. For help, use command `.help {e.Command?.QualifiedName}`",
EmbedType.Warning);
ResponseType.Warning);
break;

case UnauthorizedException _:
await BotServices.SendEmbedAsync(e.Context, "One of us does not have the required permissions.", EmbedType.Warning);
case UnauthorizedException:
await BotServices.SendResponseAsync(e.Context, "One of us does not have the required permissions.", ResponseType.Warning);
break;

case NullReferenceException _:
case InvalidDataException _:
case NullReferenceException:
case InvalidDataException:
e.Context.Client.Logger.LogWarning(eventId, e.Exception,
$"[{e.Context.Guild.Name} : {e.Context.Channel.Name}] {e.Context.User.Username} executed the command '{e.Command?.QualifiedName ?? "<unknown>"}' but it threw an error: ");
await BotServices.SendEmbedAsync(e.Context, e.Exception.Message, EmbedType.Error);
await BotServices.SendResponseAsync(e.Context, e.Exception.Message, ResponseType.Error);
break;

default:
Expand Down
45 changes: 24 additions & 21 deletions src/FlawBOT/Common/HelpFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,33 +41,36 @@ public override BaseHelpFormatter WithCommand(Command cmd)
_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))
if (cmd.Aliases?.Any() ?? false)
_output.AddField("Aliases", string.Join(", ", cmd.Aliases.Select(Formatter.InlineCode)), true);

if (!(cmd.Overloads?.Any() ?? false)) return this;
foreach (var overload in cmd.Overloads.OrderByDescending(o => o.Priority))
{
if (overload.Arguments.Count == 0) continue;

var args = new StringBuilder();
foreach (var arg in overload.Arguments)
{
var args = new StringBuilder();
foreach (var arg in overload.Arguments)
args.Append(Formatter.InlineCode($"[{CommandsNext.GetUserFriendlyTypeName(arg.Type)}]"));
args.Append(' ');
args.Append(arg.Description ?? "No description provided.");
if (arg.IsOptional)
{
args.Append(Formatter.InlineCode($"[{CommandsNext.GetUserFriendlyTypeName(arg.Type)}]"));
args.Append(' ');
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(" (optional)");
}

args.AppendLine();
args.Append(" (def: ")
.Append(Formatter.InlineCode(arg.DefaultValue is null
? "None"
: arg.DefaultValue.ToString())).Append(')');
args.Append(" (optional)");
}

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

if (cmd.Aliases?.Any() ?? false)
_output.AddField("Aliases", string.Join(", ", cmd.Aliases.Select(Formatter.InlineCode)), true);
_output.AddField($"{(cmd.Overloads.Count > 1 ? $"Overload #{overload.Priority}" : "Arguments")}",
args.ToString() ?? "No arguments.");
}

return this;
}

Expand Down
2 changes: 1 addition & 1 deletion src/FlawBOT/Common/SharedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class SharedData
public static int ShardCount { get; } = 1;
}

public enum EmbedType
public enum ResponseType
{
Default,
Warning,
Expand Down
17 changes: 9 additions & 8 deletions src/FlawBOT/FlawBOT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ public FlawBot(int shardId = 0)
EnableMentionPrefix = true, // Set the boolean for mentioning the bot as a command prefix
CaseSensitive = false,
IgnoreExtraArguments = true,
EnableDefaultHelp = false,
Services = Services
});
Commands.CommandExecuted += Command_Executed;
Expand All @@ -72,11 +71,12 @@ public FlawBot(int shardId = 0)
Commands.RegisterCommands<ChannelModule>();
Commands.RegisterCommands<DictionaryModule>();
Commands.RegisterCommands<EmojiModule>();
Commands.RegisterCommands<WorldModule>();
Commands.RegisterCommands<ImgurModule>();
Commands.RegisterCommands<MathModule>();
Commands.RegisterCommands<MiscModule>();
Commands.RegisterCommands<MusicModule>();
Commands.RegisterCommands<NasaModule>();
Commands.RegisterCommands<NewsModule>();
Commands.RegisterCommands<OmdbModule>();
Commands.RegisterCommands<PokemonModule>();
Commands.RegisterCommands<PollModule>();
Expand All @@ -90,8 +90,8 @@ public FlawBot(int shardId = 0)
Commands.RegisterCommands<TwitchModule>();
Commands.RegisterCommands<UserModule>();
Commands.RegisterCommands<WikipediaModule>();
Commands.RegisterCommands<WorldModule>();
Commands.RegisterCommands<YouTubeModule>();
Commands.RegisterCommands<MusicModule>();

// Setup Interactivity
Interactivity = Client.UseInteractivity(new InteractivityConfiguration
Expand All @@ -109,9 +109,10 @@ public FlawBot(int shardId = 0)

// Setup Lavalink
Lavalink = Client.UseLavalink();
//Process.Start("java", $"-jar {Directory.GetCurrentDirectory()}\\Lavalink.jar");

// Start the uptime counter
Console.Title = SharedData.Name + "-" + SharedData.Version;
Console.Title = $"{SharedData.Name}-{SharedData.Version}";
SharedData.ProcessStarted = DateTime.Now;
}

Expand All @@ -127,9 +128,9 @@ public async Task RunAsync()
{
// Update any other services that are being used.
Client.Logger.LogInformation(EventId, "Loading...");
//await SteamService.UpdateSteamAppListAsync().ConfigureAwait(false);
//await TeamFortressService.UpdateTf2SchemaAsync().ConfigureAwait(false);
//await PokemonService.UpdatePokemonListAsync().ConfigureAwait(false);
await SteamService.UpdateSteamAppListAsync().ConfigureAwait(false);
await TeamFortressService.UpdateTf2SchemaAsync().ConfigureAwait(false);
await PokemonService.UpdatePokemonListAsync().ConfigureAwait(false);

// Set the initial activity and connect the bot to Discord
var act = new DiscordActivity("Night of Fire", ActivityType.ListeningTo);
Expand Down Expand Up @@ -184,7 +185,7 @@ private async Task Client_VoiceStateUpdated(DiscordClient sender, VoiceStateUpda
if (channel == null || channel != e.Before.Channel) return;

var users = channel.Users;
if (musicData.IsPlaying && !users.Any(x => !x.IsBot))
if (musicData.IsPlaying && users.All(x => x.IsBot))
{
sender.Logger.LogInformation(EventId, $"All users left voice in {e.Guild.Name}, pausing playback...");
await musicData.PauseAsync();
Expand Down
6 changes: 5 additions & 1 deletion src/FlawBOT/FlawBOT.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Prefer32Bit>false</Prefer32Bit>
<PlatformTarget>AnyCPU</PlatformTarget>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<None Remove="application.yml" />
<None Remove="config.json" />
<None Remove="Lavalink.jar" />
</ItemGroup>

<ItemGroup>
Expand All @@ -40,6 +41,9 @@
<Content Include="config.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Lavalink.jar">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>

<ItemGroup>
Expand Down
Binary file added src/FlawBOT/Lavalink.jar
Binary file not shown.
12 changes: 6 additions & 6 deletions src/FlawBOT/Models/WorldData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public class Location
public string LocalTime { get; set; }

[JsonProperty("localtime_epoch", NullValueHandling = NullValueHandling.Ignore)]
public int LocalTime_epoch { get; set; }
public int LocalTimeEpoch { get; set; }

[JsonProperty("utc_offset", NullValueHandling = NullValueHandling.Ignore)]
public string UTC_Offset { get; set; }
public string UtcOffset { get; set; }
}

public class Current
Expand All @@ -121,13 +121,13 @@ public class Current
public List<string> Descriptions { get; set; }

[JsonProperty("wind_speed", NullValueHandling = NullValueHandling.Ignore)]
public int Wind_Speed { get; set; }
public int WindSpeed { get; set; }

[JsonProperty("wind_degree", NullValueHandling = NullValueHandling.Ignore)]
public int Wind_Degree { get; set; }
public int WindDegree { get; set; }

[JsonProperty("wind_dir", NullValueHandling = NullValueHandling.Ignore)]
public string Wind_Direction { get; set; }
public string WindDirection { get; set; }

[JsonProperty("pressure", NullValueHandling = NullValueHandling.Ignore)]
public int Pressure { get; set; }
Expand All @@ -145,7 +145,7 @@ public class Current
public int FeelsLike { get; set; }

[JsonProperty("uv_index", NullValueHandling = NullValueHandling.Ignore)]
public int UV_Index { get; set; }
public int UvIndex { get; set; }

[JsonProperty("visibility", NullValueHandling = NullValueHandling.Ignore)]
public int Visibility { get; set; }
Expand Down
Loading

0 comments on commit fd46701

Please sign in to comment.