Skip to content

Commit

Permalink
Allow sniped users to delete their own sniped messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
Naamloos committed Dec 15, 2022
1 parent bcfe73b commit 59397d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion ModCore/Commands/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private async Task doSnipeAsync(InteractionContext ctx, bool edit)

var customId = ExtensionStatics.GenerateIdString("del", new Dictionary<string, string>()
{
{"u", ctx.User.Id.ToString() }
{"u", ctx.User.Id.ToString() + "|" + message.Author.ToString() },
});

response.AddComponents(new DiscordButtonComponent(ButtonStyle.Danger, customId, "", emoji: new DiscordComponentEmoji("🗑")));
Expand Down
26 changes: 13 additions & 13 deletions ModCore/Components/UtilComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using ModCore.Extensions.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ModCore.Components
Expand All @@ -14,21 +15,20 @@ public class UtilComponents : BaseComponentModule
[Component("del", ComponentType.Button)]
public async Task UnSnipeAsync(ComponentInteractionCreateEventArgs e, IDictionary<string, string> context)
{
var allowedUser = context["u"];
var allowedUsers = context["u"].Split('|').Select(x => ulong.TryParse(x, out ulong res)? res : 0);

if(ulong.TryParse(allowedUser, out var userId))
if(allowedUsers.Contains(e.User.Id))
{
if(e.User.Id == userId)
{
await e.Message.DeleteAsync();
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"✅ Alright, it's gone!").AsEphemeral());
}
else
{
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"⚠️ Only <@{userId}> can use this button to delete this message!").AsEphemeral());
}
await e.Message.DeleteAsync();
await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"✅ Alright, it's gone!").AsEphemeral());
}
else
{
var allowedUsersMention = string.Join(", ", allowedUsers.Select(x => $"<@{x}>"));

await e.Interaction.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"⚠️ Only {allowedUsersMention} can use this button to delete this message!").AsEphemeral());
}
}
}
Expand Down

0 comments on commit 59397d5

Please sign in to comment.