コード例 #1
0
ファイル: PastaModule.cs プロジェクト: Drummss/Miki
        public async Task DeletePasta(EventContext e)
        {
            Locale locale = Locale.GetEntity(e.Channel.Id.ToDbLong());

            if (string.IsNullOrWhiteSpace(e.arguments))
            {
                await Utils.ErrorEmbed(locale, e.GetResource("miki_module_pasta_error_specify", e.GetResource("miki_module_pasta_error_specify")))
                .SendToChannel(e.Channel.Id);

                return;
            }

            using (var context = MikiContext.CreateNoCache())
            {
                context.Set <GlobalPasta>().AsNoTracking();

                GlobalPasta pasta = await context.Pastas.FindAsync(e.arguments);

                if (pasta == null)
                {
                    await Utils.ErrorEmbed(locale, e.GetResource("miki_module_pasta_error_null")).SendToChannel(e.Channel);

                    return;
                }

                if (pasta.CanDeletePasta(e.Author.Id))
                {
                    context.Pastas.Remove(pasta);

                    List <PastaVote> votes = context.Votes.AsNoTracking().Where(p => p.Id.Equals(e.arguments)).ToList();
                    context.Votes.RemoveRange(votes);

                    await context.SaveChangesAsync();

                    await Utils.SuccessEmbed(locale, e.GetResource("miki_module_pasta_delete_success", e.arguments)).SendToChannel(e.Channel);

                    return;
                }
                await Utils.ErrorEmbed(locale, e.GetResource("miki_module_pasta_error_no_permissions", e.GetResource("miki_module_pasta_error_specify_delete"))).SendToChannel(e.Channel);

                return;
            }
        }
コード例 #2
0
        public async Task DeletePasta(EventContext e)
        {
            Locale locale = Locale.GetEntity(e.Guild.Id.ToDbLong());

            if (string.IsNullOrWhiteSpace(e.arguments))
            {
                await Utils.ErrorEmbed(locale, "Please specify which pasta you'd like to remove.")
                .SendToChannel(e.Channel.Id);

                return;
            }

            using (var context = MikiContext.CreateNoCache())
            {
                context.Set <GlobalPasta>().AsNoTracking();

                GlobalPasta pasta = await context.Pastas.FindAsync(e.arguments);

                if (pasta == null)
                {
                    await e.Channel.SendMessage(Utils.ErrorEmbed(locale, "This pasta doesn't exist! check the tag!"));

                    return;
                }

                if (pasta.CanDeletePasta(e.Author.Id))
                {
                    context.Pastas.Remove(pasta);

                    List <PastaVote> votes = context.Votes.AsNoTracking().Where(p => p.Id.Equals(e.arguments)).ToList();
                    context.Votes.RemoveRange(votes);

                    await context.SaveChangesAsync();

                    await e.Channel.SendMessage(Utils.SuccessEmbed(locale, $"Deleted pasta `{e.arguments}`!"));

                    return;
                }
                await e.Channel.SendMessage(Utils.ErrorEmbed(locale, "This pasta is not yours!"));

                return;
            }
        }
コード例 #3
0
ファイル: PastaModule.cs プロジェクト: SnipeFrost/Miki
        public async Task DeletePasta(EventContext e)
        {
            Locale locale = new Locale(e.Channel.Id);

            if (string.IsNullOrWhiteSpace(e.arguments))
            {
                e.ErrorEmbed(e.GetResource("miki_module_pasta_error_specify", e.GetResource("miki_module_pasta_error_specify")))
                .QueueToChannel(e.Channel.Id);
                return;
            }

            using (var context = new MikiContext())
            {
                GlobalPasta pasta = await context.Pastas.FindAsync(e.arguments);

                if (pasta == null)
                {
                    e.ErrorEmbed(e.GetResource("miki_module_pasta_error_null")).QueueToChannel(e.Channel);
                    return;
                }

                if (pasta.CanDeletePasta(e.Author.Id))
                {
                    context.Pastas.Remove(pasta);

                    List <PastaVote> votes = context.Votes.Where(p => p.Id == e.arguments).ToList();
                    context.Votes.RemoveRange(votes);

                    await context.SaveChangesAsync();

                    Utils.SuccessEmbed(locale, e.GetResource("miki_module_pasta_delete_success", e.arguments)).QueueToChannel(e.Channel);
                    return;
                }
                e.ErrorEmbed(e.GetResource("miki_module_pasta_error_no_permissions", e.GetResource("miki_module_pasta_error_specify_delete"))).QueueToChannel(e.Channel);
                return;
            }
        }