示例#1
0
        private static Task OnMessageReceived(SocketMessage args)
        {
            if (!Enabled || AwaitingConnection)
            {
                return(Task.CompletedTask);
            }
            if (string.IsNullOrWhiteSpace(args.Content))
            {
                return(Task.CompletedTask);
            }
            if (args.Author.IsBot)
            {
                return(Task.CompletedTask);
            }
            if (DiscordMain.Config.IgnoredDiscordIDs.Contains(args.Author.Id))
            {
                return(Task.CompletedTask);
            }

            //Metadata part 1
            bool isDirectMessage = args.Channel is IDMChannel;
            bool isCommand       = isDirectMessage || args.Content.StartsWith(DiscordMain.Config.BotPrefix);
            bool isInMainChannel = args.Channel.Id == DiscordMain.Config.ChannelID;

            //Ignore messages that aren't commands or from main chat channel
            if (!isCommand && !isInMainChannel)
            {
                return(Task.CompletedTask);
            }

            //Metadata part 2
            int tShockUserId = DB.GetTShockID(args.Author.Id);
            var guild        = client.GetGuild(DiscordMain.Config.GuildID);
            var discordUser  = guild.GetUser(args.Author.Id);
            var tshockUser   = TShock.Users.GetUserByID(tShockUserId);
            var tshockGroup  = TShock.Groups.GetGroupByName(tshockUser != null ? tshockUser.Group : TShock.Config.DefaultGuestGroupName);

            //If someone DMs bot without being in guild
            if (discordUser == null)
            {
                return(Task.CompletedTask);
            }

            // Broadcast chat messages
            if (!isCommand)
            {
                TShock.Utils.Broadcast($"(Discord) {tshockGroup.Prefix}{GetName(args.Author.Id)}: {args.Content.ParseText()}", tshockGroup.R, tshockGroup.G, tshockGroup.B);
                return(Task.CompletedTask);
            }

            string        commandText       = args.Content.StartsWith(DiscordMain.Config.BotPrefix) ? args.Content.Substring(DiscordMain.Config.BotPrefix.Length).ParseText() : args.Content.ParseText();
            List <string> commandParameters = commandText.ParseParameters();

            //Override certain commands for Discord use
            switch (commandParameters[0].ToLower())
            {
            case "login":
                if (isInMainChannel)
                {
                    //Try to delete message, if possible.
                    try
                    {
                        args.DeleteAsync();
                    }
                    catch { }
                    args.Channel.SendMessageAsync("```You can only login via Direct Message with me!```");
                    return(Task.CompletedTask);
                }
                if (tshockUser != null)
                {
                    args.Channel.SendMessageAsync("```You are already logged in!```");
                    return(Task.CompletedTask);
                }
                if (commandParameters.Count != 3)
                {
                    args.Channel.SendMessageAsync("```Invalid syntax: login \"username\" <password>```");
                    return(Task.CompletedTask);
                }
                var newTshockUser = TShock.Users.GetUserByName(commandParameters[1]);
                if (newTshockUser == null || !newTshockUser.VerifyPassword(commandParameters[2]))
                {
                    args.Channel.SendMessageAsync("```Invalid username or password.```");
                    return(Task.CompletedTask);
                }
                DB.AddTShockUser(args.Author.Id, newTshockUser.ID);
                args.Channel.SendMessageAsync("```Login successful!```");
                break;

            case "logout":
                if (tshockUser == null)
                {
                    args.Channel.SendMessageAsync("```You are not logged in!```");
                    return(Task.CompletedTask);
                }
                DB.RemoveTShockUser(args.Author.Id);
                args.Channel.SendMessageAsync("```Logout successful!```");
                break;

            case "who":
            case "online":
            case "playing":
                args.Channel.SendMessageAsync($"```Active Players ({TShock.Utils.ActivePlayers()}/{TShock.Config.MaxSlots}):\n{string.Join(", ", TShock.Players.Where(e => e != null && e.Active).Select(e => e.Name))}```");
                break;

            case "me":
                if (commandParameters.Count > 0)
                {
                    TShock.Utils.Broadcast($"* {GetName(args.Author.Id)} {commandText.Substring(3)}", 205, 133, 63);
                }
                break;

            default:
                using (var player = new DiscordPlayer(GetName(args.Author.Id)))
                {
                    player.User  = tshockUser;
                    player.Group = tshockGroup;

                    if (!player.HasPermission("discord.commands"))
                    {
                        args.Channel.SendMessageAsync("```You do not have permission to use commands on Discord.```");
                        return(Task.CompletedTask);
                    }

                    var commands = Commands.ChatCommands.Where(c => c.HasAlias(commandParameters[0].ToLower()));
                    if (commands.Count() != 0)
                    {
                        if (Main.rand == null)
                        {
                            Main.rand = new UnifiedRandom();
                        }

                        foreach (var command in commands)
                        {
                            if (!command.CanRun(player))
                            {
                                args.Channel.SendMessageAsync("```You do not have access to this command.```");
                            }
                            else if (!command.AllowServer)
                            {
                                args.Channel.SendMessageAsync("```You cannot run this command from Discord.```");
                            }
                            else
                            {
                                command.Run(commandText, player, commandParameters.GetRange(1, commandParameters.Count - 1));
                                if (player.GetOutput().Count == 0)
                                {
                                    return(Task.CompletedTask);
                                }

                                args.Channel.SendMessageAsync($"```{string.Join("\n", player.GetOutput())}```");
                            }
                        }
                    }
                    else
                    {
                        args.Channel.SendMessageAsync("```Invalid command.```");
                    }
                }
                break;
            }

            return(Task.CompletedTask);
        }
