Skip to content

Commit c700d6d

Browse files
committed
Resolve knowing if it's a direct message or a message in a guild channel
1 parent d912195 commit c700d6d

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

src/DevChatter.Bot.Infra.Discord/DiscordChatClient.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ public class DiscordChatClient : IChatClient
2020
private TaskCompletionSource<bool> _connectionCompletionTask = new TaskCompletionSource<bool>();
2121
private TaskCompletionSource<bool> _disconnectionCompletionTask = new TaskCompletionSource<bool>();
2222
private SocketGuild _Guild;
23+
private readonly List<ulong> _GuildChannelIds = new List<ulong>();
2324
private ISocketMessageChannel _TextChannel;
2425
private bool _isReady;
2526

@@ -37,13 +38,15 @@ public DiscordChatClient(DiscordClientSettings settings)
3738
private async Task DiscordClientGuildAvailable(SocketGuild arg)
3839
{
3940
_Guild = arg;
41+
_GuildChannelIds.AddRange(_Guild.Channels.Select(channel => channel.Id));
4042
_TextChannel = _Guild.Channels.FirstOrDefault(channel => channel.Id == _settings.DiscordTextChannelId) as ISocketMessageChannel;
4143
_isReady = true;
4244
}
4345

4446
private async Task DiscordClientGuildUnavailable(SocketGuild arg)
4547
{
4648
_Guild = null;
49+
_GuildChannelIds.Clear();
4750
_isReady = false;
4851
}
4952

@@ -58,23 +61,34 @@ private async Task DiscordClientMessageReceived(SocketMessage arg)
5861
int commandStartIndex = 0;
5962
if (message.HasCharPrefix(_settings.CommandPrefix, ref commandStartIndex))
6063
{
61-
if (arg.Author is SocketGuildUser guildUser)
64+
if (_GuildChannelIds.Contains(message.Channel.Id))
6265
{
63-
GuildMessageReceived(guildUser, commandStartIndex, arg.Content);
66+
if (arg.Author is IGuildUser guildUser)
67+
{
68+
GuildCommandReceived(guildUser, commandStartIndex, arg.Content);
69+
}
70+
}
71+
else
72+
{
73+
DirectCommandReceieved(arg.Author, commandStartIndex, arg.Content);
6474
}
65-
66-
// TODO: arg.Author could be of type SocketGlobalUser (but the type is internal...) which means we got a direct message.
67-
// I'm not sure how else I can detect the difference I'm not seeing anything obvious in the API
6875
}
6976
}
7077

71-
private void GuildMessageReceived(SocketGuildUser user, int commandStartIndex, string message)
78+
private void GuildCommandReceived(IGuildUser user, int commandStartIndex, string message)
7279
{
7380
var commandInfo = CommandParser.Parse(message, commandStartIndex);
74-
if (!string.IsNullOrWhiteSpace(commandInfo.commandWord))
75-
{
76-
RaiseOnCommandReceived(user, commandInfo.commandWord, commandInfo.arguments);
77-
}
81+
if (string.IsNullOrWhiteSpace(commandInfo.commandWord)) return;
82+
83+
RaiseOnCommandReceived(user, commandInfo.commandWord, commandInfo.arguments);
84+
}
85+
86+
private void DirectCommandReceieved(IUser user, int commandStartIndex, string message)
87+
{
88+
var commandInfo = CommandParser.Parse(message, commandStartIndex);
89+
if (string.IsNullOrWhiteSpace(commandInfo.commandWord)) return;
90+
91+
// TODO: Do we want to handle direct message commands?
7892
}
7993

8094
public async Task Connect()
@@ -140,7 +154,7 @@ public void SendMessage(string message)
140154
_TextChannel.SendMessageAsync($"`{message}`").Wait();
141155
}
142156

143-
private void RaiseOnCommandReceived(SocketGuildUser user, string commandWord, List<string> arguments)
157+
private void RaiseOnCommandReceived(IGuildUser user, string commandWord, List<string> arguments)
144158
{
145159
var eventArgs = new CommandReceivedEventArgs
146160
{

src/DevChatter.Bot.Infra.Discord/Extensions/ModelExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Linq;
22
using DevChatter.Bot.Core.Data.Model;
3-
using Discord.WebSocket;
3+
using Discord;
44

55
namespace DevChatter.Bot.Infra.Discord.Extensions
66
{
77
public static class ModelExtensions
88
{
9-
public static ChatUser ToChatUser(this SocketGuildUser discordUser, DiscordClientSettings settings)
9+
public static ChatUser ToChatUser(this IGuildUser discordUser, DiscordClientSettings settings)
1010
{
1111
var chatUser = new ChatUser
1212
{
@@ -17,19 +17,19 @@ public static ChatUser ToChatUser(this SocketGuildUser discordUser, DiscordClien
1717
return chatUser;
1818
}
1919

20-
public static UserRole ToUserRole(this SocketGuildUser discordUser, DiscordClientSettings settings)
20+
public static UserRole ToUserRole(this IGuildUser discordUser, DiscordClientSettings settings)
2121
{
22-
if (discordUser.Roles.Any(role => role.Id == settings.DiscordStreamerRoleId))
22+
if (discordUser.RoleIds.Any(role => role == settings.DiscordStreamerRoleId))
2323
{
2424
return UserRole.Streamer;
2525
}
2626

27-
if (discordUser.Roles.Any(role => role.Id == settings.DiscordModeratorRoleId))
27+
if (discordUser.RoleIds.Any(role => role == settings.DiscordModeratorRoleId))
2828
{
2929
return UserRole.Mod;
3030
}
3131

32-
if (discordUser.Roles.Any(role => role.Id == settings.DiscordSubscriberRoleId))
32+
if (discordUser.RoleIds.Any(role => role == settings.DiscordSubscriberRoleId))
3333
{
3434
return UserRole.Subscriber;
3535
}

0 commit comments

Comments
 (0)