private async void OnTimeElapsed(object source, ElapsedEventArgs e) { foreach (var guild in _client.Guilds) { var channel = await TryGettingChannel(guild); if (channel == null) { await _ownerLogger.LogError("0007", "", guild.Name); return; } var events = await TryGettingEvents(guild); if (events == null) { await _ownerLogger.LogError("0006", "", guild.Name); return; } var fields = new List <(string, string, bool)>(); FillEventsField("started_event_field_title", fields, FindStartedEvents(events)); FillEventsField("upcoming_short_event_field_title", fields, FindUpcomingEventsShort(events)); FillEventsField("upcoming_long_field_title", fields, FindUpcomingEventsLong(events)); string msg = ""; if (FindStartedEvents(events).Any()) { var eventRole = await TryGettingEventRole(guild); var guildRole = guild.GetRole(eventRole.RoleID); if (guildRole != null) { msg = guildRole.Mention; } } var payload = _embedPayloadFactory.CreateEmbedPayload(EmbedStruct.Success, EmbedPayloadType.Event, "event", null, null, null, null, fields); var embed = _embedService.CreateFieldEmbed(payload); await guild .GetTextChannel(channel.ChannelID) .SendEmbedMsgAsync(msg, embed); } }
public async Task SynchroniseGuilds() { var failedGuilds = new List <string>(); foreach (var guild in _client.Guilds) { var dto = new GuildDto { GuildID = guild.Id }; var payload = _discordPayloadFactory.CreateDiscordObjectPayload <GuildAPIPayload>(dto); var result = await _apiService.PostAsync(payload); if (!result.IsSuccessStatusCode) { failedGuilds.Add(guild.Name); } } if (failedGuilds.Count > 0) { await _ownerLogger.LogError("0005", failedGuilds.ToString()); } }
private async Task MessageReceivedHandler(SocketMessage msg) { if (msg.Author.IsBot || !_injhinuity.Ready) // Wait until bot connected and initialized { return; } if (!(msg is SocketUserMessage usrMsg)) { return; } if (msg.Content.Length <= 0) { return; } var channel = msg.Channel as ISocketMessageChannel; var guild = (msg.Channel as SocketTextChannel)?.Guild; var context = new SocketCommandContext(_client, usrMsg); // Initialise error handler for exceptions to reuse in case of errors async void errorCodeHandler(ErrorCodeException ex) { var ticks = DateTimeOffset.Now.Ticks; if (_timeErrorCodeExceptionWasShown + ERROR_CODE_COOLDOWN <= ticks) { _timeErrorCodeExceptionWasShown = ticks; if (ex.ErrorCode != "0004") { await _ownerLogger.LogError(ex.ErrorCode, ex.Message); } var titleParams = new List <string> { ex.ErrorCode }; var footerParams = new List <string> { ex.ErrorCode }; var payload = _embedPayloadFactory.CreateEmbedPayload(EmbedStruct.ErrorCode, EmbedPayloadType.Error, ex.ErrorCode, context.Message.Author, null, titleParams, footerParams); var embed = _embedService.CreateBaseEmbed(payload); var message = await context.Channel.SendMessageAsync("", false, embed.Build()); await message.DeleteAfterLong(); LogException(ex); } else { await usrMsg.AddReactionAsync(new Emoji("❌")); } } try { /*if (await _conversationHandler.TryHandlingConversationForUser(context, usrMsg.Content)) * return;*/ if (await _customCommandHandler.TryHandlingCustomCommand(context, usrMsg.Content)) { return; } /*_pollService.TryAddingNewVote(msg.Content, context);*/ await TryRunCommand(guild, channel, usrMsg, context, errorCodeHandler); /*await _foolsService.CheckMessageLength(channel, (SocketUserMessage)msg, guild);*/ } catch (ErrorCodeException ex) { errorCodeHandler(ex); } catch (HttpRequestException ex) { errorCodeHandler(new ErrorCodeException("0002", ex.Message, ex.InnerException)); } catch (Exception ex) { await _ownerLogger.LogException(new BotException(context, ex.TargetSite.GetType().Name, ex.Message, ex.StackTrace, ex.InnerException)); LogException(ex); } }