public async ValueTask OnMessageCreate(IDiscordMessage message)
        {
            var @event = new MessageReceivedEvent(message);

            await _eventManager.CallAsync(@event);

            if (@event.IsCancelled || string.IsNullOrEmpty(message.Content))
            {
                return;
            }

            var scope           = _provider.CreateScope();
            var features        = new FeatureCollection();
            var responseFeature = new MessageResponseFeature();

            features.Set <IServiceProvidersFeature>(new ServiceProvidersFeature(scope.ServiceProvider));
            features.Set <IUserFeature>(new UserFeature(message.Author));
            features.Set <IMessageResponseFeature>(responseFeature);
            features.Set <IMessageRequestFeature>(new MessageRequestFeature
            {
                GuildId   = message.GuildId,
                ChannelId = message.ChannelId,
                MessageId = message.Id,
                Message   = message.Content
            });

            var context = _messageContextFactory.Create(features);

            try
            {
                CultureInfo.CurrentCulture = _localizer.DefaultCulture;

                _contextAccessor.Context = context;

                await _application(context);

                await _contextDispatcher.DispatchAsync(context);
            }
            catch (Exception e)
            {
                _logger.LogError(e, "An exception occured while processing the message '{Content}' from {User}.", message.Content, message.Author.Username);

                if (_debugOptions.ThrowOnMessageException)
                {
                    throw;
                }
            }
            finally
            {
                scope.Dispose();
                _messageContextFactory.Dispose(context);
            }
        }
예제 #2
0
        /// <summary>
        /// Add or update the warning message in Discord.
        /// </summary>
        /// <param name="guildId">The current guild ID.</param>
        /// <param name="channelId">The current channel ID.</param>
        /// <param name="warning">The warning to update.</param>
        /// <param name="user">The warned user.</param>
        /// <param name="moderator">The moderator.</param>
        /// <returns>The message builder.</returns>
        public Task UpdateWarningMessageAsync(
            ulong guildId,
            ulong channelId,
            UserWarning warning,
            IDiscordUser user,
            IDiscordUser moderator)
        {
            using var scope = _provider.CreateScope();
            var features = new FeatureCollection();

            features.Set <IServiceProvidersFeature>(new ServiceProvidersFeature(scope.ServiceProvider));
            features.Set <IMessageRequestFeature>(new MessageRequestFeature
            {
                GuildId   = guildId,
                ChannelId = channelId
            });

            return(_messageContextDispatcher.DispatchAsync(
                       context => UpdateWarningMessageAsync(context, warning, user, moderator),
                       features
                       ));
        }