Inital bot commit
This commit is contained in:
31
EventBot/Misc/EventRoleTypeReader.cs
Normal file
31
EventBot/Misc/EventRoleTypeReader.cs
Normal file
@@ -0,0 +1,31 @@
|
||||
using Discord.Commands;
|
||||
using EventBot.Entities;
|
||||
using EventBot.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EventBot.Misc
|
||||
{
|
||||
class EventRoleTypeReader : TypeReader
|
||||
{
|
||||
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
var database = services.GetRequiredService<DatabaseService>();
|
||||
if (context.Guild == null)
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.UnmetPrecondition, "Event roles are avaivable only inside guild context."));
|
||||
EventRole er = null;
|
||||
if (int.TryParse(input, out int id))
|
||||
er = database.EventRoles.FirstOrDefault(r => r.Id == id);
|
||||
else
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Event role id is not a valid number."));
|
||||
if(er == null)
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.ObjectNotFound, "Specified event role was not found."));
|
||||
if(er.Event?.GuildId != context.Guild.Id)
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.Exception, "Cross guild event role access is denied."));
|
||||
return Task.FromResult(TypeReaderResult.FromSuccess(er));
|
||||
}
|
||||
}
|
||||
}
|
30
EventBot/Misc/EventTypeReader.cs
Normal file
30
EventBot/Misc/EventTypeReader.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
using Discord.Commands;
|
||||
using EventBot.Entities;
|
||||
using EventBot.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace EventBot.Misc
|
||||
{
|
||||
class EventTypeReader : TypeReader
|
||||
{
|
||||
public override Task<TypeReaderResult> ReadAsync(ICommandContext context, string input, IServiceProvider services)
|
||||
{
|
||||
var events = services.GetRequiredService<EventManagementService>();
|
||||
if (context.Guild == null)
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.UnmetPrecondition, "Events are avaivable only inside guild context."));
|
||||
Event ev;
|
||||
if (input == null)
|
||||
ev = events.FindEventBy(context.Guild);
|
||||
else if (int.TryParse(input, out int id))
|
||||
ev = events.FindEventBy(context.Guild, id);
|
||||
else
|
||||
return Task.FromResult(TypeReaderResult.FromError(CommandError.ParseFailed, "Event id is not a number."));
|
||||
|
||||
return Task.FromResult(TypeReaderResult.FromSuccess(ev));
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user