/// <summary> /// Retrieve server of the timer /// </summary> /// <param name="timer">Timer</param> /// <returns></returns> internal DiscordGuild GetServer(MyTestTimer timer) { foreach (var srvr in Settings) { if (srvr.Value.Timer == timer) { return(srvr.Key); } } return(null); }
public async Task MyTestStart(CommandContext commandContext, [Description("Канал вывода сообщений")] DiscordChannel channelInfoMessages = null) { channelInfoMessages = channelInfoMessages == null ? commandContext.Channel : channelInfoMessages; // 1. Get loaded test TestState test; try { test = TestLoaded[commandContext.Guild];; } catch (Exception e) { commandContext.Channel.SendMessageAsync($"{e.Message}\n{e.StackTrace}"); await commandContext.Message.CreateReactionAsync(MePhItBot.Bot.ReactFail); return; } CmdMyTestSettings settings; if (!Settings.TryGetValue(commandContext.Guild, out settings) || settings.TestState == null) { await commandContext.Message.CreateReactionAsync(MePhItBot.Bot.ReactFail); return; } settings.Channel = channelInfoMessages; // 2. Get a list of students online var students = await GetStudentsOnlineAsync(commandContext.Guild); if (students.Count() == 0) { commandContext.Message.CreateReactionAsync(BotSettings.EmojiReactFail); return; } foreach (var student in students) { Settings[commandContext.Guild].TestState[student as DiscordMember] = test.Clone() as TestState; } // 3. Create test channels personal for each student // Permission to read only for this specific student // 3.1. Create MyTest channel category // 3.2. Create text channels with names corresponding to student's displayed name // 3.3 Store created temporary channels (Settings[commandContext.Guild].TempTestChannelGrpCategoryId, Settings[commandContext.Guild].TempTestChannelGrp) = await CreateTemporaryTestChannelGroup(commandContext, students); var tempChannels = Settings[commandContext.Guild].TempTestChannelGrp; // 3.4. Inform students to join their corresponding channels var channelMentions = ""; foreach (var mention in GetChannelMentions(tempChannels.Values)) { channelMentions += mention; } // Generic test info var msg = string.Format( Localization.Message(commandContext.Guild, MessageID.CmdMyTestStartHelp), channelMentions, BotSettings.Discord.CurrentUser.Mention, commandTestFinish, timeoutBeforeTestMinutes ) + "\n"; await channelInfoMessages.SendMessageAsync($"{commandContext.Guild.EveryoneRole.Mention}\n{msg}"); // Test name and time msg = $"{EmojiInfo} {test.Name}\n"; var testTime = test.Time != TestState.TimeInfinite ? new TimeSpan(0, 0, test.Time).TotalMinutes.ToString() : "∞"; msg += string.Format(Localization.Message(commandContext.Guild, MessageID.CmdMyTestStartTime), testTime) + "\n"; foreach (var tempChannel in tempChannels) { tempChannel.Value.SendMessageAsync($"{tempChannel.Key.Mention}\n{msg}"); } // 4. Register callback for test start/stop event try { #if !DEBUG var mtTimer = new MyTestTimer(this, new TimeSpan(0, timeoutBeforeTestMinutes, 0).TotalMilliseconds); #elif DEBUG var mtTimer = new MyTestTimer(this, new TimeSpan(0, 0, timeoutBeforeTestMinutes).TotalMilliseconds); #endif Settings[commandContext.Guild].Timer = mtTimer; mtTimer.Elapsed += Timer_Elapsed; mtTimer.Start(); } catch (Exception e) { channelInfoMessages.SendMessageAsync($"{EmojiError} {e.Message}"); return; } // 5. Register test start to enable the *ready* command from the student commandContext.Message.CreateReactionAsync(Bot.ReactSuccess); }