From dc44a51da197e7372ae7a1dc2149da3a6c1952fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9=20=D0=A8=D0=B8?= =?UTF-8?q?=D0=BC=D0=B0=D0=BD=D1=81=D0=BA=D0=B8=D0=B9?= Date: Thu, 16 Nov 2023 21:53:43 +0300 Subject: [PATCH] add ephemeral messages and fix some bugs --- Controllers/WeatherForecastController.cs | 41 +++++++++------------- Justice.cs | 2 +- Justice/Interactions/CitiesInteractions.cs | 41 ++++++++++++++++++---- Startup.cs | 1 - 4 files changed, 52 insertions(+), 33 deletions(-) diff --git a/Controllers/WeatherForecastController.cs b/Controllers/WeatherForecastController.cs index 190b366..bc1ccab 100644 --- a/Controllers/WeatherForecastController.cs +++ b/Controllers/WeatherForecastController.cs @@ -1,6 +1,7 @@ using Discord; using DiscordApp.Database; using Microsoft.AspNetCore.Mvc; +using System.Linq; using System.Text.Json.Nodes; namespace DiscordApp.Controllers @@ -10,8 +11,8 @@ namespace DiscordApp.Controllers public class redirects : ControllerBase { - [HttpGet("/redirects/{uri}")] - public IActionResult Get(string uri, [FromBody] string? bodyContent) + [HttpGet("/redirects/{uri}&channelid={channelid}")] + public async Task Get(string uri, [FromBody] string? bodyContent, ulong channelid) { var data = Startup.appDbContext.Redirects.First(k => k.Id == uri); if (data.RedirectType == Types.RedirectType.None) @@ -22,18 +23,16 @@ namespace DiscordApp.Controllers return Redirect(data.url); }else if (data.RedirectType == Types.RedirectType.Redirected) { - JsonNode jsonBodyContent = JsonNode.Parse(bodyContent); - string[] paymentData = jsonBodyContent["data"].ToString().Split(";"); - var channelId = paymentData[1].Split(":")[1]; - var channel = Startup.discordSocketClient.GetChannel(ulong.Parse(channelId)) as ITextChannel; - var message = channel.GetMessagesAsync().LastAsync().Result.Last() as IUserMessage; - message.ModifyAsync(func => + var guild = Startup.discordSocketClient.GetGuild(1107742957458685985); + var channel = guild.GetChannel(channelid) as ITextChannel; + var message = channel.GetMessagesAsync().LastOrDefaultAsync().Result.FirstOrDefault() as IUserMessage; + await message.ModifyAsync(func => { func.Content = " !"; func.Components = new ComponentBuilder() .WithButton(" ", "addBaseOnMapModalSender") .Build(); - }).RunSynchronously(); + }); return Redirect(message.GetJumpUrl()); } @@ -42,23 +41,17 @@ namespace DiscordApp.Controllers return BadRequest(); } } - [HttpPost("/redirects/{uri}")] - public IActionResult Post(string uri, [FromBody] string bodyContent) + [HttpGet("/redirects/{uri}")] + public IActionResult Post(string uri) { - JsonNode jsonBodyContent = JsonNode.Parse(bodyContent); - string[] paymentData = jsonBodyContent["data"].ToString().Split(";"); - var channelId = paymentData[1].Split(":")[1]; - var channel = Startup.discordSocketClient.GetChannel(ulong.Parse(channelId)) as ITextChannel; - var message = channel.GetMessagesAsync().LastAsync().Result.Last() as IUserMessage; - message.ModifyAsync(func => - { - func.Content = " !"; - func.Components = new ComponentBuilder() - .WithButton(" ", "addBaseOnMapModalSender") - .Build(); - }).RunSynchronously(); + var data = Startup.appDbContext.Redirects.First(k => k.Id == uri); + + data.RedirectType = Types.RedirectType.Redirected; + Startup.appDbContext.Redirects.Update(data); + Startup.appDbContext.SaveChanges(); + return Redirect(data.url); + - return Ok(message.GetJumpUrl()); } } diff --git a/Justice.cs b/Justice.cs index 02fee42..7d257ff 100644 --- a/Justice.cs +++ b/Justice.cs @@ -33,7 +33,7 @@ namespace DiscordApp await client.LoginAsync(TokenType.Bot, "MTE2NjA3OTk3NjQ0NjEwMzYwMg.GAKOIo.4af972Wh11G0EF4O5tNYb7l-vt5OwMc4HPRnjE"); await client.StartAsync(); await interactionHandler.InitializeAsync(); - + Startup.discordSocketClient = client; await Task.Delay(Timeout.Infinite); } diff --git a/Justice/Interactions/CitiesInteractions.cs b/Justice/Interactions/CitiesInteractions.cs index bc587ae..a0403bf 100644 --- a/Justice/Interactions/CitiesInteractions.cs +++ b/Justice/Interactions/CitiesInteractions.cs @@ -11,20 +11,24 @@ namespace DiscordApp.Justice.Interactions [ComponentInteraction("addBaseOnMap")] public async Task addBaseInteraction() { - var redirectUri = Guid.NewGuid().ToString(); await DeferAsync(true); + var thread = await ((ITextChannel)Context.Channel).CreateThreadAsync($"{Context.User.GlobalName}-addCityOnMap", ThreadType.PrivateThread) as IThreadChannel; + await thread.AddUserAsync(((IGuildUser)Context.User)); + await FollowupAsync(thread.Mention, ephemeral:true); + var redirectUri = Guid.NewGuid().ToString(); + var paymentData = new spworlds.Types.PaymentData() { Amount = 16, - Data = $"user:{Context.User.Id};channel:{Context.Channel.Id};", - RedirectUrl = $"https://discord.yawaflua.ru/redirects/{redirectUri}", + Data = $"user:{Context.User.Id};channel:{thread.Id};", + RedirectUrl = $"https://discord.yawaflua.ru/redirects/{redirectUri}&channelid={thread.Id}", WebHookUrl = $"https://discord.yawaflua.ru/redirects/{redirectUri}" }; var uri = await Startup.sp.InitPayment(paymentData); var redirectTable = new Redirects() { Id = redirectUri , url = uri}; Startup.appDbContext.Redirects.Add(redirectTable); Startup.appDbContext.SaveChanges(); - await FollowupAsync("Нажмите на кнопку ниже для оплаты", components: new ComponentBuilder().WithButton("Оплатить", url: $"https://discord.yawaflua.ru/redirects/{redirectUri}", style: ButtonStyle.Link).Build(), ephemeral: true); + await thread.SendMessageAsync("Нажмите на кнопку ниже для оплаты", components: new ComponentBuilder().WithButton("Оплатить", url: $"https://discord.yawaflua.ru/redirects/{redirectUri}", style: ButtonStyle.Link).Build()); } @@ -67,6 +71,8 @@ namespace DiscordApp.Justice.Interactions var channel = Context.Guild.GetTextChannel(1174722397820174439); await channel.SendMessageAsync("@ #here", embed: embed, components: components); await FollowupAsync("Заявка подана и передана ответственным лицам. Ожидайте!", ephemeral: true); + var threadChannel = Context.Channel as IThreadChannel; + await threadChannel.ModifyAsync(k => k.Archived = true); } [ComponentInteraction("accessNewBase")] @@ -115,15 +121,36 @@ namespace DiscordApp.Justice.Interactions try { await startup.addSityOnMap(city); - await FollowupAsync("Все готово!"); + await FollowupAsync("Все готово!", ephemeral: true); } catch { - await FollowupAsync("Какая-то ошибка, чекни логи"); + await FollowupAsync("Какая-то ошибка, чекни логи", ephemeral: true); + } + + } + [ComponentInteraction("declineNewBase")] + public async Task declineBaseOnMap() + { + await DeferAsync(true); + + var components = new ComponentBuilder() + .WithButton(customId: "accessNewBase", label: "✅", disabled: true) + .WithButton(customId: "declineNewBase", label: "❌", disabled: true) + .Build(); + + await ModifyOriginalResponseAsync(func => { func.Components = components; func.Content = $"Заявку отозвал {Context.User.Mention}"; }); + + try + { + await FollowupAsync("Все готово!", ephemeral:true); + } + catch + { + await FollowupAsync("Какая-то ошибка, чекни логи", ephemeral:true); } } - } } diff --git a/Startup.cs b/Startup.cs index 9d238ea..2689dee 100644 --- a/Startup.cs +++ b/Startup.cs @@ -187,7 +187,6 @@ namespace DiscordApp serviceProvider = services.BuildServiceProvider(); appDbContext = serviceProvider.GetRequiredService(); - discordSocketClient = serviceProvider.GetRequiredService(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env)