Skip to content
This repository has been archived by the owner on Mar 8, 2024. It is now read-only.

Added mod mail slash command to utils module. #719

Open
wants to merge 2 commits 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
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,9 @@ await ctx
[SlashCommand("uppy", "Shows how long I've been online already 😅"), UsedImplicitly]
public Task HandleUptimeCommand(InteractionContext ctx)
=> ctx.Services.GetRequiredService<UptimeCommand>().Handle(ctx);

[SlashCommand("modmail", "Send a message to the mods."), UsedImplicitly]
public Task HandleSilentMessageCommand(InteractionContext ctx, [Option("Message", "Your message for the moderators.", true)] string message, [Option("Anonymously", "Whether you want to remain anonymously.")] bool anonymously = false)
=> ctx.Services.GetRequiredService<ModMailSlashCommands>().Handle(ctx, message, anonymously);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using Microsoft.Extensions.Logging;
using POI.DiscordDotNet.Commands.SlashCommands.Modules;
using POI.Persistence.Repositories;

namespace POI.DiscordDotNet.Commands.SlashCommands.Utils;

public class ModMailSlashCommands : UtilSlashCommandsModule
{
private readonly IServerSettingsRepository _serverSettingsRepository;
private readonly ILogger<ModMailSlashCommands> _logger
;

public ModMailSlashCommands(IServerSettingsRepository serverSettingsRepository, ILogger<ModMailSlashCommands> logger)
{
_serverSettingsRepository = serverSettingsRepository;
_logger = logger;
}

public async Task Handle(InteractionContext ctx, string message, bool anonymously)
{
await ctx.CreateResponseAsync("Message has been sent.", true).ConfigureAwait(false);
var serverId = ctx.Guild.Id;
var serverSettings = await _serverSettingsRepository.FindOneById(serverId);
if (serverSettings?.ModMailChannelId == null)
{
_logger.LogWarning("Server settings or ModMailChannelId for {ServerId} not found", serverId);
return;
}

var channelId = serverSettings.ModMailChannelId!.Value;
DiscordChannel channel;
try{
channel = await ctx.Client.GetChannelAsync(channelId);
}
catch (Exception e)
{
_logger.LogWarning(e, "Channel {ChannelId} not found for server {ServerId}",channelId, serverId);
return;
}
var name = anonymously ? "Anonymous" : ctx.User.Username;
var embed = new DiscordEmbedBuilder()
.WithTitle($"New mod mail from {name}")
.WithDescription(message)
.WithTimestamp(DateTimeOffset.UtcNow)
.Build();
await channel.SendMessageAsync(embed).ConfigureAwait(false);
_logger.LogInformation("Mod mail sent from {UserId} to {ChannelId} on {ServerId}", name, channelId, serverId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ namespace POI.DiscordDotNet.Commands.SlashCommands.Utils;
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddUtilitySlashCommands(this IServiceCollection services) => services
.AddTransient<UptimeCommand>();
.AddTransient<UptimeCommand>().AddTransient<ModMailSlashCommands>();
}
6 changes: 5 additions & 1 deletion source/POI.Persistence.Domain/ServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ public class ServerSettings

public uint? StarboardEmojiCount { get; set; }

public ServerSettings(ulong serverId, ulong? rankUpFeedChannelId = null, ulong? birthdayRoleId = null, ulong? starboardChannelId = null, ulong? eventsChannelId = null, uint? starboardEmojiCount = null)
public ulong? ModMailChannelId { get; set; }

public ServerSettings(ulong serverId, ulong? rankUpFeedChannelId = null, ulong? birthdayRoleId = null, ulong? starboardChannelId = null, ulong? eventsChannelId = null,
uint? starboardEmojiCount = null, ulong? modMailChannelId = null)
{
ServerId = serverId;
RankUpFeedChannelId = rankUpFeedChannelId;
BirthdayRoleId = birthdayRoleId;
StarboardChannelId = starboardChannelId;
EventsChannelId = eventsChannelId;
StarboardEmojiCount = starboardEmojiCount;
ModMailChannelId = modMailChannelId;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace POI.Persistence.EFCore.Npgsql.Migrations
{
/// <inheritdoc />
public partial class ModMailMigrations : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<decimal>(
name: "ModMailChannelId",
table: "ServerSettings",
type: "numeric(20,0)",
nullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ModMailChannelId",
table: "ServerSettings");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.9")
.HasAnnotation("ProductVersion", "7.0.10")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
Expand Down Expand Up @@ -93,6 +93,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.Property<decimal?>("EventsChannelId")
.HasColumnType("numeric(20,0)");

b.Property<decimal?>("ModMailChannelId")
.HasColumnType("numeric(20,0)");

b.Property<decimal?>("RankUpFeedChannelId")
.HasColumnType("numeric(20,0)");

Expand Down
Loading