Beispiel #1
0
        public async Task LinkAccountAsync(string command, [Optional][Remainder] string id)
        {
            var guildUser = _dataService.GetSocketGuildUser(Context.User.Id);

            switch (command.ToLower())
            {
            case "add":
                await AddUser();

                break;

            case "delete":
                if (id == null)
                {
                    await DeleteContextUser();
                }
                else if (guildUser.Roles.Any(x =>
                                             x.Id == _dataService.ModeratorRole.Id || x.Id == _dataService.AdminRole.Id) &&
                         id != null)
                {
                    await DeleteTargetUser();
                }
                else
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("You don't have the right permissions for that.")
                                     .WithColor(165, 55, 55).Build());
                }
                break;

            case "get":
                if (id == null)
                {
                    await GetLinked();
                }
                else if (guildUser.Roles.Any(x =>
                                             x.Id == _dataService.ModeratorRole.Id || x.Id == _dataService.AdminRole.Id) &&
                         id != null)
                {
                    await GetTargetLinked();
                }
                else
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("You don't have the right permissions for that.")
                                     .WithColor(165, 55, 55).Build());
                }
                break;

            default:
                await ReplyAsync(embed : new EmbedBuilder()
                                 .WithAuthor("Unknown command")
                                 .WithDescription("See `>help link`")
                                 .WithColor(165, 55, 55).Build());

                break;
            }

            async Task DeleteTargetUser()
            {
                UserSteamID returnedUser = null;
                var         steamIdRegex = new Regex(@"(STEAM_[\d]:[\d]:\d+)");
                var         target       = id;

                if (steamIdRegex.IsMatch(id))
                {
                    returnedUser = DatabaseUtil.GetUserSteamID(steamId: steamIdRegex.Match(id).Value);
                }
                else
                {
                    var targetUser = _dataService.GetSocketGuildUser(id);
                    returnedUser = DatabaseUtil.GetUserSteamID(targetUser.Id);
                    target       = targetUser.ToString();
                }

                if (returnedUser == null)
                {
                    await DisplayNoLinkFound(target);

                    return;
                }

                var deleteResult = DatabaseUtil.DeleteUserSteamID(returnedUser);

                if (deleteResult)
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Link deleted!")
                                     .WithDescription(
                                         $"`{_dataService.GetSocketGuildUser(returnedUser.UserId)}` is **no longer** linked to `{returnedUser.SteamID}`")
                                     .WithColor(165, 55, 55).Build());
                    await ReplyAsync();
                }
                else
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Failed to delete link")
                                     .WithColor(165, 55, 55).Build());
                }
            }

            async Task DeleteContextUser()
            {
                var returnedUser = DatabaseUtil.GetUserSteamID(Context.User.Id);

                if (returnedUser == null)
                {
                    await DisplayNoLinkFound(Context.User.ToString());

                    return;
                }

                var deleteResult = DatabaseUtil.DeleteUserSteamID(returnedUser);

                if (deleteResult)
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Link deleted!")
                                     .WithDescription($"`{Context.User}` is **no longer** linked to `{returnedUser.SteamID}`")
                                     .WithColor(165, 55, 55).Build());
                }
                else
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Failed to delete link")
                                     .WithColor(165, 55, 55).Build());
                }
            }

            async Task AddUser()
            {
                if (id == null)
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("ID cannot be empty")
                                     .WithColor(165, 55, 55).Build());

                    return;
                }

                var steamIdRegex = new Regex(@"(STEAM_[\d]:[\d]:\d+)");

                if (!steamIdRegex.IsMatch(id))
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Invalid SteamID format")
                                     .WithDescription("Example: `STEAM_1:1:11383969`")
                                     .WithColor(165, 55, 55).Build());

                    return;
                }

                var parsedID = steamIdRegex.Match(id).Value;

                var result = DatabaseUtil.AddUserSteamID(new UserSteamID
                {
                    UserId  = Context.User.Id,
                    SteamID = parsedID
                });

                if (result)
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Link added!")
                                     .WithDescription($"`{Context.User}` is linked to `{parsedID}`")
                                     .WithColor(55, 165, 55).Build());
                }
                else
                {
                    await ReplyAsync(embed : new EmbedBuilder()
                                     .WithAuthor("Unable to add link")
                                     .WithColor(165, 55, 55).Build());
                }
            }

            async Task GetLinked()
            {
                var userSteam = DatabaseUtil.GetUserSteamID(Context.User.Id);

                if (userSteam == null)
                {
                    await DisplayNoLinkFound(Context.User.ToString());

                    return;
                }

                await ReplyAsync(embed : new EmbedBuilder()
                                 .WithAuthor("Link found")
                                 .WithDescription($"`{Context.User}` is linked to `{userSteam.SteamID}`")
                                 .WithColor(55, 55, 165).Build());
            }

            async Task GetTargetLinked()
            {
                UserSteamID returnedUser = null;
                var         steamIdRegex = new Regex(@"(STEAM_[\d]:[\d]:\d+)");
                var         target       = id;

                if (steamIdRegex.IsMatch(id))
                {
                    returnedUser = DatabaseUtil.GetUserSteamID(steamId: steamIdRegex.Match(id).Value);
                }
                else
                {
                    var targetUser = _dataService.GetSocketGuildUser(id);
                    returnedUser = DatabaseUtil.GetUserSteamID(targetUser.Id);
                    target       = targetUser.ToString();
                }

                if (returnedUser == null)
                {
                    await DisplayNoLinkFound(target);

                    return;
                }

                await ReplyAsync(embed : new EmbedBuilder()
                                 .WithAuthor("Link found")
                                 .WithDescription(
                                     $"`{_dataService.GetSocketGuildUser(returnedUser.UserId)}` is linked to `{returnedUser.SteamID}`")
                                 .WithColor(55, 55, 165).Build());
            }

            async Task DisplayNoLinkFound(string target)
            {
                await ReplyAsync(embed : new EmbedBuilder()
                                 .WithAuthor($"No link found for {target}!")
                                 .WithColor(165, 55, 55).Build());
            }
        }