Skip to content
This repository was archived by the owner on Nov 24, 2022. It is now read-only.

Commit cb0ad54

Browse files
Merge pull request #63 from InfinityGhost/feature/direct-message-modmail
Add direct message mod mail
2 parents 320dd3e + f2b1adb commit cb0ad54

4 files changed

Lines changed: 58 additions & 6 deletions

File tree

TabletBot.Common/Settings.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ public sealed class Settings
1111
{
1212
private const ulong MAIN_GUILD_ID = 615607687467761684;
1313
private const ulong LOG_MESSAGE_CHANNEL_ID = 715344685853442198;
14+
private const ulong MOD_MAIL_CHANNEL_ID = 958916136966193182;
1415
private const ulong MODERATOR_ROLE_ID = 644180151755735060;
1516
private const ulong MUTED_ROLE_ID = 715342682293010452;
1617

1718
public ulong GuildID { set; get; } = MAIN_GUILD_ID;
1819
public ulong LogMessageChannelID { set; get; } = LOG_MESSAGE_CHANNEL_ID;
20+
public ulong ModMailChannelID { set; get; } = MOD_MAIL_CHANNEL_ID;
1921
public ulong ModeratorRoleID { set; get; } = MODERATOR_ROLE_ID;
2022
public ulong MutedRoleID { set; get; } = MUTED_ROLE_ID;
2123
public string CommandPrefix { set; get; } = "!";
@@ -30,7 +32,7 @@ public sealed class Settings
3032
[JsonIgnore]
3133
public bool RunAsUnit { set; get; } = false;
3234

33-
private static readonly JsonSerializerOptions options = new JsonSerializerOptions
35+
private static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions
3436
{
3537
WriteIndented = true
3638
};
@@ -39,21 +41,21 @@ public async Task Write(FileInfo file)
3941
{
4042
if (!file.Directory.Exists)
4143
file.Directory.Create();
42-
using (var fs = file.Create())
43-
await JsonSerializer.SerializeAsync<Settings>(fs, this, options);
44+
await using (var fs = file.Create())
45+
await JsonSerializer.SerializeAsync<Settings>(fs, this, SerializerOptions);
4446
}
4547

4648
public static async Task<Settings> Read(FileInfo file)
4749
{
48-
using (var fs = file.OpenRead())
50+
await using (var fs = file.OpenRead())
4951
return await JsonSerializer.DeserializeAsync<Settings>(fs);
5052
}
5153

5254
public async Task<string> ExportAsync()
5355
{
54-
using (var ms = new MemoryStream())
56+
await using (var ms = new MemoryStream())
5557
{
56-
await JsonSerializer.SerializeAsync<Settings>(ms, this, options);
58+
await JsonSerializer.SerializeAsync<Settings>(ms, this, SerializerOptions);
5759
ms.Position = 0;
5860
using (var sr = new StreamReader(ms))
5961
return await sr.ReadToEndAsync();

TabletBot.Discord/BotServiceCollection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using TabletBot.Discord.SlashCommands;
88
using TabletBot.Discord.Watchers;
99
using TabletBot.Discord.Watchers.Commands;
10+
using TabletBot.Discord.Watchers.DirectMessage;
1011
using TabletBot.Discord.Watchers.GitHub;
1112
using TabletBot.Discord.Watchers.ReactionRoles;
1213
using TabletBot.Discord.Watchers.Spam;
@@ -32,6 +33,7 @@ public static IServiceCollection Build(Settings settings, DiscordSocketClient di
3233
.AddMessageWatcher<CommandMessageWatcher>()
3334
.AddMessageWatcher<IssueMessageWatcher>()
3435
.AddMessageWatcher<SpamMessageWatcher>()
36+
.AddMessageWatcher<ModMailMessageWatcher>()
3537
// Reaction watchers
3638
.AddReactionWatcher<RoleReactionWatcher>()
3739
// Interaction watchers

TabletBot.Discord/TabletBot.Discord.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net5.0</TargetFramework>
5+
<Nullable>enable</Nullable>
56
</PropertyGroup>
67

78
<ItemGroup>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Linq;
2+
using System.Threading.Tasks;
3+
using Discord;
4+
using Discord.WebSocket;
5+
using TabletBot.Common;
6+
using TabletBot.Discord.Commands;
7+
8+
namespace TabletBot.Discord.Watchers.DirectMessage
9+
{
10+
public class ModMailMessageWatcher : IMessageWatcher
11+
{
12+
private readonly DiscordSocketClient _client;
13+
private readonly Settings _settings;
14+
15+
public ModMailMessageWatcher(DiscordSocketClient client, Settings settings)
16+
{
17+
_client = client;
18+
_settings = settings;
19+
}
20+
21+
private IMessageChannel? _directMessageLogChannel;
22+
23+
public async Task Receive(IMessage message)
24+
{
25+
if (message.Channel is IDMChannel dmChannel)
26+
{
27+
if (_directMessageLogChannel == null)
28+
{
29+
var channel = await _client.Rest.GetChannelAsync(_settings.ModMailChannelID);
30+
_directMessageLogChannel = (IMessageChannel) channel;
31+
}
32+
33+
var embed = new EmbedBuilder
34+
{
35+
Description = message.Content,
36+
Timestamp = message.Timestamp,
37+
Author = dmChannel.Recipient.ToEmbedAuthor(),
38+
Color = Color.Blue
39+
};
40+
41+
await _directMessageLogChannel.SendMessageAsync(embed: embed.Build());
42+
}
43+
}
44+
45+
public Task Deleted(Cacheable<IMessage, ulong> message, Cacheable<IMessageChannel, ulong> channel) => Task.CompletedTask;
46+
}
47+
}

0 commit comments

Comments
 (0)