Skip to content

Commit f9c162c

Browse files
committed
Merged
2 parents d77ed36 + fe6af82 commit f9c162c

29 files changed

+29691
-0
lines changed

UPBot Code/Commands/Ping.cs

+274
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using DSharpPlus.Entities;
5+
using DSharpPlus.SlashCommands;
6+
using UPBot.UPBot_Code;
7+
8+
/// <summary>
9+
/// This command implements a basic ping command.
10+
/// It is mostly for debug reasons.
11+
/// author: CPU
12+
/// </summary>
13+
///
14+
15+
public class SlashPing : ApplicationCommandModule
16+
{
17+
const int MaxTrackedRequests = 10;
18+
readonly Random random = new();
19+
static int lastGlobal = -1;
20+
static List<LastRequestByMember> lastRequests = null;
21+
22+
23+
24+
[SlashCommand("ping", "Checks if the bot is alive")]
25+
public async Task PingCommand(InteractionContext ctx)
26+
{
27+
if (ctx.Guild == null)
28+
await ctx.CreateResponseAsync("I am alive, but I sould be used only in guilds.", true);
29+
else
30+
await GeneratePong(ctx);
31+
}
32+
33+
readonly string[,] answers = {
34+
/* Good */ { "I am alive!", "Pong", "Ack", "I am here", "I am here $$$", "I am here @@@", "Pong, $$$" },
35+
/* Again? */ { "Again, I am alive", "Again, Pong", "Another Ack", "I told you, I am here", "Yes, I am here $$$", "@@@, I told you I am here", "Pong, $$$. You don't get it?" },
36+
/* Testing? */ { "Are you testing something?", "Are you doing some debug?", "Are you testing something, $$$?", "Are you doing some debug, @@@?", "Yeah, I am here.",
37+
"There is something wrong?", "Do you really miss me or is this a joke?" },
38+
/* Light annoyed */ {"This is no more funny", "Yeah, @@@, I am a bot", "I am contractually obliged to answer. But I do not like it", "I will start pinging you when you are asleep, @@@", "Look guys! $$$ has nothing better to do than pinging me!",
39+
"I am alive, but I am also annoyed", "ƃuoԀ" },
40+
/* Menacing */ {"Stop it.", "I will probably write your name in my black list", "Why do you insist?", "Find another bot to harass", "<the bot is not working>", "Request time out.", "You are consuming your keyboard" },
41+
/* Punishment */ {"I am going to **_ignore_** you", "@@@ you are a bad person. And you will be **_ignored_**", "I am not answering **_anymore_** to you", "$$$ account number is 555-343-1254. Go steal his money", "You are annoying me. I am going to **_ignore_** you.", "Enough is enough", "Goodbye" }
42+
};
43+
44+
45+
async Task GeneratePong(InteractionContext ctx)
46+
{
47+
Utils.LogUserCommand(ctx);
48+
try
49+
{
50+
51+
// Check if we have to initiialize our history of pings
52+
if (lastRequests == null) lastRequests = new List<LastRequestByMember>();
53+
54+
// Grab the current member id
55+
DiscordMember member = ctx.Member;
56+
ulong memberId = member.Id;
57+
58+
// Find the last request
59+
LastRequestByMember lastRequest = null;
60+
int annoyedLevel = 0;
61+
foreach (LastRequestByMember lr in lastRequests)
62+
if (lr.memberId == memberId)
63+
{
64+
lastRequest = lr;
65+
break;
66+
}
67+
if (lastRequest == null)
68+
{ // No last request, create one
69+
lastRequest = new LastRequestByMember(memberId);
70+
lastRequests.Add(lastRequest);
71+
}
72+
else
73+
{
74+
annoyedLevel = lastRequest.AddRequest();
75+
}
76+
if (annoyedLevel == -1)
77+
{
78+
await ctx.CreateResponseAsync("...");
79+
DiscordMessage empty = ctx.GetOriginalResponseAsync().Result;
80+
await empty.DeleteAsync(); // No answer
81+
return;
82+
}
83+
84+
// Was the request already done recently?
85+
int rnd = random.Next(0, 7);
86+
while (rnd == lastRequest.lastRandom || rnd == lastGlobal) rnd = random.Next(0, 7); // Find one that is not the same of last one
87+
lastRequest.lastRandom = rnd; // Record for the next time
88+
lastGlobal = rnd; // Record for the next time
89+
string msg = answers[annoyedLevel, rnd];
90+
msg = msg.Replace("$$$", member.DisplayName).Replace("@@@", member.Mention);
91+
92+
await ctx.CreateResponseAsync(msg);
93+
}
94+
catch (Exception ex)
95+
{
96+
if (ex is DSharpPlus.Exceptions.NotFoundException) return; // Timed out
97+
await ctx.CreateResponseAsync(Utils.GenerateErrorAnswer(ctx.Guild.Name, "Ping", ex));
98+
}
99+
}
100+
101+
102+
103+
public class LastRequestByMember
104+
{
105+
public ulong memberId; // the ID of the Discord user
106+
public DateTime[] requestTimes; // An array (10 elements) of when the last pings were done by the user
107+
public int lastRandom;
108+
readonly TimeSpan tenMins = TimeSpan.FromSeconds(600);
109+
110+
public LastRequestByMember(ulong memberId)
111+
{
112+
this.memberId = memberId;
113+
requestTimes = new DateTime[MaxTrackedRequests];
114+
requestTimes[0] = DateTime.Now;
115+
lastRandom = -1;
116+
for (int i = 1; i < MaxTrackedRequests; i++)
117+
requestTimes[i] = DateTime.MinValue;
118+
}
119+
120+
internal int AddRequest()
121+
{
122+
// remove all items older than 10 minutes
123+
for (int i = 0; i < requestTimes.Length; i++)
124+
{
125+
if (DateTime.Now - requestTimes[i] > tenMins)
126+
requestTimes[i] = DateTime.MinValue;
127+
}
128+
// Move to have the first not null in first place
129+
for (int i = 0; i < requestTimes.Length; i++)
130+
if (requestTimes[i] != DateTime.MinValue)
131+
{
132+
// Move all back "i" positions
133+
for (int j = i; j < requestTimes.Length; j++)
134+
{
135+
requestTimes[j - i] = requestTimes[j];
136+
}
137+
// Set as null the remaining
138+
for (int j = 0; j < i; j++)
139+
{
140+
requestTimes[requestTimes.Length - j - 1] = DateTime.MinValue;
141+
}
142+
break;
143+
}
144+
// Find the first empty position and set it to max
145+
int num = 0;
146+
for (int i = 0; i < requestTimes.Length; i++)
147+
if (requestTimes[i] == DateTime.MinValue)
148+
{
149+
requestTimes[i] = DateTime.Now;
150+
num = i + 1;
151+
break;
152+
}
153+
if (num == 0)
154+
{ // We did not find any valid value. Shift everything back one place
155+
for (int i = 0; i < requestTimes.Length - 1; i++)
156+
{
157+
requestTimes[i] = requestTimes[i + 1];
158+
}
159+
requestTimes[^1] = DateTime.Now;
160+
num = requestTimes.Length;
161+
}
162+
163+
// Get the time from the first to the current and count how many are
164+
TimeSpan amount = DateTime.Now - requestTimes[0];
165+
float averageBetweenRequests = (float)amount.TotalSeconds / num;
166+
167+
int index = 0;
168+
switch (num)
169+
{
170+
case 1: break;
171+
case 2:
172+
index = averageBetweenRequests switch
173+
{
174+
< 3 => 2,
175+
< 10 => 1,
176+
_ => index
177+
};
178+
break;
179+
case 3:
180+
index = averageBetweenRequests switch
181+
{
182+
< 5 => 3,
183+
< 10 => 2,
184+
< 20 => 1,
185+
_ => index
186+
};
187+
break;
188+
case 4:
189+
index = averageBetweenRequests switch
190+
{
191+
< 5 => 4,
192+
< 10 => 3,
193+
< 20 => 2,
194+
< 30 => 1,
195+
_ => index
196+
};
197+
break;
198+
case 5:
199+
index = averageBetweenRequests switch
200+
{
201+
< 5 => 5,
202+
< 20 => 4,
203+
< 30 => 3,
204+
< 40 => 2,
205+
< 50 => 1,
206+
_ => index
207+
};
208+
break;
209+
case 6:
210+
index = averageBetweenRequests switch
211+
{
212+
< 5 => -1,
213+
< 20 => 5,
214+
< 30 => 4,
215+
< 40 => 3,
216+
< 50 => 2,
217+
< 60 => 1,
218+
_ => index
219+
};
220+
break;
221+
case 7:
222+
index = averageBetweenRequests switch
223+
{
224+
< 5 => -1,
225+
< 10 => 5,
226+
< 20 => 4,
227+
< 30 => 3,
228+
< 40 => 2,
229+
< 50 => 1,
230+
_ => index
231+
};
232+
break;
233+
case 8:
234+
index = averageBetweenRequests switch
235+
{
236+
< 10 => -1,
237+
< 20 => 5,
238+
< 30 => 4,
239+
< 40 => 3,
240+
< 50 => 2,
241+
< 60 => 1,
242+
_ => index
243+
};
244+
break;
245+
case 9:
246+
index = averageBetweenRequests switch
247+
{
248+
< 10 => -1,
249+
< 15 => 5,
250+
< 20 => 4,
251+
< 25 => 3,
252+
< 30 => 2,
253+
< 35 => 1,
254+
_ => index
255+
};
256+
break;
257+
default:
258+
index = averageBetweenRequests switch
259+
{
260+
< 10 => -1,
261+
< 20 => 5,
262+
< 30 => 4,
263+
< 38 => 3,
264+
< 46 => 2,
265+
< 54 => 1,
266+
_ => index
267+
};
268+
break;
269+
}
270+
271+
return index;
272+
}
273+
}
274+
}

0 commit comments

Comments
 (0)