-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWarnings.ContextMenus.cs
96 lines (86 loc) · 4.24 KB
/
Warnings.ContextMenus.cs
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
using CompatApiClient.Utils;
using DSharpPlus.Commands.Processors.MessageCommands;
using DSharpPlus.Commands.Processors.UserCommands;
using DSharpPlus.Interactivity;
using Microsoft.Extensions.DependencyInjection;
namespace CompatBot.Commands;
internal static class WarningsContextMenus
{
[Command("❗ Warn"), RequiresBotModRole, SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu)]
public static ValueTask WarnUser(UserCommandContext ctx, DiscordUser user)
=> Warn(ctx, null, user);
[Command("❗ Warn user"), RequiresBotModRole, SlashCommandTypes(DiscordApplicationCommandType.MessageContextMenu)]
public static ValueTask WarnMessageAuthor(MessageCommandContext ctx, DiscordMessage message)
=> Warn(ctx, message, null);
[Command("🔍 Show warnings"), SlashCommandTypes(DiscordApplicationCommandType.UserContextMenu), AllowDMUsage]
public static ValueTask ShowWarnings(UserCommandContext ctx, DiscordUser user)
=> Warnings.ListGroup.List(ctx, user);
private static async ValueTask Warn(SlashCommandContext ctx, DiscordMessage? message = null, DiscordUser? user = null)
{
var interactivity = ctx.Extension.ServiceProvider.GetService<InteractivityExtension>();
if (interactivity is null)
{
await ctx.RespondAsync($"{Config.Reactions.Failure} Couldn't get interactivity extension").ConfigureAwait(false);
return;
}
var interaction = ctx.Interaction;
var modal = new DiscordInteractionResponseBuilder()
.AsEphemeral()
.WithCustomId($"modal:warn:{Guid.NewGuid():n}")
.WithTitle("Issue new warning")
.AddComponents(
new DiscordTextInputComponent(
"Warning reason",
"warning",
"Rule #2",
min_length: 2
)
);
await ctx.RespondWithModalAsync(modal).ConfigureAwait(false);
try
{
InteractivityResult<ModalSubmittedEventArgs> modalResult;
string reason;
do
{
modalResult = await interactivity.WaitForModalAsync(modal.CustomId, ctx.User).ConfigureAwait(false);
if (modalResult.TimedOut)
return;
} while (!modalResult.Result.Values.TryGetValue("warning", out reason!));
interaction = modalResult.Result.Interaction;
await interaction.CreateResponseAsync(
DiscordInteractionResponseType.DeferredChannelMessageWithSource,
new DiscordInteractionResponseBuilder().AsEphemeral()
).ConfigureAwait(false);
user ??= message?.Author!;
var (saved, suppress, recent, total) = await Warnings.AddAsync(user.Id, ctx.User, reason, message?.Content.Sanitize()).ConfigureAwait(false);
if (!saved)
{
await ctx.RespondAsync($"{Config.Reactions.Failure} Couldn't save the warning, please try again", ephemeral: true).ConfigureAwait(false);
return;
}
if (!suppress)
{
var userMsgContent = $"""
User warning saved, {user.Mention} has {recent} recent warning{StringUtils.GetSuffix(recent)} ({total} total)
Warned for: {reason} by {ctx.User.Mention}
""";
var userMsg = new DiscordMessageBuilder()
.WithContent(userMsgContent)
.AddMention(new UserMention(user.Id));
if (message is not null)
userMsg.WithReply(message.Id, mention: true);
await ctx.Channel.SendMessageAsync(userMsg).ConfigureAwait(false);
}
await Warnings.ListUserWarningsAsync(ctx.Client, interaction, user.Id, user.Username.Sanitize()).ConfigureAwait(false);
}
catch (Exception e)
{
Config.Log.Error(e);
var msg = new DiscordInteractionResponseBuilder()
.AsEphemeral()
.WithContent($"{Config.Reactions.Failure} Failed to change nickname, check bot's permissions");
await interaction.EditOriginalResponseAsync(new(msg)).ConfigureAwait(false);
}
}
}