I have this JSON file that stores Dokkan Passives for my Discord Bot for people to use and I recently added a Slash command where the user can Delete a passive based on the name they pass through it. If the name matches an entry in the JSON file then it can go ahead and delete the whole member from the JSON file, this is what I'm trying to do.
This is what part of my JSON File looks like
{
"userPassiveStorage": "Storage for user created passives using /passivecreator",
"members": [
{
"membersJSONList": [],
"Error": null,
"UserName": "𝕤𝕒𝕞.𝕛𝕖𝕤𝕦𝕤𝟠",
"PassiveName": "TestTUR",
"Rarity": "TUR",
"UnitHP": 10000,
"UnitATK": 19234,
"UnitDEF": 14235,
"UnitLeaderName": "A TUR Passive",
"UnitLeaderSkill": 200,
"UnitPassiveATK": 250,
"UnitPassiveDEF": 150,
"DmgReduction": 30,
"Support": 30,
"Links": "No active Links"
},
{
"membersJSONList": [],
"Error": null,
"UserName": "𝕤𝕒𝕞.𝕛𝕖𝕤𝕦𝕤𝟠",
"PassiveName": "TEQ Jiren",
"Rarity": "LR",
"UnitHP": 20863,
"UnitATK": 21525,
"UnitDEF": 14338,
"UnitLeaderName": "Universe 11",
"UnitLeaderSkill": 170,
"UnitPassiveATK": 200,
"UnitPassiveDEF": 200,
"DmgReduction": 30,
"Support": 0,
"Links": "No active Links"
}
]
}
I will explain what I'm trying to get this command to do
- User passes in the name of the passive they are trying to delete (This is the "PassiveName" property in the JSON file)
- Load the JSON and search for the passive based on that name
- If there is no entry that matches their search query then stop the command
- If there is a match, load that specific passive
- Delete it from the JSON file
This is my current method within a class where I can Load passives & hopefully Delete passives at the same time, it takes in a parameter object which loads in the already searched passive if Step 4 was successful
public bool DeleteSpecificPassive(DokkanUserPassiveBuilder classObj)
{
try
{
//var path = @"C:\Users\samue\Documents\Bot\bin\Debug\UserPassivesStorage.json";
var path = @"D:\Visual Studio Projects\DiscordBotTest\bin\Debug\UserPassivesStorage.json";
var json = File.ReadAllText(path);
var jsonObj = JObject.Parse(json);
var members = jsonObj["members"].ToObject<List<DokkanUserPassiveBuilder>>();
if (members.Contains(classObj))
{
members.Remove(classObj);
jsonObj["members"] = JArray.FromObject(members);
File.WriteAllText(path, jsonObj.ToString());
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
Error = ex.ToString();
return false;
}
}
For some reason, when I type in a correct passive name like "TEQ Jiren", even though it exists in the list, the if statement comparing the user input with the JSON file doesn't work and goes to the else block instead. So how can I get it so that the user input and the member in the class are compared and if it exists in the List then delete it and rewrite the JSON file?
If you want to view the full class for the bigger picture the Discord Bot is in my GitHub repo where I carry out these commands ->
[DokkanUserPassivesBuilder.cs] (https://github.com/samjesus8/CoolerDiscordBot/blob/master/Builders/DokkanUserPassiveBuilder.cs)