/// <summary> /// Setup the client and assign methods /// </summary> public async Task Client_Setup() { #if !DEBUG await Client.LoginAsync(TokenType.Bot, Config.Instance.General.ReleaseBotKey); // RELEASE #else await Client.LoginAsync(TokenType.Bot, Config.Instance.General.TestBotKey); // TEST #endif await Client.StartAsync(); // Call method to configure client once it is ready Client.Ready += Client_Ready; Client.Disconnected += Client_Disconected; // Handle incoming messages DmHandler dmHandler = new DmHandler(); Client.MessageReceived += async message => { ChannelMaster master = ChannelMasters.Where(x => x.Channel.Id == message.Channel.Id).SingleOrDefault(); if (master != null) { if (message.Author.Id != Client.CurrentUser.Id) { Writer.Log("received message from " + message.Author + ": " + message.Content); } await master.Process(Command.FormCommand(message)); Config.Instance.Write(); } else { if ((message.Channel.GetType() == typeof(SocketDMChannel)) && (message.Author.Id != Client.CurrentUser.Id)) { Writer.Log("received dm from " + message.Author + ": " + message.Content); await dmHandler.Handle(Command.FormCommand(message)); } } }; // Start a timer to clean up the channels periodically CleanTimer = new Timer(TimeSpan.FromMinutes(5).TotalMilliseconds); CleanTimer.Elapsed += OnCleanEvent; CleanTimer.AutoReset = true; CleanTimer.Enabled = true; }
/// <summary> /// Start a channel master for a specific channel in discord /// </summary> /// <param name="channelconfig">The representation of a channel in discord</param> /// <param name="channel">If you already have an instance of the channel master, you can pass it as a parameter</param> /// <returns>A bool depicting if the channel handler could be started</returns> public async static Task <bool> StartChannelMaster(ChannelConfig channelconfig, IMessageChannel channel = null) { // Try to get the channel object from the channel id in the config if (channel == null) { channel = Client.GetChannel(channelconfig.ChannelId) as IMessageChannel; } else { channelconfig = Config.Instance.ChannelConfigs.Where(x => x.ChannelId == channel.Id).Single(); } // Create a handler for each type of message the channel should be able to process var handlers = new List <IMessageHandler> { new WelcomeHandler(channelconfig), new AttendanceHandler(channelconfig), new BorderHandler(channelconfig), new ScheduleHandler(channelconfig), new AnnouncementHandler(channelconfig) }; // If the client couldn't get the channel, return false if (channel == null) { Config.Instance.DeleteConfig(channelconfig); } // Get all the messages that have been posted to the channel, remove any that have been deleted Dictionary <CommandType, IUserMessage> postedMessages = new Dictionary <CommandType, IUserMessage>(); if (channelconfig.ChannelData.PostedMessages == null) { channelconfig.ChannelData.PostedMessages = new Dictionary <CommandType, ulong>(); } // Check if a message has been deleted List <CommandType> toRemove = new List <CommandType>(); foreach (var m in channelconfig.ChannelData.PostedMessages) { IUserMessage userMessage = null; try { userMessage = await channel.GetMessageAsync(m.Value) as IUserMessage; } catch (Exception) { } if (userMessage == null) { toRemove.Add(m.Key); } else { postedMessages.Add(m.Key, userMessage); } } foreach (var type in toRemove) { channelconfig.ChannelData.PostedMessages.Remove(type); } Config.Instance.Write(); // If a handler doesn't have a message posted in the channel, post it's default message foreach (var h in handlers) { try { if (postedMessages.Where(x => x.Key == h.MessageType).Count() == 0) { IUserMessage userMessage = await channel.SendMessageAsync(h.DefaultMessage()); postedMessages.Add(h.MessageType, userMessage); channelconfig.ChannelData.PostedMessages.Add(h.MessageType, userMessage.Id); } } catch (Exception) { } } Config.Instance.Write(); // Start a channel handler ChannelMaster master = new ChannelMaster(Client.CurrentUser.Id, channel, handlers, postedMessages); ChannelMasters.Add(master); await Task.Run(() => master.Run()); return(true); }