Beispiel #1
0
        public static async Task Run(SocketSlashCommand command, DiscordSocketClient client, StorageClient storageClient, Dictionary <string, SocketSlashCommandDataOption> options, Guild guild, ISocketMessageChannel recruitingChannel, List <Team> teams)
        {
            var guildUser   = (SocketGuildUser)options["username"].Value;
            var discordUser = guildUser.Nickname ?? guildUser.Username;
            var teamName    = options["team"].Value.ToString();
            var captain     = options.ContainsKey("captain") && (bool)options["captain"].Value;

            // Player not exist? -> respond with error
            (var oldTeam, var player) = Team.FindPlayer(teams, discordUser);
            if (player == null)
            {
                await command.FollowupAsync($"User {discordUser} does not exist in the recruiting table", ephemeral : true);

                return;
            }

            oldTeam.RemovePlayer(player);
            var  newTeam   = Team.AddPlayer(teams, teamName, player, captain);
            bool isNewTeam = newTeam.MsgId == 0;

            // Update old team message
            if (oldTeam.Players.Count > 0)
            {
                await recruitingChannel.ModifyMessageAsync(oldTeam.MsgId, (message) => message.Content = oldTeam.ToMessage());
            }
            else
            {
                await recruitingChannel.DeleteMessageAsync(oldTeam.MsgId);
            }

            // Update new team message
            if (newTeam.MsgId == 0)
            {
                newTeam.MsgId = (await recruitingChannel.SendMessageAsync(newTeam.ToMessage())).Id;
            }
            else
            {
                await recruitingChannel.ModifyMessageAsync(newTeam.MsgId, (message) => message.Content = newTeam.ToMessage());
            }

            var transactions = new List <(string, TableTransactionActionType, Team, ETag)>();

            if (oldTeam.Players.Count > 0)
            {
                transactions.Add((oldTeam.Name, TableTransactionActionType.UpdateMerge, oldTeam, oldTeam.etag));
            }
            else
            {
                transactions.Add((oldTeam.Name, TableTransactionActionType.Delete, null, oldTeam.etag));
            }

            if (!string.Equals(newTeam.Name, oldTeam.Name, StringComparison.OrdinalIgnoreCase))
            {
                transactions.Add((newTeam.Name, isNewTeam ? TableTransactionActionType.UpsertMerge : TableTransactionActionType.UpdateMerge, newTeam, newTeam.etag));
            }

            // if the transaction fails it should retry, and then the message will be updated to reflect the actual value in storage.
            await storageClient.ExecuteTransaction(Team.TableName, transactions, guild.RowKey);

            await command.FollowupAsync($"You have moved user {discordUser} from {oldTeam.Name} -> {newTeam.Name}", ephemeral : true);
        }