summaryrefslogtreecommitdiff
path: root/DiscoBot/Commands/FileHandler.cs
blob: 0e8af603a39bc60df3cab60c7faabf8fa65f4c82 (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
namespace DiscoBot.Commands
{
    using System;
    using System.Linq;
    using System.Net;

    using DiscoBot.Auxiliary;
    using DiscoBot.Characters;
    using DiscoBot.DSA_Game;
    using DiscoBot.DSA_Game.Characters;

    using Discord.Commands;

    public class FileHandler : ModuleBase
    {
        [Command("send"), Summary("fügt Helden hinzu")]
        public void AddChar()
        {
            var msg = this.Context.Message;
            if (msg.Attachments == null)
            {
                throw new ArgumentException("Es wurde keine Datei angehängt");
            }

            var attachments = msg.Attachments.ToList();

            if (!attachments.Any(x => x.Filename.EndsWith(".xml")))
            {
                throw new ArgumentException("Es wurde kein xml Held mitgeschickt");
            }

            foreach (var attachment in attachments.Where(x => x.Filename.EndsWith(".xml")))
            {
                using (var client = new WebClient())
                {
                    client.DownloadFile(attachment.Url, "helden\\" + attachment.Filename);
                }

                Dsa.Chars.Add(new Character("helden\\" + attachment.Filename));
                (Dsa.Chars.Last() as Character)?.Talente.Select(x => new Talent(x.Name, x.Probe, 0))
                    .Where(c => !Dsa.Talente.Exists(v => v.Name.Equals(c.Name))).ToList().ForEach(v => Dsa.Talente.Add(v));
            }
        }
    }
}