示例#1
0
        public async Task MessageReceived(SocketMessage rawMessage)
        {
            // Ignore system messages
            if (!(rawMessage is SocketUserMessage message))
            {
                return;
            }
            // Ignore bot messages
            if (message.Source != MessageSource.User)
            {
                return;
            }
            // Ignore messages from channels outside of the whitelisted ones
            //if (!_filter.IsWhitelisted(message.Channel)) return; // Move to database for filters

            int argPos = 0;

            if (!(message.HasMentionPrefix(_discordClient.CurrentUser, ref argPos) ||
                  message.HasStringPrefix(_credentials.DiscordPrefix, ref argPos)))
            {
                return;
            }

            var context = new SocketCommandContext(_discordClient, message);

            await Task.Run(async() =>
            {
                /* Send message to all modules */
                var app = await context.Client.GetApplicationInfoAsync();

                var discordContext = new DiscordContext
                {
                    AppOwner = new DiscordContext.DiscordObject {
                        Id = app.Owner.Id, Name = app.Owner.Username
                    },
                    Channel = new DiscordContext.DiscordObject {
                        Id = context.Channel.Id, Name = context.Channel.Name
                    },
                    Guild = new DiscordContext.DiscordObject {
                        Id = context.Guild.Id, Name = context.Guild.Name
                    },
                    Guilds = context.Client.Guilds.Select(g => new DiscordContext.DiscordStats {
                        Channels = g.Channels.Select(c => new DiscordContext.DiscordObject {
                            Id   = c.Id,
                            Name = c.Name
                        }),
                        Users = g.Users.Select(u => new DiscordContext.DiscordObject {
                            Id   = u.Id,
                            Name = u.Username
                        }),
                    }),
                    Version = DiscordConfig.Version
                };

                EventPublisher.OnMessageReceivedEvent(context,
                                                      new MessageReceivedArgs((int)MessageSender.Discord, _credentials.DiscordPrefix,
                                                                              context.Message.Content, discordContext));
            });
        }