示例#1
0
        public async Task Rename([Name("Queue name")] string queueName, [Name("New name")] string newName)
        {
            if (!PickupHelpers.IsInPickupChannel((IGuildChannel)Context.Channel))
            {
                return;
            }

            queueName = queueName.Trim(' ', '"').Trim();
            newName   = newName.Trim(' ', '"').Trim();

            var queue = await _miscCommandService.VerifyQueueByName(queueName, (IGuildChannel)Context.Channel);

            if (queue == null)
            {
                return;
            }

            var isAdmin = (Context.User as IGuildUser)?.GuildPermissions.Has(GuildPermission.Administrator) ?? false;

            if (isAdmin || queue.OwnerId == Context.User.Id.ToString())
            {
                var newQueueCheck = await _queueRepository.FindQueue(newName, Context.Guild.Id.ToString());

                if (newQueueCheck != null)
                {
                    await Context.Message.ReplyAsync($"`A queue with the name '{newName}' already exists.`").AutoRemoveMessage(10);

                    return;
                }

                var newQueue = (PickupQueue)queue.Clone();
                newQueue.RowKey = newName.ToLowerInvariant();
                newQueue.Name   = newName;

                var result = await _queueRepository.AddQueue(newQueue);

                if (result)
                {
                    await _queueRepository.RemoveQueue(queue);

                    await Context.Message.ReplyAsync($"The queue '{queue.Name}' has been renamed to '{newQueue.Name}'");

                    await Context.Message.ReplyAsync($"`{newQueue.Name} - {PickupHelpers.ParseSubscribers(newQueue)}`");

                    if (!string.IsNullOrEmpty(queue.StaticMessageId))
                    {
                        await _listCommandService.SaveStaticQueueMessage(newQueue, Context.Guild);
                    }
                    return;
                }

                await Context.Message.ReplyAsync("An error occured when trying to update the queue name, try again.")
                .AutoRemoveMessage(10);
            }
            else
            {
                await Context.Message.ReplyAsync("`You do not have permission to rename this queue, you have to be either the owner or a server admin`")
                .AutoRemoveMessage(10);
            }
        }
示例#2
0
        public async Task Add(string queueName, ISocketMessageChannel channel, IGuildUser user)
        {
            var         guild = (SocketGuild)user.Guild;
            PickupQueue queue;

            if (!string.IsNullOrWhiteSpace(queueName))
            {
                queue = await _queueRepository.FindQueue(queueName, guild.Id.ToString());
            }
            else
            {
                var queues = (await _queueRepository.AllQueues(guild.Id.ToString())).OrderByDescending(w => w.Readiness).ToList();
                queue = queues.Any(q => q.Readiness < 100) ? queues.FirstOrDefault(q => q.Readiness < 100) : queues.FirstOrDefault();
            }

            if (queue == null)
            {
                await channel.SendMessageAsync($"`Queue with the name '{queueName}' doesn't exists!`").AutoRemoveMessage(10);

                return;
            }

            if (!await _miscCommandService.VerifyUserFlaggedStatus(user, channel))
            {
                return;
            }

            if (queue.Subscribers.Any(w => w.Id == user.Id))
            {
                await channel.SendMessageAsync($"`{queue.Name} - {PickupHelpers.ParseSubscribers(queue)}`");

                return;
            }

            var activity = await _activitiesRepository.Find(user);

            activity.PickupAdd += 1;

            queue.Updated = DateTime.UtcNow;

            bool updateStaticMessage = false;

            if (queue.Subscribers.Count >= queue.MaxInQueue)
            {
                if (queue.WaitingList.All(w => w.Id != user.Id))
                {
                    queue.WaitingList.Add(new Subscriber {
                        Id = user.Id, Name = PickupHelpers.GetNickname(user)
                    });
                    updateStaticMessage = true;

                    if (channel.Name != ChannelNames.ActivePickups)
                    {
                        await channel.SendMessageAsync($"`You have been added to the '{queue.Name}' waiting list`").AutoRemoveMessage(10);
                    }
                }
                else
                {
                    if (channel.Name != ChannelNames.ActivePickups)
                    {
                        await channel.SendMessageAsync($"`You are already on the '{queue.Name}' waiting list`").AutoRemoveMessage(10);
                    }
                }
            }
            else
            {
                updateStaticMessage = true;
                queue.Subscribers.Add(new Subscriber {
                    Id = user.Id, Name = PickupHelpers.GetNickname(user)
                });

                if (queue.Subscribers.Count == queue.MaxInQueue)
                {
                    await PickupHelpers.NotifyUsers(
                        queue,
                        guild.Name,
                        user,
                        queue.Subscribers.Select(s => guild.GetUser(s.Id)).Where(u => u != null).ToArray());
                }

                if (channel.Name != ChannelNames.ActivePickups)
                {
                    await channel.SendMessageAsync($"`{queue.Name} - {PickupHelpers.ParseSubscribers(queue)}`");
                }
            }

            if (updateStaticMessage)
            {
                queue = await _listCommandService.SaveStaticQueueMessage(queue, guild);
            }

            await _queueRepository.UpdateQueue(queue);

            await _activitiesRepository.Update(activity);
        }