Beispiel #1
0
        /// <summary>
        /// Searches for a channel and returns the Channel Id
        /// TODO:
        /// - Generalize to use with any Channel
        /// -
        /// </summary>
        /// <param name="ctx"></param>
        /// <returns></returns>
        private async Task <ulong> FindChannel(CommandContext ctx, string channelName)
        {
            await ctx.Member.SendMessageAsync("test2");

            DiscordChannel hnCh = null;

            if (hawkDict == null)
            {
                try
                {
                    hawkDict = new SynchronizedDictionary <ulong, ulong>(ConfigLoader.Config.ChannelDict[channelName]);
                }
                catch (AggregateException e)
                {
                    await ctx.Member.SendMessageAsync(e.Message).ConfigureAwait(false);

                    await ctx.Member.SendMessageAsync(e.StackTrace).ConfigureAwait(false);

                    hawkDict = new SynchronizedDictionary <ulong, ulong>();
                }
                catch (Exception e)
                {
                    await ctx.Member.SendMessageAsync(e.Message).ConfigureAwait(false);

                    await ctx.Member.SendMessageAsync(e.StackTrace).ConfigureAwait(false);

                    hawkDict = new SynchronizedDictionary <ulong, ulong>();
                }
            }


            // If check if there is a channel Id in the dictionary first.
            // Attempt to get the channel. Return Channel Id if successfully retrieved from Guild.
            ulong result;

            if (hawkDict.Read(ctx.Guild.Id, out result))
            {
                if (ctx.Guild.GetChannel(result) != null)
                {
                    return(result);
                }
            }
            // Search for Channel named Hawk Nest and add to dictionary if found.
            bool chExists = false;
            var  chList   = await ctx.Guild.GetChannelsAsync();

            foreach (var ch in chList)
            {
                if (ch.Name.ToLower().Equals(channelName.ToLower()))
                {
                    chExists = true;
                    hawkDict.Add(ctx.Guild.Id, ch.Id);
                    hnCh = ch;
                    break;
                }
            }
            // If Channel not found. Create it then add it to the dictionary.
            if (!chExists)
            {
                hnCh = await CreateVCChannel(ctx, channelName);

                hawkDict.Add(ctx.Guild.Id, hnCh.Id);
            }
            // Save the updated dictionary then return Channel Id
            ConfigLoader.Config.ChannelDict[channelName] = hawkDict.SyncDict;
            ConfigLoader.Config.SaveConfig();
            if (hnCh == null)
            {
                return(0);
            }
            return(hnCh.Id);
        }