summaryrefslogtreecommitdiff
path: root/DiscoBot/Commands/Help.cs
blob: 7d35875e649506b92c2e3678857ebe70bafa1b4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System.Linq;

namespace DiscoBot.Commands
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading.Tasks;

    using DiscoBot.Auxiliary;

    using Discord.Commands;

    using Newtonsoft.Json;

    using CommandInfo = DiscoBot.Auxiliary.CommandInfo;

    public class Help : ModuleBase
    {
        static Help()
        {
            TextReader stream = new StreamReader(@"..\..\Help.json"); // Load command-description file
            var reader = new JsonTextReader(stream); // create stream reader

            reader.Read(); // step into structure, until the array starts
            reader.Read();
            reader.Read();
            
            try
            {
                var test = new JsonSerializer().Deserialize<List<CommandInfo>>(reader); // Deserialize Data and create CommandInfo Struct
                
                Commands.AddRange(test); // Add new CommandInfos to List
            }
            catch (Exception e)
            {
                // ignored
            }
        }

        public static List<CommandInfo> Commands { get; } = new List<CommandInfo>();


        public static string Get_Specific_Help(string command)
        {
            // return command specific help
            var com = Commands.OrderBy(x => SpellCorrect.CompareEasy(x.Name, command.ToLower())).First(); // get best fit command
            return com.GetDescription();
        }

        [Command("help"), Summary("prints the help menu.")]
        [Alias("Help", "man", "Man")]
        public async Task ShowHelpAsync(params string[] command_list)
        {
            var command = "";
            if (command_list.Length > 0) {
                command = command_list.Aggregate((s, c) => s + " " + c);
            }

            if (command.Equals(string.Empty)) // return generic Help
            {
                string res = "";

                foreach (var com in Commands)
                {
                    res += "!" + com.Name + ": " + com.Brief;

                    if (com.Description.Length > 1)
                    {
                        res += "\n\t(\"!man " + com.Name + "\" gibt genauere Informationen)";
                    }

                    res += "\n\n";
                }
                //await this.ReplyAsync("```\n[hilfreiche Erklärungen]\nAuflistung aller Commands mit !list commands\n```");
                await this.ReplyAsync("```xl\n" + res +"\n```");
                return;
            }



            // return command specific help
            //var com = Commands.OrderBy(x => SpellCorrect.CompareEasy(x.Name, command.ToLower())).First(); // get best fit command

            //await this.ReplyAsync("```xl\n" + com.GetDescription() + "\n```");
            await this.ReplyAsync("```xl\n" + Get_Specific_Help(command) + "\n```");
        }
    }
}