-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cs
More file actions
178 lines (152 loc) · 6.75 KB
/
Main.cs
File metadata and controls
178 lines (152 loc) · 6.75 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DiscordPing
{
public partial class Main : Form
{
private bool sendingEnabled = false;
private int sendDelay = 1000; // default to 1 second delay
public Main()
{
InitializeComponent();
this.delaySlider.ValueChanged += new System.EventHandler(this.delaySlider_ValueChanged);
this.webhookUrlTextBox.TextChanged += new System.EventHandler(this.webhookUrlTextBox_TextChanged);
}
private void Form1_Load(object sender, EventArgs e)
{
guna2Panel1.BorderRadius = 20;
sendButton.BorderRadius = 6;
}
private void sendButton_Click(object sender, EventArgs e)
{
// Get the message and webhook URL from their respective text boxes
string message = messageTextBox.Text.Trim();
string webhookUrl = webhookUrlTextBox.Text.Trim();
// Check if the webhook URL is empty or false
if (string.IsNullOrEmpty(webhookUrl) || webhookUrl.ToLower() == "false")
{
// If the webhook URL is empty or false, log an error message and return
AddLogMessage("Webhook URL is missing or invalid.");
return;
}
// Create a new HttpClient object
HttpClient client = new HttpClient();
// Create a new StringContent object with the message data
StringContent content = new StringContent("{\"content\":\"" + message + "\"}", Encoding.UTF8, "application/json");
content.Headers.ContentType.CharSet = "utf-8";
try
{
// Post the message data to the webhook URL
HttpResponseMessage response = client.PostAsync(webhookUrl, content).Result;
// Check the response status code to see if the message was successfully sent
if (response.IsSuccessStatusCode)
{
// If the message was sent successfully, log a success message
AddLogMessage("Message sent successfully.");
// Update the webhook nickname
string nickname = nicknameTextBox.Text.Trim();
if (!string.IsNullOrEmpty(nickname))
{
// Create a JSON payload with the new nickname
string payload = "{\"name\":\"" + nickname + "\"}";
// Create a new HttpRequestMessage to update the webhook
HttpRequestMessage request = new HttpRequestMessage(new HttpMethod("PATCH"), webhookUrl);
request.Content = new StringContent(payload, Encoding.UTF8, "application/json");
// Send the request to update the webhook
HttpResponseMessage nicknameResponse = client.SendAsync(request).Result;
// Check the response status code to see if the nickname was successfully updated
if (nicknameResponse.IsSuccessStatusCode)
{
// If the nickname was updated successfully, log a success message
AddLogMessage("Webhook nickname updated successfully.");
}
else
{
// If there was an error updating the nickname, log an error message
AddLogMessage("Error updating webhook nickname: " + nicknameResponse.StatusCode.ToString());
}
}
}
else
{
// If there was an error sending the message, log an error message
AddLogMessage("Error sending message: " + response.StatusCode.ToString());
}
}
catch (Exception ex)
{
// If there was an exception, log an error message
AddLogMessage("Error sending message: " + ex.Message);
}
}
private void AddLogMessage(string message)
{
if (logTextBox.InvokeRequired)
{
// If the call is not coming from the main thread, marshal the call to the main thread
logTextBox.Invoke(new Action<string>(AddLogMessage), message);
}
else
{
// If the call is coming from the main thread, update the log text box
logTextBox.AppendText(DateTime.Now.ToString("HH:mm:ss") + " - " + message + "\r\n");
}
}
private void webhookUrlTextBox_TextChanged(object sender, EventArgs e)
{
// If the webhook URL text box is empty or false, disable the send button
if (string.IsNullOrEmpty(webhookUrlTextBox.Text.Trim()) || webhookUrlTextBox.Text.Trim().ToLower() == "false")
{
sendButton.Enabled = false;
}
else
{
sendButton.Enabled = true;
}
}
private void delaySlider_ValueChanged(object sender, EventArgs e)
{
// Get the value of the delay slider and set the text of the delay label to the value in milliseconds
delayLabel.Text = $"{delaySlider.Value} ms";
sendDelay = delaySlider.Value;
}
private Thread sendingThread = null;
private void enableSendingCheckBox_CheckedChanged(object sender, EventArgs e)
{
sendingEnabled = enableSendingCheckBox.Checked;
if (sendingEnabled)
{
// Start a new thread to send messages at the specified interval
sendingThread = new Thread(new ThreadStart(SendingThreadFunction));
sendingThread.Start();
}
else
{
// Stop the sending thread
sendingThread?.Abort();
sendingThread = null;
}
}
private void SendingThreadFunction()
{
while (sendingEnabled)
{
// Send the message
sendButton_Click(this, EventArgs.Empty);
// Sleep for the specified delay
Thread.Sleep(sendDelay);
}
}
}
}