コード例 #1
0
ファイル: Channel.cs プロジェクト: vasyaabr/Discord.Net
        /// <summary> Edits this channel, changing only non-null attributes. </summary>
        public async Task Edit(string name = null, string topic = null, int?position = null)
        {
            if (name != null || topic != null)
            {
                var request = new UpdateChannelRequest(Id)
                {
                    Name     = name ?? Name,
                    Topic    = topic ?? Topic,
                    Position = Position
                };
                await Client.ClientAPI.Send(request).ConfigureAwait(false);
            }

            if (position != null)
            {
                Channel[] channels      = Server.AllChannels.Where(x => x.Type == Type).OrderBy(x => x.Position).ToArray();
                int       oldPos        = Array.IndexOf(channels, this);
                var       newPosChannel = channels.Where(x => x.Position > position).FirstOrDefault();
                int       newPos        = (newPosChannel != null ? Array.IndexOf(channels, newPosChannel) : channels.Length) - 1;
                if (newPos < 0)
                {
                    newPos = 0;
                }
                int minPos;

                if (oldPos < newPos) //Moving Down
                {
                    minPos = oldPos;
                    for (int i = oldPos; i < newPos; i++)
                    {
                        channels[i] = channels[i + 1];
                    }
                    channels[newPos] = this;
                }
                else //(oldPos > newPos) Moving Up
                {
                    minPos = newPos;
                    for (int i = oldPos; i > newPos; i--)
                    {
                        channels[i] = channels[i - 1];
                    }
                    channels[newPos] = this;
                }
                Channel after = minPos > 0 ? channels.Skip(minPos - 1).FirstOrDefault() : null;
                await Server.ReorderChannels(channels.Skip(minPos), after).ConfigureAwait(false);
            }
        }