public async Task AnalyseTime(SocketCommandContext Context) { using (var database = new GuildDB()) { // first make sure that the user has the correct permissions if (!PermissionHelper.UserHasPermission(Context.User as SocketGuildUser, PermissionHelper.Member, database)) { return; } DateTime now = DateTime.UtcNow; // check if the text contains a time indication TimeSpan?FoundTime = DateTimeMethods.StringToTime(Context.Message.Content); if (!FoundTime.HasValue) { return; } // check if the user has a timezone applied TimeZoneInfo tz = DateTimeMethods.UserToTimezone(Context.User); if (tz == null) { return; } // indicate that the bot is working on the answer await Context.Channel.TriggerTypingAsync(); var language = statecollection.GetLanguage(Context.Guild, database); // find the desired date time in the local time DateTime dt = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0) + FoundTime.Value; // print the table to the chat string result = DateTimeMethods.TimetableToString(DateTimeMethods.LocalTimeToTimetable(dt, tz, Context.Guild)); await Context.Channel.SendMessageAsync(language.GetString("scan.time.found")); await Context.Channel.TriggerTypingAsync(); await Context.Channel.SendMessageAsync(result); return; } }
private async Task Client_UserJoined(SocketGuildUser user) { using (var database = new GuildDB()) { // find the public channel of given guild var sc = statecollection.GetPublicChannel(user.Guild, database); if (sc == null) { return; } // add some delays to make Betty's response seem more natural await Task.Delay(10000); await sc.TriggerTypingAsync(); await Task.Delay(3000); var language = statecollection.GetLanguage(user.Guild, database); await sc.SendMessageAsync(language.GetString("event.join", new SentenceContext() .Add("mention", user.Mention))); } }
public CancellationTokenSource CreateWaiterTask(SocketGuild guild, SocketTextChannel channel, bool doNotifications = true, IEnumerable <TimedMessage> messages = null, Action <GuildDB> action = null) { // create tokensource for cancellation var tokensource = new CancellationTokenSource(); // create and run the task Task.Run(async() => { var token = tokensource.Token; if (messages != null && messages.Any()) { // wait for all notifications foreach (var m in messages.OrderBy(x => x.Date)) { // if cancellation is requested, break the loop if (token.IsCancellationRequested) { break; } // don't do notifications that should've happened in the past if (m.Date < DateTime.UtcNow) { continue; } try { // wait for the date of given message await DateTimeMethods.WaitForDate(m.Date, token); if (doNotifications && !token.IsCancellationRequested) { using (var database = new GuildDB()) { // send given message to discord var c = channel ?? statecollection.GetNotificationElsePublicChannel(guild, database); await c.SendMessageAsync(statecollection.GetLanguage(guild, database).GetString(m.Keyword, m.Context)); } } } catch (TaskCanceledException) { } catch (Exception e) { logger.Log(new LogMessage(LogSeverity.Warning, "Notifier", $"Attempted to run notifier, but failed: {e.Message}\n{e.StackTrace}")); } } } else { logger.Log(new LogMessage(LogSeverity.Warning, "Notifier", $"Message container was empty. The action will be triggered immediately")); } // perform the action once all the messages have passed if (!token.IsCancellationRequested) { using (var database = new GuildDB()) action?.Invoke(database); } logger.Log(new LogMessage(LogSeverity.Info, "Notifier", $"Finished waiter task for '{guild.Name}'")); }); logger.Log(new LogMessage(LogSeverity.Info, "Notifier", $"Started a waiter task for '{guild.Name}'")); return(tokensource); }