Ejemplo n.º 1
0
        public async Task <BridgeUser> LoadUser(User user)
        {
            BridgeUser player = this[user];

            if (player == null)
            {
                if (_main.Config.RememberLogins)
                {
                    return(await _main.Logins.Authenticate(user.Id));
                }
                else
                {
                    return(this[user] = new BridgeUser(user));
                }
            }

            return(player);
        }
Ejemplo n.º 2
0
        private async void onMessageReceived(object sender, MessageEventArgs e)
        {
            if (e.Message.IsAuthor)
            {
                return;
            }

            try
            {
                // Ignore commands
                if (e.Message.IsMentioningMe() || e.Message.Text.StartsWith(_main.Config.BotPrefix.ToString()))
                {
                    return;
                }

                // We don't need to process attachments; in fact, this crashes the server
                if (e.Message.Attachments.Length > 0 || e.Message.Embeds.Length > 0)
                {
                    return;
                }

                if (e.Channel.IsPrivate)
                {
                    if (e.User.IsBot && _main.Config.ServerBots.Exists(b => b.Id == e.User.Id))
                    {
                        // Message is Multi-Server Broadcast
                        TSPlayer.All.SendMessage(e.Message.Text, _main.ChatHandler.ChatColorOverride ?? Color.White);

                        // Strip tags when sending to console
                        TSPlayer.Server.SendMessage(e.Message.Text.StripTags(), _main.ChatHandler.ChatColorOverride ?? Color.White);
                    }
                }

                // We don't want to broadcast what other bots are saying; when we do, we use multi-server chat
                else if (e.User.IsBot)
                {
                    // If the channel is a Terraria channel, log the message
                    if (_main.Config.TerrariaChannels.Contains(e.Channel.Name))
                    {
                        Log.Info(e.Channel.Name, $"{e.User.Name}> {e.Message.Text}");
                    }

                    return;
                }

                else
                {
                    // Only broadcast non-self messages sent in game channels
                    if (_main.Config.TerrariaChannels.Contains(e.Channel.Name))
                    {
                        if (!String.IsNullOrWhiteSpace(_main.Config.MinimumRoleToBroadcast))
                        {
                            // Check for talk permission based on role
                            Role role = e.Server.FindRoles(_main.Config.MinimumRoleToBroadcast, true).FirstOrDefault();

                            // If role is null, incorrect setup, so disregard (should probably notify the log, however...)
                            if (role != null && !e.User.Roles.Contains(role))
                            {
                                // If the user doesn't have the set role, check role positions
                                int highestRolePosition = e.User.Roles.OrderBy(r => r.Position).Last().Position;
                                if (highestRolePosition < role.Position)
                                {
                                    // Todo: notify the user that their messages are not being broadcasted, but only nag them once
                                    return;
                                }
                            }
                        }

                        Role  topRole   = e.User.Roles.OrderBy(r => r.Position).Last();
                        Color roleColor = topRole.IsEveryone ? Color.Gray : new Color(topRole.Color.R, topRole.Color.G, topRole.Color.B);

                        BridgeUser player = await LoadUser(e.User);

                        // Setting up the color dictionary - if there is need for more colors, they may be added later
                        var colorDictionary = new Dictionary <string, Color?>
                        {
                            ["Role"]  = roleColor,
                            ["Group"] = player.IsLoggedIn ? new Color(player.Group.R, player.Group.G, player.Group.B) : roleColor,
                        };

                        #region Format Colors

                        var roleName = new ChatMessage.Section(topRole.IsEveryone ? _main.Config.DefaultRoleName : topRole.Name);
                        if (_main.Config.Broadcast.Colors.Role == DiscordBroadcastColor.Role || _main.Config.Broadcast.Colors.Role == DiscordBroadcastColor.Group)
                        {
                            roleName.Color = colorDictionary[_main.Config.Broadcast.Colors.Role.ToString()];
                        }

                        var name = new ChatMessage.Section(e.User.Name);
                        if (_main.Config.Broadcast.Colors.Name == DiscordBroadcastColor.Role || _main.Config.Broadcast.Colors.Name == DiscordBroadcastColor.Group)
                        {
                            name.Color = colorDictionary[_main.Config.Broadcast.Colors.Name.ToString()];
                        }

                        var nick = new ChatMessage.Section(String.IsNullOrWhiteSpace(e.User.Nickname) ? e.User.Name : e.User.Nickname);
                        if (_main.Config.Broadcast.Colors.Nickname == DiscordBroadcastColor.Role || _main.Config.Broadcast.Colors.Nickname == DiscordBroadcastColor.Group)
                        {
                            nick.Color = colorDictionary[_main.Config.Broadcast.Colors.Nickname.ToString()];
                        }

                        string text = e.Message.Text;

                        #endregion

                        string msg = String.Format(_main.Config.Broadcast.Format.ParseColors(colorDictionary),
                                                   roleName, name, nick, text);

                        TSPlayer.All.SendMessage(msg, _main.ChatHandler.ChatColorOverride ?? Color.White);

                        // Strip tags when sending to console
                        TSPlayer.Server.SendMessage(msg.StripTags(), _main.ChatHandler.ChatColorOverride ?? Color.White);
                    }

                    Log.Info(e.Channel.Name, $"Discord> {e.User.Name}: {e.Message.Text}");
                }
            }
            catch (Exception ex)
            {
                Log.Error("OnMessageReceived", ex.Message, ex);
            }
        }