Exemple #1
0
        private static async Task HandleMessageAsync(SocketMessage arg)
        {
            DogStatsd.Increment("messages.recieved");

            if (arg.Author.IsBot ||
                arg.Author.IsWebhook ||
                arg.Author.DiscriminatorValue == 0 ||
                arg is not SocketUserMessage message)
            {
                return;
            }

            ShardedCommandContext context = new ShardedCommandContext(
                SkuldApp.DiscordClient,
                message
                );

            Guild sguild = null;

            if (context.Guild != null)
            {
                if (!await CheckPermissionToSendMessageAsync(context.Channel as ITextChannel).ConfigureAwait(false))
                {
                    return;
                }

                if (SkuldApp.DiscordClient.GetShardFor(context.Guild).ConnectionState != ConnectionState.Connected)
                {
                    return;
                }

                _ = Task.Run(() => HandleSideTasksAsync(context));

                var guser = await
                                (context.Guild as IGuild).GetCurrentUserAsync()
                            .ConfigureAwait(false);

                var guildMem = await
                                   (context.Guild as IGuild).GetUserAsync(message.Author.Id)
                               .ConfigureAwait(false);

                if (!guser.GetPermissions(context.Channel as IGuildChannel).SendMessages)
                {
                    return;
                }

                if (!MessageTools.IsEnabledChannel(guildMem, context.Channel as ITextChannel))
                {
                    return;
                }

                using var Database = new SkuldDbContextFactory().CreateDbContext();

                sguild = await
                         Database.InsertOrGetGuildAsync(context.Guild)
                         .ConfigureAwait(false);

                if (sguild.Name != context.Guild.Name)
                {
                    sguild.Name = context.Guild.Name;

                    await Database.SaveChangesAsync().ConfigureAwait(false);
                }

                if (sguild.IconUrl != context.Guild.IconUrl)
                {
                    sguild.IconUrl = context.Guild.IconUrl;

                    await Database.SaveChangesAsync().ConfigureAwait(false);
                }
            }

            if (!MessageTools.HasPrefix(message, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix))
            {
                return;
            }

            try
            {
                using var Database = new SkuldDbContextFactory().CreateDbContext();

                User suser = await Database.InsertOrGetUserAsync(message.Author).ConfigureAwait(false);

                if (suser != null &&
                    suser.Flags.IsBitSet(DiscordUtilities.Banned) &&
                    (!suser.Flags.IsBitSet(DiscordUtilities.BotCreator) ||
                     !suser.Flags.IsBitSet(DiscordUtilities.BotAdmin))
                    )
                {
                    return;
                }

                var prefix = MessageTools.GetPrefixFromCommand(context.Message.Content, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix);

                if (prefix != null)
                {
                    await DispatchCommandAsync(context, prefix).ConfigureAwait(false);
                }
            }
            catch (Exception ex)
            {
                Log.Critical(Key, ex.Message, context, ex);
            }
        }
Exemple #2
0
        private static async Task HandleMessageAsync(SocketMessage arg)
        {
            DogStatsd.Increment("messages.recieved");

            if (arg.Author.IsBot ||
                arg.Author.IsWebhook ||
                arg.Author.DiscriminatorValue == 0 ||
                arg is not SocketUserMessage message)
            {
                return;
            }

            using var Database = new SkuldDbContextFactory().CreateDbContext();

            User suser = await Database.InsertOrGetUserAsync(arg.Author).ConfigureAwait(false);

            if (suser is not null &&
                suser.Flags.IsBitSet(DiscordUtilities.Banned) &&
                (!suser.Flags.IsBitSet(DiscordUtilities.BotCreator) ||
                 !suser.Flags.IsBitSet(DiscordUtilities.BotAdmin))
                )
            {
                return;
            }

            ShardedCommandContext context = new(SkuldApp.DiscordClient, message);

            Guild sguild = null;

            if (context.Guild is not null)
            {
                if (SkuldApp.DiscordClient.GetShardFor(context.Guild).ConnectionState != ConnectionState.Connected)
                {
                    return;
                }

                if (!await CheckPermissionToSendMessageAsync(context.Channel as ITextChannel).ConfigureAwait(false))
                {
                    return;
                }

                var guser = await
                                (context.Guild as IGuild).GetCurrentUserAsync()
                            .ConfigureAwait(false);

                if (!guser.GetPermissions(context.Channel as IGuildChannel).SendMessages)
                {
                    return;
                }

                var guildMem = await
                                   (context.Guild as IGuild).GetUserAsync(message.Author.Id)
                               .ConfigureAwait(false);

                if (!MessageTools.IsEnabledChannel(guildMem, context.Channel as ITextChannel))
                {
                    return;
                }

                sguild = await
                         Database.InsertOrGetGuildAsync(context.Guild)
                         .ConfigureAwait(false);

                if (sguild.Name != context.Guild.Name)
                {
                    sguild.Name = context.Guild.Name;

                    await Database.SaveChangesAsync().ConfigureAwait(false);
                }

                if (sguild.IconUrl != context.Guild.IconUrl)
                {
                    sguild.IconUrl = context.Guild.IconUrl;

                    await Database.SaveChangesAsync().ConfigureAwait(false);
                }
            }

            if (!MessageTools.HasPrefix(message, SkuldApp.Configuration.Prefix, SkuldApp.Configuration.AltPrefix, sguild?.Prefix))
            {
                return;
            }

            await Task.WhenAll(ProcessCommandAsync(context, sguild), ProcessExperienceAsync(context, sguild), ProcessCustomCommand(context, sguild));
        }