summaryrefslogtreecommitdiff
path: root/dsa/DiscoBot/Commands/CommandExtension.cs
blob: 098e35f8236d437e9e675f6d8c73233cdefc2fc9 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
namespace DiscoBot.Auxiliary
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;

    using Discord;
    using Discord.Commands;

    public static class CommandExtension
    {
        private static WebClient client;

        public static async Task ReplyTimedAsync(this ModuleBase m, string message, TimeSpan time)
        {
            var token = message.GetHashCode();
            var send = m.Context.Channel.SendMessageAsync($"#{token}\n```xl\n{message}```", false);

            var barInvoker = new BackgroundWorker();
            barInvoker.DoWork += delegate
                {
                    Thread.Sleep(time);
                    Delete(token, m);
                };

            await send;
            barInvoker.RunWorkerAsync();
        }

        private static void Delete(int token, ModuleBase m)
        {
            var messagesAsync = m.Context.Channel.GetMessagesAsync();
            Task.WaitAll(messagesAsync.ToArray());
            var list = messagesAsync.ToEnumerable().ToList();
            var messages = new List<IMessage>();
            foreach (var task in list)
            {
                messages.AddRange(task.ToList());
            }
            
            var test = messages.Where(x => x.Content.StartsWith($"#{token}\n") && x.Author.IsBot).Select(c=>c );
            var waiters = new List<Task>();
            foreach (var message in test)
            {
                waiters.Add((message as IUserMessage).DeleteAsync());
            }
            Task.WaitAll(waiters.ToArray());
        }

        public static async Task ReplyAsync(this ModuleBase m, IEnumerable<string> message, bool directMessage = false)
        {
            var sb = new StringBuilder();
            foreach (string re in message)
            {
                if (sb.Length + re.Length > 1798)
                {
                    if (directMessage)
                    {
                        await m.Context.User.SendMessageAsync("```xl\n" + sb + "\n```");
                    }
                    else
                    {
                        await m.Context.Channel.SendMessageAsync("```xl\n" + sb + "\n```");
                    }

                    sb.Clear();
                }

                sb.AppendLine(re);
            }

            if (directMessage)
            {
                await m.Context.User.SendMessageAsync("```xl\n" + sb + "\n```");
            }
            else
            {
                await m.Context.Channel.SendMessageAsync("```xl\n" + sb + "\n```");
            }
        }

        public static async Task ReplyAsync(this ModuleBase m, IEnumerable<string> message, TimeSpan time)
        {
            var sb = new StringBuilder();
            foreach (string re in message)
            {
                if (sb.Length + re.Length > 1798)
                {

                    await m.ReplyTimedAsync(sb.ToString(), time);


                    sb.Clear();
                }

                sb.AppendLine(re);
            }

            await m.ReplyTimedAsync(sb.ToString(), TimeSpan.FromSeconds(90));
        }

        public static async Task SendWebFile(this IMessageChannel channel, string url = "https://i.imgur.com/0iHEycJ.png")
        {
            if (client == null)
            {
                client = new WebClient();
            }

            Stream stream = client.OpenRead(url);
            await channel.SendFileAsync(stream, url.Split('/').Last());
        }
    }
}