-1

I have this code which reads from my json file an array of words

public static string[] GetProfanity()
        {
            var json = string.Empty;
            using (var fs = File.OpenRead("profanity.json"))
            using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
                json = sr.ReadToEnd();

            var profanityJson = JsonConvert.DeserializeObject<ProfanityJson>(json);

            return profanityJson.badwords;
        }

This is the json

{
  "badwords" : ["bad", "stupid"]
}

And i try to access this here

public static bool ProfanityCheck(string inputString)
        {
            string[] badWords = GetProfanity();
            string checkString = inputString.ToLower();

            if (badWords.Any(checkString.Contains))
                return true;

            return false;
        }

As requested I access the ProfanityCheck method here

[Command("echo")]
        [Description("says whatever the user gives")]
        public async Task Echo(CommandContext ctx, [RemainingText] string echoText)
        {
           bool hasProfanity = ProfanityFilter.ProfanityCheck(echoText);

           if(hasProfanity)
           {
                var errMsg = ProfanityFilter.ErrorMessage();
                var errSent = await ctx.Channel.SendMessageAsync(embed: errMsg).ConfigureAwait(false);
                Thread.Sleep(3000);
                await ctx.Channel.DeleteMessageAsync(errSent).ConfigureAwait(false);
                await ctx.Channel.DeleteMessageAsync(ctx.Message).ConfigureAwait(false);
                return;
           }
            
            await ctx.Channel.SendMessageAsync(echoText).ConfigureAwait(false);
        }

and the struct I Deserialize it as

public struct ProfanityJson
    {
        [JsonProperty("badwords")]
        public string[] badwords { get; private set; }
    }

but when i attempt to search for this any bad words in a string I pass, nothing happens, no errors in the console, no output otherwise. I have it set up so that it sends me an error message when profanity is found, but in its current state it does nothing when profanity is passed

  • Where is ProfanityJson class? You could return it Deserializing an anonymous object.Likely your class definition is wrong. – Cetin Basoz Jan 30 '21 at 11:47
  • did you debug this and checked the value of your array `badWords`? Seems it doesn´t contain what you expect. – MakePeaceGreatAgain Jan 30 '21 at 11:52
  • As a sidenote you don't really need to read the whole file to a string before deserializing it. https://stackoverflow.com/a/17788118/613130. – xanatos Jan 30 '21 at 11:58
  • 2
    As it stands, there's no code that calls `ProfanityCheck` so I wouldn't expect any output/errors. Please [edit] your question and include a [mre] – Camilo Terevinto Jan 30 '21 at 11:59
  • To all of those who tried to help me, a big thank you, but looking at error outputs, i forgot to add the json to the debug folder (forgive me for wasting your time) – Aarav Navani Jan 30 '21 at 12:52

2 Answers2

0

Your code seems to be correct... I would write the GetProfanity() in another way (and I wouldn't surely reread it every time a word is passed to to ProfanityCheck) but this is tangential to your problem. I've written a minimum testable example:

public class ProfanityJson
{
    public string[] badwords { get; set; }
}

public static class ProfanityChecker
{
    public static string[] GetProfanity()
    {
        var json = string.Empty;
        using (var fs = File.OpenRead("profanity.json"))
        using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
            json = sr.ReadToEnd();

        var profanityJson = JsonConvert.DeserializeObject<ProfanityJson>(json);

        return profanityJson.badwords;
    }

    public static string[] GetProfanity2()
    {
        using (var sr = new StreamReader("profanity.json"))
        using (var jtr = new JsonTextReader(sr))
        {
            var ser = new JsonSerializer();
            var profanityJson = ser.Deserialize<ProfanityJson>(jtr);
            return profanityJson.badwords;
        }
    }

    public static bool ProfanityCheck(string inputString)
    {
        string[] badWords = GetProfanity2();
        Trace.WriteLine($"Loaded {badWords.Length} bad words");

        string checkString = inputString.ToLower();

        if (badWords.Any(checkString.Contains))
            return true;

        return false;
    }
}

static void Main(string[] args)
{
    Console.WriteLine(ProfanityChecker.ProfanityCheck("badder"));
}

So the only idea I have is that you are using a "stale" version of profanity.json. I've added a little loggin in the ProfanityCheck() method. It will go to the Output pane in Visual Studio.

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • My ProfanityCheck is inside a static class (which I should mention) so how should i go about checking the value of something (as you have done in your main)? – Aarav Navani Jan 30 '21 at 12:40
  • @AaravNavani Added a static class around `ProfanityCheck`. Don't see big difference in the code. – xanatos Jan 30 '21 at 12:43
-1

(Would be a mess as a comment) You could have your class like this:

public class ProfanityJson
{
    [JsonProperty("badwords")]
    public string[] Badwords { get; set; }
}

Is it like so? Json is case sensitive.

Cetin Basoz
  • 22,495
  • 3
  • 31
  • 39