Skip to content

Commit

Permalink
Improved behavior in case of generated messages bigger than 2000 char…
Browse files Browse the repository at this point in the history
…acters
  • Loading branch information
CPULL committed Apr 28, 2022
1 parent a071c89 commit 784609c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 26 deletions.
36 changes: 18 additions & 18 deletions UPBot Code/Commands/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,35 +59,35 @@ public async Task RPSCommand(CommandContext ctx) {
await ctx.Channel.DeleteMessageAsync(msg);
if (ir.Id == "idrock") {
if(val == 0) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**");
}
else if(val == 1) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**");
}
else if(val == 2) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**");
}
}
else if (ir.Id == "idpaper") {
if(val == 0) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**");
}
else if(val == 1) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**");
}
else if(val == 2) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**");
}
}
else if (ir.Id == "idscissors") {
if(val == 0) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**");
}
else if(val == 1) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**");
}
else if(val == 2) {
await Utils.DeleteDelayed(15, await ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**"));
await Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**");
}
}
}
Expand Down Expand Up @@ -136,27 +136,27 @@ Task PlayRockPaperScissors(CommandContext ctx, string kind) {

if (playerChoice == RPSTypes.Rock) {
if (botChoice == RPSTypes.Rock) {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 🪨 Rock! **DRAW!**");
} else if (botChoice == RPSTypes.Paper) {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played 📄 Paper! **I win!**");
} else {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 🪨 Rock " + ctx.Member.Mention + ", I played ✂️ Scissor! **You win!**");
}
} else if (playerChoice == RPSTypes.Paper) {
if (botChoice == RPSTypes.Rock) {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 🪨 Rock! **You win!**");
} else if (botChoice == RPSTypes.Paper) {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played 📄 Paper! **DRAW!**");
} else {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said 📄 Paper " + ctx.Member.Mention + ", I played ✂️ Scissor! **I win!**");
}
} else {
if (botChoice == RPSTypes.Rock) {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 🪨 Rock! **I win!**");
} else if (botChoice == RPSTypes.Paper) {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played 📄 Paper! **You win!**");
} else {
return Utils.DeleteDelayed(15, ctx.Channel.SendMessageAsync("You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**"));
return Utils.DeleteDelayedSend(15, ctx.Channel, "You said ✂️ Scissor " + ctx.Member.Mention + ", I played ✂️ Scissor! **DRAW!**");
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions UPBot Code/Commands/Stats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,28 @@ async Task GenerateStats(CommandContext ctx, string cmd, DiscordChannel ch) {
var msg = await ctx.Channel.SendMessageAsync("Counting emojis usage, mentioned users and roles for the channel...");
string res = await CalculateAll(ctx, ch);
await Utils.DeleteDelayed(60, ctx.Message);
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);

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

} else if (cmd == "users") {
var msg = await ctx.Channel.SendMessageAsync("Counting mentioned people...");
string res = await CalculateUserMentions(ctx, ch);
_ = msg.DeleteAsync();
await Utils.DeleteDelayed(60, ctx.Message);
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);

} else if (cmd == "roles") {
var msg = await ctx.Channel.SendMessageAsync("Counting mentioned roles...");
string res = await CalculateRoleMentions(ctx, ch);
_ = msg.DeleteAsync();
await Utils.DeleteDelayed(60, ctx.Message);
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);
}
} catch (Exception ex) {
await ctx.RespondAsync(Utils.GenerateErrorAnswer(ctx.Guild.Name, "Stats", ex));
Expand Down Expand Up @@ -179,28 +179,28 @@ public async Task GenerateStatsInteractive(CommandContext ctx) {
msg = await ctx.Channel.SendMessageAsync("Counting emojis usage, mentioned users and roles for the channel...");
string res = await CalculateAll(ctx, ctx.Channel);
msg.DeleteAsync().Wait();
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);

} else if (ir.Id == "idusedemojis") {
msg.DeleteAsync().Wait();
msg = await ctx.Channel.SendMessageAsync("Counting emojis usage for the channel...");
string res = await CalculateEmojis(ctx, ctx.Channel);
msg.DeleteAsync().Wait();
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);

} else if (ir.Id == "idmentionedusers") {
msg.DeleteAsync().Wait();
msg = await ctx.Channel.SendMessageAsync("Counting mentioned people...");
string res = await CalculateUserMentions(ctx, ctx.Channel);
msg.DeleteAsync().Wait();
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);

} else if (ir.Id == "idmentionedroles") {
msg.DeleteAsync().Wait();
msg = await ctx.Channel.SendMessageAsync("Counting mentioned roles...");
string res = await CalculateRoleMentions(ctx, ctx.Channel);
msg.DeleteAsync().Wait();
await Utils.DeleteDelayed(120, ctx.Channel.SendMessageAsync(res));
await Utils.DeleteDelayedSend(120, ctx.Channel, res);

} else {
await Utils.DeleteDelayed(120, ctx.Message);
Expand Down
21 changes: 21 additions & 0 deletions UPBot Code/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,27 @@ public static Task DeleteDelayed(int seconds, DiscordMessage msg1) {
Task.Run(() => DelayAfterAWhile(msg1, seconds * 1000));
return Task.FromResult(0);
}
public static Task DeleteDelayedSend(int seconds, DiscordChannel ch, string msg) {
if (msg.Length > 1999) { // Split
List<DiscordMessage> msgs = new List<DiscordMessage>();
while (msg.Length > 1999) {
int pos = msg.LastIndexOf(' ', 2000);
if (pos == -1) pos = 1990;
string msg1 = msg[0..pos].Trim();
msg = msg[pos..].Trim();
msgs.Add(ch.SendMessageAsync(msg1).Result);
}
if (msg.Length > 0) msgs.Add(ch.SendMessageAsync(msg).Result);
foreach (var dm in msgs) {
Task.Run(() => DelayAfterAWhile(dm, seconds * 1000));
}
}
else {
DiscordMessage dmsg = ch.SendMessageAsync(msg).Result;
Task.Run(() => DelayAfterAWhile(dmsg, seconds * 1000));
}
return Task.FromResult(0);
}

/// <summary>
/// Used to delete some messages after a while
Expand Down

0 comments on commit 784609c

Please sign in to comment.