Skip to content

Commit 784609c

Browse files
committed
Improved behavior in case of generated messages bigger than 2000 characters
1 parent a071c89 commit 784609c

3 files changed

Lines changed: 47 additions & 26 deletions

File tree

UPBot Code/Commands/Game.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,35 @@ public async Task RPSCommand(CommandContext ctx) {
5959
await ctx.Channel.DeleteMessageAsync(msg);
6060
if (ir.Id == "idrock") {
6161
if(val == 0) {
62-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**"));
62+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**");
6363
}
6464
else if(val == 1) {
65-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**"));
65+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**");
6666
}
6767
else if(val == 2) {
68-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**"));
68+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**");
6969
}
7070
}
7171
else if (ir.Id == "idpaper") {
7272
if(val == 0) {
73-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**"));
73+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**");
7474
}
7575
else if(val == 1) {
76-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**"));
76+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**");
7777
}
7878
else if(val == 2) {
79-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**"));
79+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**");
8080
}
8181
}
8282
else if (ir.Id == "idscissors") {
8383
if(val == 0) {
84-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**"));
84+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**");
8585
}
8686
else if(val == 1) {
87-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**"));
87+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**");
8888
}
8989
else if(val == 2) {
90-
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**"));
90+
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**");
9191
}
9292
}
9393
}
@@ -136,27 +136,27 @@ Task PlayRockPaperScissors(CommandContext ctx, string kind) {
136136

137137
if (playerChoice == RPSTypes.Rock) {
138138
if (botChoice == RPSTypes.Rock) {
139-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**"));
139+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**");
140140
} else if (botChoice == RPSTypes.Paper) {
141-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**"));
141+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**");
142142
} else {
143-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**"));
143+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**");
144144
}
145145
} else if (playerChoice == RPSTypes.Paper) {
146146
if (botChoice == RPSTypes.Rock) {
147-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**"));
147+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**");
148148
} else if (botChoice == RPSTypes.Paper) {
149-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**"));
149+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**");
150150
} else {
151-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**"));
151+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**");
152152
}
153153
} else {
154154
if (botChoice == RPSTypes.Rock) {
155-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**"));
155+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**");
156156
} else if (botChoice == RPSTypes.Paper) {
157-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**"));
157+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**");
158158
} else {
159-
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**"));
159+
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**");
160160
}
161161
}
162162
}

UPBot Code/Commands/Stats.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,28 +69,28 @@ async Task GenerateStats(CommandContext ctx, string cmd, DiscordChannel ch) {
6969
var msg = await ctx.Channel.SendMessageAsync("Counting emojis usage, mentioned users and roles for the channel...");
7070
string res = await CalculateAll(ctx, ch);
7171
await Utils.DeleteDelayed(60, ctx.Message);
72-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
72+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
7373

7474
} else if (cmd == "emojis") {
7575
var msg = await ctx.Channel.SendMessageAsync("Counting emojis usage, mentioned users and roles for the channel...");
7676
string res = await CalculateEmojis(ctx, ch);
7777
_ = msg.DeleteAsync();
7878
await Utils.DeleteDelayed(60, ctx.Message);
79-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
79+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
8080

8181
} else if (cmd == "users") {
8282
var msg = await ctx.Channel.SendMessageAsync("Counting mentioned people...");
8383
string res = await CalculateUserMentions(ctx, ch);
8484
_ = msg.DeleteAsync();
8585
await Utils.DeleteDelayed(60, ctx.Message);
86-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
86+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
8787

8888
} else if (cmd == "roles") {
8989
var msg = await ctx.Channel.SendMessageAsync("Counting mentioned roles...");
9090
string res = await CalculateRoleMentions(ctx, ch);
9191
_ = msg.DeleteAsync();
9292
await Utils.DeleteDelayed(60, ctx.Message);
93-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
93+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
9494
}
9595
} catch (Exception ex) {
9696
await ctx.RespondAsync(Utils.GenerateErrorAnswer(ctx.Guild.Name, "Stats", ex));
@@ -179,28 +179,28 @@ public async Task GenerateStatsInteractive(CommandContext ctx) {
179179
msg = await ctx.Channel.SendMessageAsync("Counting emojis usage, mentioned users and roles for the channel...");
180180
string res = await CalculateAll(ctx, ctx.Channel);
181181
msg.DeleteAsync().Wait();
182-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
182+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
183183

184184
} else if (ir.Id == "idusedemojis") {
185185
msg.DeleteAsync().Wait();
186186
msg = await ctx.Channel.SendMessageAsync("Counting emojis usage for the channel...");
187187
string res = await CalculateEmojis(ctx, ctx.Channel);
188188
msg.DeleteAsync().Wait();
189-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
189+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
190190

191191
} else if (ir.Id == "idmentionedusers") {
192192
msg.DeleteAsync().Wait();
193193
msg = await ctx.Channel.SendMessageAsync("Counting mentioned people...");
194194
string res = await CalculateUserMentions(ctx, ctx.Channel);
195195
msg.DeleteAsync().Wait();
196-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
196+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
197197

198198
} else if (ir.Id == "idmentionedroles") {
199199
msg.DeleteAsync().Wait();
200200
msg = await ctx.Channel.SendMessageAsync("Counting mentioned roles...");
201201
string res = await CalculateRoleMentions(ctx, ctx.Channel);
202202
msg.DeleteAsync().Wait();
203-
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
203+
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
204204

205205
} else {
206206
await Utils.DeleteDelayed(120, ctx.Message);

UPBot Code/Utils.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,27 @@ public static Task DeleteDelayed(int seconds, DiscordMessage msg1) {
413413
Task.Run(() => DelayAfterAWhile(msg1, seconds * 1000));
414414
return Task.FromResult(0);
415415
}
416+
public static Task DeleteDelayedSend(int seconds, DiscordChannel ch, string msg) {
417+
if (msg.Length > 1999) { // Split
418+
List<DiscordMessage> msgs = new List<DiscordMessage>();
419+
while (msg.Length > 1999) {
420+
int pos = msg.LastIndexOf(' ', 2000);
421+
if (pos == -1) pos = 1990;
422+
string msg1 = msg[0..pos].Trim();
423+
msg = msg[pos..].Trim();
424+
msgs.Add(ch.SendMessageAsync(msg1).Result);
425+
}
426+
if (msg.Length > 0) msgs.Add(ch.SendMessageAsync(msg).Result);
427+
foreach (var dm in msgs) {
428+
Task.Run(() => DelayAfterAWhile(dm, seconds * 1000));
429+
}
430+
}
431+
else {
432+
DiscordMessage dmsg = ch.SendMessageAsync(msg).Result;
433+
Task.Run(() => DelayAfterAWhile(dmsg, seconds * 1000));
434+
}
435+
return Task.FromResult(0);
436+
}
416437

417438
/// <summary>
418439
/// Used to delete some messages after a while

0 commit comments

Comments
 (0)