public async Task <int> AddEvent(CommandContext ctx, EventType type, string message, DateTime eventTime, ulong channelId = 0) { List <ulong> mentionedUserIds = new List <ulong>(); List <ulong> mentionedRoleIds = new List <ulong>(); ctx.Message.MentionedUsers.ToList().ForEach(user => mentionedUserIds.Add(user.Id)); ctx.Message.MentionedRoles.ToList().ForEach(user => mentionedRoleIds.Add(user.Id)); TimedEvent tEvent = new TimedEvent { Type = type, Message = message, EventTime = eventTime, UserId = ctx.User.Id, ChannelId = channelId > 0 ? channelId : ctx.Channel.Id, MentionUserIdList = mentionedUserIds.ToArray(), MentionRoleIdList = mentionedRoleIds.ToArray() }; tEvent.Id = (await ShimakazeBot.DbCtx.TimedEvents.AddAsync(tEvent)).Entity.Id; try { await ShimakazeBot.DbCtx.SaveChangesAsync(); } catch (Exception e) { await ctx.RespondAsync(e.Message); } return(AddAndStartEvent(EventInTimer.MakeTimer(tEvent), tEvent.Id) ? tEvent.Id : -1); }
private async void EventEnded(object sender, ElapsedEventArgs e) { EventInTimer tEvent = (EventInTimer)sender; tEvent.Elapsed -= EventEnded; tEvent.Stop(); if (events.Count() > 0) { events.Remove(tEvent); } if (tEvent.dbEvent == null) { ShimakazeBot.Client.Logger.Log(LogLevel.Error, LogSources.TIMER_EVENT_EVENT, "Timer had no event attached"); return; } var channel = await ShimakazeBot.Client.GetChannelAsync(tEvent.dbEvent.ChannelId); var eType = tEvent.dbEvent.Type; DiscordEmbedBuilder embedBuilder = new DiscordEmbedBuilder() .WithAuthor(ShimakazeBot.Client.CurrentUser.Username, null, ShimakazeBot.Client.CurrentUser.AvatarUrl) .WithColor(eType == EventType.REMINDER ? DiscordColor.Purple : DiscordColor.HotPink) .WithTimestamp(tEvent.dbEvent.EventTime) .WithTitle(eType == EventType.REMINDER ? "Here's your reminder~~" : "Event Time! - イベント タイム!") .WithFooter($"Event #{tEvent.dbEvent.Id}") .WithDescription((string.IsNullOrWhiteSpace(tEvent.dbEvent.Message) ? "*No message.*" : tEvent.dbEvent.Message)); if (eType == EventType.EVENT) { embedBuilder.AddField("Created by", $"<@{tEvent.dbEvent.UserId}>"); } List <ulong> mentionUserIds = tEvent.dbEvent.MentionUserIdList.ToList(); List <ulong> mentionRoleIds = tEvent.dbEvent.MentionRoleIdList.ToList(); if (tEvent.dbEvent.Type == EventType.REMINDER) { mentionUserIds.Insert(0, tEvent.dbEvent.UserId); } string mentionString = (mentionUserIds.Count() > 0 ? $"<@{ string.Join("> <@", mentionUserIds)}> " : "") + (mentionRoleIds.Count() > 0 ? $" <@&{ string.Join("> <@&", mentionRoleIds)}> " : ""); await CTX.SendSanitizedMessageAsync(channel, mentionString, false, embedBuilder); ShimakazeBot.DbCtx.TimedEvents.RemoveRange( ShimakazeBot.DbCtx.TimedEvents.Where(tE => tE.Id == tEvent.dbEvent.Id)); await ShimakazeBot.DbCtx.SaveChangesAsync(); }
private bool AddAndStartEvent(EventInTimer eventInTimer, int id) { if (eventInTimer == null) { ShimakazeBot.Client.Logger.Log(LogLevel.Error, LogSources.TIMER_EVENT_EVENT, $"Failed to make timer from event #{id}"); return(false); } events.Add(eventInTimer); events.Last().Elapsed += EventEnded; events.Last().Start(); return(true); }
private void SetEventTimers() { ShimakazeBot.DbCtx.TimedEvents.ToList().ForEach(tEvent => { AddAndStartEvent(EventInTimer.MakeTimer(tEvent), tEvent.Id); }); }