示例#2
0
        private static Task OnMessageReceived(SocketMessage args)
        {
            if (!Enabled || AwaitingConnection)
            {
                return(Task.CompletedTask);
            }
            if (string.IsNullOrWhiteSpace(args.Content))
            {
                return(Task.CompletedTask);
            }

            if (args.Author.IsBot && !DiscordMain.Config.WhiteListedBotID.Contains(args.Author.Discriminator))
            {
                return(Task.CompletedTask);
            }
            if (DiscordMain.Config.IgnoredDiscordIDs.Contains(args.Author.Id))
            {
                return(Task.CompletedTask);
            }

            //Metadata part 1
            bool isDirectMessage = args.Channel is IDMChannel;
            bool isCommand       = isDirectMessage || args.Content.StartsWith(DiscordMain.Config.BotPrefix);
            bool isInMainChannel = args.Channel.Id == DiscordMain.Config.ChannelID;


            //Metadata part 2
            int tShockUserId = DB.GetTShockID(args.Author.Id);
            var guild        = client.GetGuild(DiscordMain.Config.GuildID);
            var discordUser  = guild.GetUser(args.Author.Id);
            var tshockUser   = TShock.Users.GetUserByID(tShockUserId);
            var tshockGroup  = TShock.Groups.GetGroupByName(tshockUser != null ? tshockUser.Group : TShock.Config.DefaultGuestGroupName);

            //Ignore messages that aren't commands or from main chat channel
            if (!isCommand && !isInMainChannel)
            {
                return(Task.CompletedTask);
            }

            // Broadcast chat messages
            if ((!isCommand) && !args.Author.IsBot)
            {
                TShock.Utils.Broadcast($"[Discord] {tshockGroup.Prefix}{GetName(args.Author.Id)}: {args.Content.ParseText()}", tshockGroup.R, tshockGroup.G, tshockGroup.B);
                return(Task.CompletedTask);
            }
            if ((!isCommand) && args.Author.IsBot && args.Author.Discriminator == "0000")
            {
                TShock.Utils.Broadcast($"[Messenger] {args.Author.Username}: {args.Content.ParseText().Replace("*", string.Empty)}", 0, 132, 255);
                return(Task.CompletedTask);
            }
            if ((!isCommand) && args.Author.IsBot && args.Author.Discriminator == "6404")
            {
                TShock.Utils.Broadcast($"[Messenger] {args.Content.ParseText().Replace("*", string.Empty)}", 0, 132, 255);
                return(Task.CompletedTask);
            }

            if (!isCommand && args.Author.IsBot && args.Author.Discriminator != DiscordMain.Config.botID && args.Author.Discriminator != "0000")
            {
                if (!isCommand && args.Author.Discriminator == "0234" && !args.Content.ParseText().Replace("*", string.Empty).Contains("has joined.` ``") && !args.Content.ParseText().Replace("*", string.Empty).Contains("has left.` ``"))
                {
                    TShock.Utils.Broadcast($"[Server 1] {args.Content.ParseText().Replace("*", string.Empty)}", 000, 230, 020);
                    return(Task.CompletedTask);
                }
                else if (args.Content.ParseText().Replace("*", string.Empty).Contains("has joined.` ``") || args.Content.ParseText().Replace("*", string.Empty).Contains("has left.` ``"))
                {
                    return(Task.CompletedTask);
                }
                if (!isCommand && args.Author.Discriminator == "0622" && !args.Content.ParseText().Replace("*", string.Empty).Contains("has joined.` ``") && !args.Content.ParseText().Replace("*", string.Empty).Contains("has left.` ``"))
                {
                    TShock.Utils.Broadcast($"[Server 2] {args.Content.ParseText().Replace("*", string.Empty)}", 000, 230, 020);
                    return(Task.CompletedTask);
                }
                else if (args.Content.ParseText().Replace("*", string.Empty).Contains("has joined.` ``") || args.Content.ParseText().Replace("*", string.Empty).Contains("has left.` ``"))
                {
                    return(Task.CompletedTask);
                }
            }
            if (args.Author.Discriminator == DiscordMain.Config.botID)
            {
                return(Task.CompletedTask);
            }

            //If someone DMs bot without being in guild
            if (discordUser == null && args.Author.Discriminator != "0000")
            {
                return(Task.CompletedTask);
            }



            string        commandText       = args.Content.StartsWith(DiscordMain.Config.BotPrefix) ? args.Content.Substring(DiscordMain.Config.BotPrefix.Length).ParseText() : args.Content.ParseText();
            List <string> commandParameters = commandText.ParseParameters();

            //Override certain commands for Discord use
            switch (commandParameters[0].ToLower())
            {
            case "login":
                if ((isInMainChannel) || !isDirectMessage)
                {
                    //Try to delete message, if possible.
                    try
                    {
                        args.DeleteAsync();
                    }
                    catch { }
                    if (args.Author.Discriminator != "0000")
                    {
                        args.Author.SendMessageAsync("**To login, type:** ```login \"teararia_account\" password```");
                        args.Channel.SendMessageAsync("You cannot login in public chat. Send the command to me via Direct Messages.");
                    }
                    else if (args.Author.Discriminator == "0000")
                    {
                        args.Channel.SendMessageAsync("```You cannot login via Messenger! Please use Discord.```");
                    }
                    return(Task.CompletedTask);
                }
                if (tshockUser != null)
                {
                    args.Channel.SendMessageAsync("```You are already logged in!```");
                    return(Task.CompletedTask);
                }
                if (commandParameters.Count != 3)
                {
                    args.Channel.SendMessageAsync("```Invalid syntax: login \"teararia_account\" <password>```");
                    return(Task.CompletedTask);
                }
                var newTshockUser = TShock.Users.GetUserByName(commandParameters[1]);
                if (newTshockUser == null || !newTshockUser.VerifyPassword(commandParameters[2]))
                {
                    args.Channel.SendMessageAsync("```Invalid username or password.```");
                    return(Task.CompletedTask);
                }
                DB.AddTShockUser(args.Author.Id, newTshockUser.ID);
                args.Channel.SendMessageAsync("```Login successful!```");
                break;

            case "logout":
                if (args.Author.Discriminator != "0000")
                {
                    if (tshockUser == null)
                    {
                        args.Channel.SendMessageAsync("```You are not logged in!```");
                        return(Task.CompletedTask);
                    }
                    DB.RemoveTShockUser(args.Author.Id);
                    args.Channel.SendMessageAsync("```Logout successful!```");
                }
                break;

            case "who":
            case "online":
            case "playing":
                args.Channel.SendMessageAsync($"```Active Players ({TShock.Utils.ActivePlayers()}/{TShock.Config.MaxSlots}):\n{string.Join(", ", TShock.Players.Where(e => e != null && e.Active).Select(e => e.Name))}```");
                break;

            case "me":
                if (commandParameters.Count > 0)
                {
                    TShock.Utils.Broadcast($"* {GetName(args.Author.Id)} {commandText.Substring(3)}", 205, 133, 63);
                }
                break;

            default:
                if (args.Author.Discriminator == "0000")
                {
                    args.Channel.SendMessageAsync("```You cannot use commands in Messenger due to Facebook being a dick. Please use Discord.```");
                    return(Task.CompletedTask);
                }
                using (var player = new DiscordPlayer(GetName(args.Author.Id)))
                {
                    player.User  = tshockUser;
                    player.Group = tshockGroup;

                    if (!player.HasPermission("discord.commands"))
                    {
                        args.Channel.SendMessageAsync("You're not logged-in with your TEARaria account yet. Check your inbox to know how.");
                        //args.Channel.SendMessageAsync("Send a Direct Message to Bot " + Discord.client.GetUser(305435658833494026).Mention + " with this syntax: `/login player_name password` to login.");
                        args.Author.SendMessageAsync("**To login, type:** ```login \"teararia_account\" password```");
                        return(Task.CompletedTask);
                    }

                    var commands = Commands.ChatCommands.Where(c => c.HasAlias(commandParameters[0].ToLower()));
                    if (commands.Count() != 0)
                    {
                        if (Main.rand == null)
                        {
                            Main.rand = new UnifiedRandom();
                        }

                        foreach (var command in commands)
                        {
                            if (!command.CanRun(player))
                            {
                                args.Channel.SendMessageAsync("```You do not have access to this command.```");
                            }
                            else if (!command.AllowServer)
                            {
                                args.Channel.SendMessageAsync("```This command is only available in-game.```");
                            }
                            else
                            {
                                command.Run(commandText, player, commandParameters.GetRange(1, commandParameters.Count - 1));
                                if (player.GetOutput().Count == 0)
                                {
                                    return(Task.CompletedTask);
                                }

                                args.Channel.SendMessageAsync($"```{string.Join("\n", player.GetOutput()).StripTags()}```");
                            }
                        }
                    }
                    else
                    {
                        args.Channel.SendMessageAsync("```Invalid command.```");
                    }
                }
                break;
            }

            return(Task.CompletedTask);
        }