Skip to content

Commit

Permalink
Fixed a part of the code of CustomCommands that was still using files
Browse files Browse the repository at this point in the history
  • Loading branch information
CPULL committed Aug 21, 2021
1 parent f9a87a6 commit cfbb4a8
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 47 deletions.
55 changes: 10 additions & 45 deletions UPBot Code/Commands/CustomCommandsService.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
Expand All @@ -16,7 +15,6 @@
public class CustomCommandsService : BaseCommandModule {
private static List<CustomCommand> Commands = null;
internal static DiscordClient DiscordClient { get; set; }
internal const string DirectoryNameCC = "CustomCommands";

[Command("ccnew")]
[Aliases("createcc", "addcc", "ccadd", "cccreate", "newcc")]
Expand Down Expand Up @@ -66,11 +64,8 @@ public async Task CreateCommand(CommandContext ctx, [Description("A 'list' of al
[RequireRoles(RoleCheckMode.Any, "Mod", "Owner")] // Restrict access to users with the "Mod" or "Owner" role only
public async Task DeleteCommand(CommandContext ctx, [Description("Main name of the CC you want to delete")] string name) {
Utils.LogUserCommand(ctx);
string filePath = Utils.ConstructPath(DirectoryNameCC, name, ".txt");
if (File.Exists(filePath)) {
File.Delete(filePath);
if (TryGetCommand(name, out CustomCommand cmd))
Commands.Remove(cmd);
if (TryGetCommand(name, out CustomCommand cmd)) {
Commands.Remove(cmd);

string embedMessage = $"CC {name} successfully deleted!";
await Utils.BuildEmbedAndExecute("Success", embedMessage, Utils.Green, ctx, true);
Expand All @@ -85,27 +80,12 @@ public async Task DeleteCommand(CommandContext ctx, [Description("Main name of t
[RequireRoles(RoleCheckMode.Any, "Mod", "Owner")] // Restrict access to users with the "Mod" or "Owner" role only
public async Task EditCommand(CommandContext ctx, [Description("Main name of the CC you want to edit")] string name) {
Utils.LogUserCommand(ctx);
string filePath = Utils.ConstructPath(DirectoryNameCC, name, ".txt");
if (File.Exists(filePath)) {
string content = await WaitForContent(ctx, name);
string firstLine;
using (StreamReader sr = File.OpenText(filePath))
firstLine = await sr.ReadLineAsync();
string content = await WaitForContent(ctx, name);
if (TryGetCommand(name, out CustomCommand command))
command.EditCommand(content);


await using (StreamWriter sw = File.CreateText(filePath)) {
await sw.WriteLineAsync(firstLine);
await sw.WriteLineAsync(content);
}

if (TryGetCommand(name, out CustomCommand command))
command.EditCommand(content);

string embedMessage = $"CC **{name}** successfully edited!";
await Utils.BuildEmbedAndExecute("Success", embedMessage, Utils.Green, ctx, false);
}
else
await Utils.ErrorCallback(CommandErrors.MissingCommand, ctx);
string embedMessage = $"CC **{name}** successfully edited!";
await Utils.BuildEmbedAndExecute("Success", embedMessage, Utils.Green, ctx, false);
}

[Command("cceditname")]
Expand All @@ -125,25 +105,10 @@ public async Task EditCommand(CommandContext ctx, [Description("Main name of the
return;
}

string filePath = Utils.ConstructPath(DirectoryNameCC, names[0], ".txt");
if (File.Exists(filePath)) {
if (TryGetCommand(names[0], out CustomCommand command))
command.EditCommand(names.Skip(1).ToArray());

string content = string.Empty;
using (StreamReader sr = File.OpenText(filePath)) {
string c;
await sr.ReadLineAsync();
while ((c = await sr.ReadLineAsync()) != null)
content += c + System.Environment.NewLine;
}
Database.DeleteByKey<CustomCommand>(names[0]);

string newPath = Utils.ConstructPath(DirectoryNameCC, names[1], ".txt");
File.Move(filePath, newPath);
using (StreamWriter sw = File.CreateText(newPath)) {
await sw.WriteLineAsync(string.Join(',', names.Skip(1)));
await sw.WriteLineAsync(content);
}
if (TryGetCommand(names[0], out CustomCommand command)) {
command.EditCommand(names.Skip(1).ToArray());

string embedDescription = "The CC names have been successfully edited.";
await Utils.BuildEmbedAndExecute("Success", embedDescription, Utils.Green, ctx, false);
Expand Down
2 changes: 2 additions & 0 deletions UPBot Code/DataClasses/CustomCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal async Task ExecuteCommand(CommandContext ctx) {

internal void EditCommand(string newContent) {
Content = newContent;
Database.Update(this);
}

internal void EditCommand(string[] newNames) {
Expand All @@ -40,6 +41,7 @@ internal void EditCommand(string[] newNames) {
if (newNames.Length > 2) Alias1 = newNames[2];
if (newNames.Length > 3) Alias2 = newNames[3];
if (newNames.Length >= 4) Alias3 = newNames[4];
Database.Update(this);
}

internal bool Contains(string name) {
Expand Down
10 changes: 10 additions & 0 deletions UPBot Code/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ public static void Delete<T>(T val) {
Utils.Log("Error in Deleting data for " + val.GetType() + ": " + ex.Message);
}
}
public static void DeleteByKey<T>(object key) {
try {
EntityDef ed = entities[typeof(T)];
SQLiteCommand cmd = new SQLiteCommand(ed.delete, connection);
cmd.Parameters.Add(new SQLiteParameter("@param1", key));
cmd.ExecuteNonQuery();
} catch (Exception ex) {
Utils.Log("Error in Deleting data for " + typeof(T) + ": " + ex.Message);
}
}

public static T Get<T>(object keyvalue) {
try {
Expand Down
3 changes: 1 addition & 2 deletions UPBot Code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ public enum EmojiEnum {
AutoRefactored = 12,
}

public enum CommandErrors
{
public enum CommandErrors {
InvalidParams,
InvalidParamsDelete,
CommandExists,
Expand Down

0 comments on commit cfbb4a8

Please sign in to comment.