Esempio n. 1
0
        public async Task <DiscordMessage?> EditOriginalMessage(SlashMessageEvent messageEvent, InteractionApplicationCommandCallbackData?response)
        {
            HttpRequestMessage msg = new();

            msg.Headers.Authorization = new("Bot", token);
            msg.Method = HttpMethod.Patch;

            msg.RequestUri = new Uri($"https://discord.com/api/webhooks/{botID}/{messageEvent.InteractionToken}/messages/@original");

            var json = JsonConvert.SerializeObject(response, Formatting.Indented, _jsonSerializerSettings);

            msg.Content = new StringContent(json);
            msg.Content.Headers.ContentType = new("application/json");

            var result = await _httpClient.SendAsync(msg);

            if (result.IsSuccessStatusCode)
            {
                var jsonResult = await result.Content.ReadAsStringAsync();

                var newCommand = JsonConvert.DeserializeObject <DiscordMessage>(jsonResult);

                //_logger.LogInformation("[DONE] Command updated");
                //_logger.LogDebug("[   ] Response was: {Json}", jsonResult);

                return(newCommand);
            }

            return(null);
        }
Esempio n. 2
0
        private async Task RunAsyncCommand(Interaction?i)
        {
            using (var scope = Services.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetRequiredService <CoreDatabaseContext>();

                if (i?.GuildId != null && dbContext.GetGuildFor(i.GuildId) is null)
                {
                    var discordGuild = await DiscordBot.Instance.Client.GetGuildAsync(i.GuildId);

                    _logger.LogInformation("Registering guild: {GuildID}", discordGuild.Id);

                    dbContext.Add(new Guild(discordGuild.Name, discordGuild.Id.ToString()));
                    dbContext.SaveChanges();
                }

                var messageEvent = new SlashMessageEvent(i, dbContext);



                var res = Parse(i.Data);

                if (res.Callback is not null)
                {
                    if (res.Callback is CommandFunction func)
                    {
                        //func.Cmd(res, messageEvent);
                        await _executor.RunCommand(res, messageEvent);

                        //_eventBus.PostEventAsync()
                    }
                }
                else
                {
                    //await messageEvent.RespondError("Could not find the command");
                }
            }
        }