Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add report commands #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MacroBotApp/Config/BotConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class ChannelsConfig
public ulong ErrorLogChannelId { get; set; }
public ulong MemberScreeningChannelId { get; set; }
public ulong StatusCheckChannelId { get; set; }
public ulong ReportsChannelId { get; set; }
public ulong[] ImageOnlyChannels { get; set; }
}
}
Expand Down
13 changes: 13 additions & 0 deletions MacroBotApp/DataAccess/AutoMapper/ReportMapping.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using AutoMapper;
using MacroBot.DataAccess.Entities;
using MacroBot.Discord.Modules.Reports;

namespace MacroBot.DataAccess.AutoMapper;

public class ReportMapping : Profile
{
public ReportMapping()
{
CreateMap<ReportEntity, Report>();
}
}
13 changes: 13 additions & 0 deletions MacroBotApp/DataAccess/Entities/ReportEntity.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MacroBot.DataAccess.Entities;

public class ReportEntity
{
public string Id { get; set; }
public ulong Reporter { get; set; }
public ulong Guild { get; set; }
public ulong User { get; set; }
public ulong? Channel { get; set; }
public ulong? Message { get; set; }
public string Content { get; set; }
public DateTime Reported { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using MacroBot.DataAccess.Entities;
using MacroBot.Discord.Modules.Reports;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace MacroBot.DataAccess.EntityConfiguations;

public class ReportEntityConfiguration : IEntityTypeConfiguration<ReportEntity>
{
public void Configure(EntityTypeBuilder<ReportEntity> builder)
{
builder.ToTable("reports");
builder.HasKey(e => e.Id);
builder.Property(p => p.Id)
.HasColumnName("id")
.IsRequired();
builder.Property(p => p.Reporter)
.HasColumnName("reporter")
.IsRequired();
builder.Property(p => p.Content)
.HasColumnName("content")
.IsRequired();
builder.Property(p => p.Guild)
.HasColumnName("guild")
.IsRequired();
builder.Property(p => p.User)
.HasColumnName("user")
.IsRequired();
builder.Property(p => p.Reported)
.HasColumnName("reported")
.IsRequired()
.HasColumnType("datetime2");
}
}
8 changes: 5 additions & 3 deletions MacroBotApp/DataAccess/MacroBotContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace MacroBot.DataAccess;
public class MacroBotContext : DbContext
{
public DbSet<TagEntity> TagEntities => Set<TagEntity>();

public DbSet<ReportEntity> ReportEntities => Set<ReportEntity>();

protected override void OnConfiguring(DbContextOptionsBuilder options)
{
if (options.IsConfigured) return;
var connectionStringBuilder = new SqliteConnectionStringBuilder { DataSource = Paths.DatabasePath };
var connectionString = connectionStringBuilder.ToString();
var connection = new SqliteConnection(connectionString);
var connection = new SqliteConnection(connectionString);
var loggerFactory = new LoggerFactory()
.AddSerilog();
options.UseSqlite(connection,
Expand All @@ -25,9 +26,10 @@ protected override void OnConfiguring(DbContextOptionsBuilder options)
.Name));
options.UseLoggerFactory(loggerFactory);
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new TagEntityConfiguration());
modelBuilder.ApplyConfiguration(new ReportEntityConfiguration());
}
}
98 changes: 98 additions & 0 deletions MacroBotApp/DataAccess/Repositories/ReportRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using AutoMapper;
using MacroBot.DataAccess.Entities;
using MacroBot.DataAccess.RepositoryInterfaces;
using MacroBot.Discord.Modules.Reports;
using Microsoft.EntityFrameworkCore;
using Serilog;
using ILogger = Serilog.ILogger;

namespace MacroBot.DataAccess.Repositories;

public class ReportRepository : IReportRepository
{
private readonly MacroBotContext _dbContext;
private readonly ILogger _logger = Log.ForContext<ReportRepository>();
private readonly IMapper _mapper;

public ReportRepository(MacroBotContext dbContext, IMapper mapper)
{
_dbContext = dbContext;
_mapper = mapper;
}

public async Task<Report?> GetReport(string id)
{
var existingReport =
await _dbContext.ReportEntities.FirstOrDefaultAsync(x => x.Id == id);

if (existingReport is null)
{
_logger.Warning("Report {ReportId} not found", id);
return null;
}

var mappedReport = _mapper.Map<ReportEntity, Report>(existingReport);
return mappedReport;
}

public async Task<bool> ReportExists(string id)
{
return await _dbContext.ReportEntities.AnyAsync(x => x.Id == id);
}

public async Task<bool> ReportExists(ulong reporter, ulong user, ulong guildId)
{
return await _dbContext.ReportEntities.AnyAsync(x =>
x.Reporter == reporter && x.User == user && x.Guild == guildId);
}

public async Task<string?> CreateReport(ulong reporter, ulong user, ulong guild,
string content, ulong? channel = null, ulong? message = null, string? id = null,
DateTime? reported = null)
{

var reportEntity = new ReportEntity
{
Reporter = reporter,
User = user,
Guild = guild,
Id = id ?? GenerateReportId(6),
Channel = channel,
Message = message,
Content = content,
Reported = reported ?? DateTime.Now
};
await _dbContext.ReportEntities.AddAsync(reportEntity);
await _dbContext.SaveChangesAsync();
return reportEntity.Id;
}

public async Task DeleteReport(string id)
{
var existingReport = await _dbContext.ReportEntities.FirstOrDefaultAsync(x => x.Id == id);
if (existingReport is null)
{
_logger.Warning("Cannot delete report {ReportId} - Report ID not found", id);
return;
}

_dbContext.ReportEntities.Remove(existingReport);
await _dbContext.SaveChangesAsync();
}

public async Task<IEnumerable<Report>> GetReportsForGuild(ulong guildId)
{
var guildReports = await _dbContext.ReportEntities.Where(x => x.Guild == guildId).ToArrayAsync();
if (guildReports.Length == 0) return Enumerable.Empty<Report>();
var guildReportsMapped = _mapper.Map<IEnumerable<ReportEntity>, IEnumerable<Report>>(guildReports);
return guildReportsMapped;
}

public static string GenerateReportId(int length)
{
var random = new Random();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
}
17 changes: 17 additions & 0 deletions MacroBotApp/DataAccess/RepositoryInterfaces/IReportRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using MacroBot.Discord.Modules.Reports;

namespace MacroBot.DataAccess.RepositoryInterfaces;

public interface IReportRepository
{
public Task<Report?> GetReport(string id);
public Task<IEnumerable<Report>> GetReportsForGuild(ulong guildId);
public Task DeleteReport(string id);

public Task<string?> CreateReport(ulong reporter, ulong user, ulong guild,
string content, ulong? channel = null, ulong? message = null, string? id = null,
DateTime? reported = null);

public Task<bool> ReportExists(ulong reporter, ulong user, ulong guildId);
public Task<bool> ReportExists(string id);
}
13 changes: 13 additions & 0 deletions MacroBotApp/Discord/Modules/Reports/Report.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace MacroBot.Discord.Modules.Reports;

public class Report
{
public string Id { get; set; }
public ulong Reporter { get; set; }
public ulong Guild { get; set; }
public ulong User { get; set; }
public ulong? Channel { get; set; }
public ulong? Message { get; set; }
public string Content { get; set; }
public DateTime Reported { get; set; }
}
Loading