Skip to content

Commit

Permalink
Resolve knowing if it's a direct message or a message in a guild channel
Browse files Browse the repository at this point in the history
  • Loading branch information
AridTag committed Apr 7, 2018
1 parent d912195 commit c700d6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 17 deletions.
36 changes: 25 additions & 11 deletions src/DevChatter.Bot.Infra.Discord/DiscordChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class DiscordChatClient : IChatClient
private TaskCompletionSource<bool> _connectionCompletionTask = new TaskCompletionSource<bool>();
private TaskCompletionSource<bool> _disconnectionCompletionTask = new TaskCompletionSource<bool>();
private SocketGuild _Guild;
private readonly List<ulong> _GuildChannelIds = new List<ulong>();
private ISocketMessageChannel _TextChannel;
private bool _isReady;

Expand All @@ -37,13 +38,15 @@ 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;
}

private async Task DiscordClientGuildUnavailable(SocketGuild arg)
{
_Guild = null;
_GuildChannelIds.Clear();
_isReady = false;
}

Expand All @@ -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()
Expand Down Expand Up @@ -140,7 +154,7 @@ public void SendMessage(string message)
_TextChannel.SendMessageAsync($"`{message}`").Wait();
}

private void RaiseOnCommandReceived(SocketGuildUser user, string commandWord, List<string> arguments)
private void RaiseOnCommandReceived(IGuildUser user, string commandWord, List<string> arguments)
{
var eventArgs = new CommandReceivedEventArgs
{
Expand Down
12 changes: 6 additions & 6 deletions src/DevChatter.Bot.Infra.Discord/Extensions/ModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -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;
}
Expand Down

0 comments on commit c700d6d

Please sign in to comment.