-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscordBot.cs
136 lines (121 loc) · 5.44 KB
/
DiscordBot.cs
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.IO;
using System.Threading.Tasks;
using DSharpPlus;
using Microsoft.Extensions.Configuration;
using System.Net;
using System.Net.Http;
using DSharpPlus.Entities;
namespace SimpleDiscordBot
{
public class DiscordBot
{
#region FirstWorkingMethod
public static DiscordClient discord;
private static HttpClient httpClient;
private static bool isBotDUploaded;
///* We'll load the app config into this when we create it a little later. */
private static IConfigurationRoot _config;
#pragma warning disable IDE0060 // Remove unused parameter
public static async Task MainAsync(string[] args)
#pragma warning restore IDE0060 // Remove unused parameter
{
isBotDUploaded = false;
try
{
httpClient = LocalHttpClient();
DiscordEmbedBuilder discordEmbed = new DiscordEmbedBuilder();
//Load the config file(we'll create this shortly)
Console.WriteLine("[info] Loading config file..");
_config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", optional: false, reloadOnChange: true)
.Build();
Console.WriteLine("[info] Done. The Bot is working!");
discord = new DiscordClient(new DiscordConfiguration
{
Token = _config.GetValue<string>("discord:token"),
TokenType = TokenType.Bot
});
discord.MessageCreated += async e =>
{
isBotDUploaded = false;
if (e.Message.Content.ToLower().StartsWith("hi"))
{
await e.Message.RespondAsync("Good day! Can I help you?");
}
if (e.Message.Content.ToLower().StartsWith("get image"))
{
await UploadFile(e);
await WaitToCheckNewMessage(5000);
}
if (e.Message.Attachments.Count > 0 && !isBotDUploaded)
{
await DownloadFileAsync(e);
}
};
await discord.ConnectAsync();
await Task.Delay(-1);
}
catch (Exception ex)
{
// This will catch any exceptions that occur during the operation/setup of your bot.
// Feel free to replace this with what ever logging solution you'd like to use.
// I may do a guide later on the basic logger I implemented in my most recent bot.
Console.Error.WriteLine(ex.ToString());
}
}
public static HttpClient LocalHttpClient()
{
//// https://hidemyna.me/ru/proxy-list/?maxtime=250#list
// Содержит параметры HTTP-прокси для System.Net.WebRequest класса.
var proxy = new WebProxy()
{
Address = new Uri($"http://77.87.240.74:3128"),
UseDefaultCredentials = false,
};
// Создает экземпляр класса System.Net.Http.HttpClientHandler.
var httpClientHandler = new HttpClientHandler() { Proxy = proxy };
// Предоставляет базовый класс для отправки HTTP-запросов и получения HTTP-ответов
// от ресурса с заданным URI.
HttpClient hc = new HttpClient(httpClientHandler);
return hc;
}
public static async Task DownloadFileAsync(DSharpPlus.EventArgs.MessageCreateEventArgs e)
{
if (!isBotDUploaded)
{
var fileUrl = e.Message.Attachments[0].Url;
var fileName = e.Message.Attachments[0].FileName;
var endDirectory = Directory.GetCurrentDirectory() + @"\Files\";
using (httpClient = new HttpClient())
{
using HttpResponseMessage response = await httpClient.GetAsync(fileUrl);
using Stream streamToReadFrom = await response.Content.ReadAsStreamAsync();
using FileStream DestinationStream = File.Create(endDirectory + fileName.ToString());
await streamToReadFrom.CopyToAsync(DestinationStream);
}
await e.Message.RespondAsync("Successfully Downloaded!");
Console.WriteLine("[info] Downloaded");
}
else
{
Console.WriteLine("[info] It's mine");
}
}
public static async Task UploadFile(DSharpPlus.EventArgs.MessageCreateEventArgs e)
{
await e.Message.RespondAsync("Second..");
var filePath = Directory.GetCurrentDirectory() + @"\made.png";
await e.Channel.SendFileAsync(filePath, "Complited");
await e.Message.RespondAsync("Good luck!");
isBotDUploaded = true;
}
public static async Task WaitToCheckNewMessage(int delay)
{
isBotDUploaded = false;
await Task.Delay(delay);
}
#endregion
}
}