public async Task <Result> RespondAsync(IMessageCreate ev, CancellationToken ct)
    {
        if (ev.Author.IsBot.HasValue && ev.Author.IsBot.Value ||
            ev.Author.IsSystem.HasValue && ev.Author.IsSystem.Value ||
            !ev.GuildID.HasValue ||
            ev.ChannelID.Value != _polychatSettings.ChatChannelId ||
            ev.Content.StartsWith(_discordSettings.Prefix)
            )
        {
            return(Result.FromSuccess());
        }

        var sanitisedMsgContent = await _sanitiser.SanitiseMessageContent(ev);

        var authorPolychatString = new PolychatChatMessageString(ev.Author.Username);
        var protoMsgContent      = $"§9[Discord] §7{authorPolychatString.ToSanitisedString()}§r: {sanitisedMsgContent}";
        var protoMsg             = new ChatMessage
        {
            ServerId      = "Discord",
            Message       = protoMsgContent,
            MessageOffset = protoMsgContent.IndexOf(':')
        };

        await _polychatService.BroadcastMessage(protoMsg);

        return(Result.FromSuccess());
    }
    /// <inheritdoc />
    public async Task <string> SanitiseMessageContent(IMessage message)
    {
        var s = message.Content;

        s = SanitiseUsernameAndNicknameMentions(s, message.Mentions);
        s = await SanitiseChannelMentions(s, message.GuildID);

        s = await SanitiseRoleMentions(s, message.GuildID);

        s = SanitiseStandardEmoji(s);
        s = SanitiseCustomEmoji(s);
        s = s.Replace("️", " ");

        var chatString = new PolychatChatMessageString(s);

        return(chatString.ToSanitisedString());
    }
Exemplo n.º 3
0
        protected override async Task Handle(TcpRequest <ServerStatus> request, CancellationToken cancellationToken)
        {
            var msg         = request.Message;
            var serverId    = new PolychatServerIdString(msg.ServerId);
            var sanitisedId = serverId.ToSanitisedUppercase();
            var server      = _polychatService.GetOnlineServerOrDefault(sanitisedId);

            if (server is not null)
            {
                await _polychatService.ForwardMessage(sanitisedId, msg);

                if (
                    msg.Status is
                    ServerStatus.Types.ServerStatusEnum.Stopped or ServerStatus.Types.ServerStatusEnum.Crashed
                    )
                {
                    _polychatService.RemoveOnlineServer(sanitisedId);
                    _logger.LogInformation("Removed server {id} from the list of online servers.", sanitisedId);
                    request.ConnectedClient.StopListening();
                }

                var getChatChannelResult =
                    await _channelApi.GetChannelAsync(new(_polychatSettings.ChatChannelId), cancellationToken);

                if (!getChatChannelResult.IsSuccess)
                {
                    throw new Exception(getChatChannelResult.Error.Message);
                }

                Optional <Color> embedColour = msg.Status switch
                {
                    ServerStatus.Types.ServerStatusEnum.Started => _colourPalette.Green,
                    ServerStatus.Types.ServerStatusEnum.Crashed => _colourPalette.Red,
                    ServerStatus.Types.ServerStatusEnum.Stopped => _colourPalette.Black,
                    _ => new()
                };
                var message           = $"Server {msg.Status.ToString().ToLowerInvariant()}.";
                var chatMessage       = new PolychatChatMessageString(sanitisedId, message);
                var embed             = new Embed(chatMessage.ToSanitisedString(), Colour: embedColour);
                var sendMessageResult = await _channelApi.CreateMessageAsync(new(_polychatSettings.ChatChannelId),
                                                                             embeds : new[] { embed }, ct : cancellationToken);

                if (!sendMessageResult.IsSuccess)
                {
                    throw new Exception(getChatChannelResult.Error?.Message ?? "Could not forward message to Discord.");
                }

                _logger.LogInformation("Server {id} changed status to {newStatus}", sanitisedId,
                                       msg.Status);
            }
            else
            {
                if (msg.Status == ServerStatus.Types.ServerStatusEnum.Started)
                {
                    _logger.LogWarning(
                        "Server {id} has unexpectedly sent ServerStatus message before sending ServerInfo message.",
                        sanitisedId);
                }
                else
                {
                    throw new KeyNotFoundException($"Could not find server {sanitisedId} in the list of online servers");
                }
            }
        }