From c700d6d77dbb054f69f019975ab4160234d27e1f Mon Sep 17 00:00:00 2001 From: AridTag Date: Sat, 7 Apr 2018 17:57:33 -0400 Subject: [PATCH] Resolve knowing if it's a direct message or a message in a guild channel --- .../DiscordChatClient.cs | 36 +++++++++++++------ .../Extensions/ModelExtensions.cs | 12 +++---- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/DevChatter.Bot.Infra.Discord/DiscordChatClient.cs b/src/DevChatter.Bot.Infra.Discord/DiscordChatClient.cs index 909b3c6f..4c2c79b1 100644 --- a/src/DevChatter.Bot.Infra.Discord/DiscordChatClient.cs +++ b/src/DevChatter.Bot.Infra.Discord/DiscordChatClient.cs @@ -20,6 +20,7 @@ public class DiscordChatClient : IChatClient private TaskCompletionSource _connectionCompletionTask = new TaskCompletionSource(); private TaskCompletionSource _disconnectionCompletionTask = new TaskCompletionSource(); private SocketGuild _Guild; + private readonly List _GuildChannelIds = new List(); private ISocketMessageChannel _TextChannel; private bool _isReady; @@ -37,6 +38,7 @@ public DiscordChatClient(DiscordClientSettings settings) private async Task DiscordClientGuildAvailable(SocketGuild arg) { _Guild = arg; + _GuildChannelIds.AddRange(_Guild.Channels.Select(channel => channel.Id)); _TextChannel = _Guild.Channels.FirstOrDefault(channel => channel.Id == _settings.DiscordTextChannelId) as ISocketMessageChannel; _isReady = true; } @@ -44,6 +46,7 @@ private async Task DiscordClientGuildAvailable(SocketGuild arg) private async Task DiscordClientGuildUnavailable(SocketGuild arg) { _Guild = null; + _GuildChannelIds.Clear(); _isReady = false; } @@ -58,23 +61,34 @@ private async Task DiscordClientMessageReceived(SocketMessage arg) int commandStartIndex = 0; if (message.HasCharPrefix(_settings.CommandPrefix, ref commandStartIndex)) { - if (arg.Author is SocketGuildUser guildUser) + if (_GuildChannelIds.Contains(message.Channel.Id)) { - GuildMessageReceived(guildUser, commandStartIndex, arg.Content); + if (arg.Author is IGuildUser guildUser) + { + GuildCommandReceived(guildUser, commandStartIndex, arg.Content); + } + } + else + { + DirectCommandReceieved(arg.Author, commandStartIndex, arg.Content); } - - // TODO: arg.Author could be of type SocketGlobalUser (but the type is internal...) which means we got a direct message. - // I'm not sure how else I can detect the difference I'm not seeing anything obvious in the API } } - private void GuildMessageReceived(SocketGuildUser user, int commandStartIndex, string message) + private void GuildCommandReceived(IGuildUser user, int commandStartIndex, string message) { var commandInfo = CommandParser.Parse(message, commandStartIndex); - if (!string.IsNullOrWhiteSpace(commandInfo.commandWord)) - { - RaiseOnCommandReceived(user, commandInfo.commandWord, commandInfo.arguments); - } + if (string.IsNullOrWhiteSpace(commandInfo.commandWord)) return; + + RaiseOnCommandReceived(user, commandInfo.commandWord, commandInfo.arguments); + } + + private void DirectCommandReceieved(IUser user, int commandStartIndex, string message) + { + var commandInfo = CommandParser.Parse(message, commandStartIndex); + if (string.IsNullOrWhiteSpace(commandInfo.commandWord)) return; + + // TODO: Do we want to handle direct message commands? } public async Task Connect() @@ -140,7 +154,7 @@ public void SendMessage(string message) _TextChannel.SendMessageAsync($"`{message}`").Wait(); } - private void RaiseOnCommandReceived(SocketGuildUser user, string commandWord, List arguments) + private void RaiseOnCommandReceived(IGuildUser user, string commandWord, List arguments) { var eventArgs = new CommandReceivedEventArgs { diff --git a/src/DevChatter.Bot.Infra.Discord/Extensions/ModelExtensions.cs b/src/DevChatter.Bot.Infra.Discord/Extensions/ModelExtensions.cs index 35093ef6..b846afae 100644 --- a/src/DevChatter.Bot.Infra.Discord/Extensions/ModelExtensions.cs +++ b/src/DevChatter.Bot.Infra.Discord/Extensions/ModelExtensions.cs @@ -1,12 +1,12 @@ using System.Linq; using DevChatter.Bot.Core.Data.Model; -using Discord.WebSocket; +using Discord; namespace DevChatter.Bot.Infra.Discord.Extensions { public static class ModelExtensions { - public static ChatUser ToChatUser(this SocketGuildUser discordUser, DiscordClientSettings settings) + public static ChatUser ToChatUser(this IGuildUser discordUser, DiscordClientSettings settings) { var chatUser = new ChatUser { @@ -17,19 +17,19 @@ public static ChatUser ToChatUser(this SocketGuildUser discordUser, DiscordClien return chatUser; } - public static UserRole ToUserRole(this SocketGuildUser discordUser, DiscordClientSettings settings) + public static UserRole ToUserRole(this IGuildUser discordUser, DiscordClientSettings settings) { - if (discordUser.Roles.Any(role => role.Id == settings.DiscordStreamerRoleId)) + if (discordUser.RoleIds.Any(role => role == settings.DiscordStreamerRoleId)) { return UserRole.Streamer; } - if (discordUser.Roles.Any(role => role.Id == settings.DiscordModeratorRoleId)) + if (discordUser.RoleIds.Any(role => role == settings.DiscordModeratorRoleId)) { return UserRole.Mod; } - if (discordUser.Roles.Any(role => role.Id == settings.DiscordSubscriberRoleId)) + if (discordUser.RoleIds.Any(role => role == settings.DiscordSubscriberRoleId)) { return UserRole.Subscriber; }