예제 #1
0
        public async Task <bool> RemoveFameChannelAsync(IDiscordGuild dbGuild, IMessage msg)
        {
            try
            {
                if (msg.Author is SocketGuildUser guildUser)
                {
                    IGuild     guild   = guildUser.Guild;
                    IGuildUser botUser = await guild.GetCurrentUserAsync();

                    if (dbGuild.HasHallOfShames && botUser.GuildPermissions.ManageChannels)
                    {
                        IGuildChannel chan = await guild.GetChannelAsync(dbGuild.HallOfShameID);

                        if (chan != null)
                        {
                            await chan.DeleteAsync();
                        }
                        dbGuild.HasHallOfShames = false;
                        dbGuild.HallOfShameID   = 0;

                        return(true);
                    }
                }

                return(false);
            }
            catch (Exception ex)
            {
                this.Logger.Nice("Fame", ConsoleColor.Magenta, $"Could not disable a fame channel: {ex}");
                return(false);
            }
        }
예제 #2
0
        private static async Task UpdateVoiceChannels(SocketUser su, SocketVoiceState oldState, SocketVoiceState newState)
        {
            if (newState.VoiceChannel == oldState.VoiceChannel)
            {
                return;                                                 // The event can be triggered without changing channels
            }
            // Handle joining a channel
            if (newState.VoiceChannel != null && Utilities.VoiceWhitelist.check(newState.VoiceChannel.Guild.Id, newState.VoiceChannel.Name)) // Check to see if a channel was joined and that channel is not in the blacklist
            {
                IGuildChannel vc  = newState.VoiceChannel;                                                                                   //
                var           vct = Utilities.Voice.ProcessName(vc.Name);
                Console.WriteLine($"{su.Username}#{su.Discriminator} has joined {vc.Name}({vc.Id})");

                var users = (await vc.GetUsersAsync().FlattenAsync()).ToList();

                if (users.Count == 1) // If there is only one person in the group, it was previously empty, and another slot needs opened up
                {
                    var pos    = 0;
                    var number = 1;
                    var vcs    = await vc.Guild.GetVoiceChannelsAsync();      // Get a list of voice channels

                    var vcso = vcs.OrderBy(item => item.Position);            // Order the list of voice channels
                    foreach (var channel in vcso)                             // Loop over every channel
                    {
                        var chnl = Utilities.Voice.ProcessName(channel.Name); // Get the name and number from the channel
                        if (chnl.name != vct.name)
                        {
                            continue;                          // Check to see if the channel is of the same type
                        }
                        pos = Math.Max(channel.Position, pos); // If the channel is lower, save the new position
                        if (chnl.number != number)
                        {                                      // Is the channel number correct
                            await((SocketVoiceChannel)channel).ModifyAsync(x =>
                            {
                                // ReSharper disable once AccessToModifiedClosure
                                x.Name = chnl.name + " " + number; // Update the channel number
                            });
                        }
                        number++; // Increment the number that the channel is supposed to be
                    }

                    var rcpe = (await vc.Guild.GetVoiceChannelsAsync()).Select(channel => new ReorderChannelProperties(channel.Id, channel.Position + (channel.Position > pos ? 1 : 0))).ToList(); // Enumerable to store the new channel order


                    var nvc = await vc.Guild.CreateVoiceChannelAsync(vct.name + " " + number); // Create the channel

                    Console.WriteLine($"Created channel {nvc.Name}({nvc.Id})");
                    await nvc.ModifyAsync(x =>
                    {
                        x.CategoryId = ((INestedChannel)vc).CategoryId;
                        x.Position   = pos + 1; // Set the position of the channel
                    });

                    rcpe.Add(new ReorderChannelProperties(nvc.GuildId, pos + 1)); // Add the channel to the list of channels to reorder

                    await vc.Guild.ReorderChannelsAsync(rcpe);                    // Reorder the channels
                }
            }

            // Handle leaving a channel
            if (oldState.VoiceChannel != null && Utilities.VoiceWhitelist.check(oldState.VoiceChannel.Guild.Id, oldState.VoiceChannel.Name))
            {
                IGuildChannel vc  = oldState.VoiceChannel;
                var           vct = Utilities.Voice.ProcessName(vc.Name);
                Console.WriteLine($"{su.Username}#{su.Discriminator} has left {vc.Name}({vc.Id})");

                var users = (await vc.GetUsersAsync().FlattenAsync()).ToList();

                if (!users.Any()) // If there are no users in the channel
                {
                    Console.WriteLine($"Removing empty channel {vc.Name}({vc.Id})");
                    await vc.DeleteAsync(); // Delete it

                    var number = 1;
                    var vcs    = await vc.Guild.GetVoiceChannelsAsync();

                    var vcso = vcs.OrderBy((item) => item.Position);

                    // This functions almost identically to the number adjustment in the join
                    foreach (var channel in vcso)
                    {
                        var chnl = Utilities.Voice.ProcessName(channel.Name);
                        if (chnl.name != vct.name)
                        {
                            continue;
                        }
                        if (chnl.number == vct.number)
                        {
                            continue;                            // Because the deleted channel is still in the list, skip it
                        }
                        if (chnl.number != number)
                        {
                            await((SocketVoiceChannel)channel).ModifyAsync(x =>
                            {
                                // ReSharper disable once AccessToModifiedClosure
                                x.Name = chnl.name + " " + number;
                            });
                        }
                        number++;
                    }
                }
            }
        }