Exemplo n.º 1
0
        public static async Task GetDebugLogAsync(InteractionContext ctx)
        {
            await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource, new DiscordInteractionResponseBuilder().WithContent("Trying to get log").AsEphemeral(false));

            DateTime now         = DateTime.Now;
            var      target_file = $"miku_log{now.ToString("yyyy/MM/dd").Replace("/","")}.txt";

            if (!File.Exists(target_file))
            {
                await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Failed to get log"));

                return;
            }
            else
            {
                await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent($"Found log {Formatter.Bold(target_file)}"));
            }
            try
            {
                if (!File.Exists($"temp-{target_file}"))
                {
                    File.Copy(target_file, $"temp-{target_file}");
                }
                else
                {
                    File.Delete($"temp-{target_file}");
                    File.Copy(target_file, $"temp-{target_file}");
                }
                FileStream log = new(path : $"temp-{target_file}", FileMode.Open, FileAccess.Read);
                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().AddFile(target_file, log, true).WithContent($"Log {Formatter.Bold(target_file)}").AsEphemeral(false));

                log.Close();
                log.Dispose();
                File.Delete($"temp-{target_file}");
            }
            catch (Exception ex)
            {
                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(ex.Message).AsEphemeral(false));

                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().WithContent(ex.StackTrace).AsEphemeral(false));
            }
            await ctx.EditResponseAsync(new DiscordWebhookBuilder().WithContent("Done"));
        }
            public async Task SetLoginAsync(InteractionContext ctx,
                                            [Option("username", "Account Username")] string username,
                                            [Option("email", "Account Email"), EmailAddress] string email,
                                            [Option("password", "Account Password")] string password)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                RegisterModel credentials = new() { Username = username, Email = email, Password = password };
                Response      result      = await _apiAuth.RegisterNewUserAsync(credentials, CancellationToken.None);

                await ctx.FollowUpAsync($"{ctx.User.Mention} {result.Status} : {result.Message}\n");
            }
            public async Task ConfigureAutobanAsync(InteractionContext ctx)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                GuildConfig config = await guildConfig.FindOrCreateConfigAsync(ctx.Guild.Id);

                DiscordChannel current = ctx.Guild.GetChannel(config.JoinLogChannel);

                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder()
                {
                    Content = $"Autoban is currently {(config.AutoBanBlacklisted ? "on" : "off")}."
                }
                                        .AddComponents(new DiscordSelectComponent("sg-autoban-select", "Turn Autoban on/off", GetToggleOptions(), maxOptions: 1)));
            }
            public async Task SetServerEmitterAsync(InteractionContext ctx)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                GuildConfig config = await guildConfig.FindOrCreateConfigAsync(ctx.Guild.Id);

                if (config.ApiLogin is null)
                {
                    await ctx.FollowUpAsync("Please set API Credentials first.");

                    return;
                }

                await _emitterClient.SetEmitterAsync(new()
                {
                    DisplayName = ctx.Guild.Name,
                    EmitterType = EmitterType.Server,
                    Snowflake   = ctx.Guild.Id
                }, await _apiAuthService.GetOrUpdateAuthTokenAsync(ctx.Guild.Id));

                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder { Content = "Emitter successfully set :" }
                                        .AddEmbed(Utilities.BuildEmitterEmbed(await _emitterClient.GetEmitterAsync(await _apiAuthService.GetOrUpdateAuthTokenAsync(ctx.Guild.Id)))));
            }
            public async Task ConfigureBanlogAsync(InteractionContext ctx)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                GuildConfig config = await guildConfig.FindOrCreateConfigAsync(ctx.Guild.Id);

                DiscordChannel current = ctx.Guild.GetChannel(config.JoinLogChannel);

                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder()
                {
                    Content = $"Current Banlog channel : {current?.Mention ?? "None"}."
                }
                                        .AddComponents(new DiscordSelectComponent("sg-banlog-select", "Select Banlog channel",
                                                                                  GetWritableChannelOptions(ctx.Guild).Prepend(EmptySelectionOption), maxOptions: 1)));
            }
            public async Task GetEmitterAsync(InteractionContext ctx)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                GuildConfig config = await guildConfig.FindOrCreateConfigAsync(ctx.Guild.Id);

                if (config.ApiLogin is null)
                {
                    await ctx.FollowUpAsync("Please set API Credentials first.");

                    return;
                }

                Emitter emitter = await _emitterClient.GetEmitterAsync(await _apiAuthService.GetOrUpdateAuthTokenAsync(ctx.Guild.Id));

                if (emitter is null)
                {
                    await ctx.FollowUpAsync("No Emitter set for provided credentials.");
                }
                else
                {
                    await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder().AddEmbed(Utilities.BuildEmitterEmbed(emitter)));
                }
            }
            public async Task SetLoginAsync(InteractionContext ctx,
                                            [Option("username", "Account Username")] string username,
                                            [Option("password", "Account Password")] string password)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                GuildConfig config = await guildConfig.FindOrCreateConfigAsync(ctx.Guild.Id);

                config.ApiLogin = new() { Username = username, Password = encryption.Encrypt(password) };

                await guildConfig.UpdateOneAsync(
                    Builders <GuildConfig> .Filter.Eq(c => c.Id, config.Id),
                    Builders <GuildConfig> .Update.Set(c => c.ApiLogin, config.ApiLogin));

                await ctx.FollowUpAsync($"API credentials has been set.", true);
            }
            public async Task ConfigureJoinlogSuppressAsync(InteractionContext ctx)
            {
                await ctx.CreateResponseAsync(InteractionResponseType.DeferredChannelMessageWithSource, new() { IsEphemeral = true });

                GuildConfig config = await guildConfig.FindOrCreateConfigAsync(ctx.Guild.Id);

                DiscordChannel current = ctx.Guild.GetChannel(config.JoinLogChannel);

                await ctx.FollowUpAsync(new DiscordFollowupMessageBuilder()
                {
                    Content = config.SuppressJoinlogCleanRecords
                                                ? "All clean records are currently suppressed from displaying in Joinlog."
                                                : "All records are displayed in Joinlog."
                }
                                        .AddComponents(new DiscordSelectComponent("sg-joinlog-suppress-select", "Turn Autoban on/off", GetToggleOptions(), maxOptions: 1)));
            }
            protected async Task InsertUserAsync(InteractionContext context, DiscordUser user, byte level, string reason, bool banUser = false, int deleteDays = 0)
            {
                if (user?.Id == context.User.Id)
                {
                    await context.FollowUpAsync("You cannot insert yourself in the Trustlist.", true);
                }
                else if (user.IsBot)
                {
                    await context.FollowUpAsync("You cannot insert a Bot in the Trustlist.", true);
                }
                else if ((user as DiscordMember)?.Roles.Any(r => r.Permissions.HasPermission(Permissions.ManageGuild)) ?? false)
                {
                    await context.FollowUpAsync("You cannot insert a server operator in the Trustlist. Demote them first.", true);
                }
                else if (reason.Length < 5)
                {
                    await context.FollowUpAsync("Reason is too short", true);
                }
                else
                {
                    try
                    {
                        GuildConfig config = await guildConfig.FindOrCreateConfigAsync(context.Guild.Id);

                        if (config.ApiLogin is null)
                        {
                            await context.FollowUpAsync("No API Credentials set. Use ``/socialguard config accesskey <key>`` to set an Access Key.");
                        }
                        else
                        {
                            await _trustlist.SubmitEntryAsync(user.Id, new()
                            {
                                EscalationLevel = level,
                                EscalationNote  = reason
                            }, (await _apiAuth.GetOrUpdateAuthTokenAsync(context.Guild.Id)).Token);

                            string       userMention = (user as DiscordMember)?.Mention ?? user.Id.ToString();
                            DiscordEmbed embed       = await _trustlist.GetLookupEmbedAsync(user);

                            await context.FollowUpAsync(new DiscordFollowupMessageBuilder()
                            {
                                Content = $"User '{userMention}' successfully inserted into Trustlist."
                            }
                                                        .AddEmbed(embed));

                            if (banUser || (config.AutoBanBlacklisted && level >= 3))
                            {
                                await context.Guild.BanMemberAsync(user.Id, deleteDays, $"[SocialGuard] {reason}");

                                await context.Guild.GetChannel(config.BanLogChannel).SendMessageAsync($"Banned user '{userMention}'.", embed);
                            }
                        }
                    }
                    catch (ApplicationException e)
                    {
                        await context.FollowUpAsync(e.Message, true);

#if DEBUG
                        throw;
#endif
                    }
                }
            }