Adds role specific channels.

This commit is contained in:
Karolis2011
2019-06-20 11:19:39 +03:00
parent 8058bffba5
commit b74c666d05
13 changed files with 555 additions and 18 deletions

View File

@@ -35,6 +35,7 @@ namespace EventBot.Services
_discord = services.GetRequiredService<DiscordSocketClient>();
_discord.GuildAvailable += OnGuildAvaivable;
_discord.JoinedGuild += OnGuildAvaivable;
}
public DatabaseService(): base() {}

View File

@@ -25,6 +25,28 @@ namespace EventBot.Services
_discord.ReactionAdded += ReactionAddedAsync;
_discord.MessageDeleted += MessageDeletedAsync;
_discord.ChannelDestroyed += ChannelDeleted;
_discord.RoleDeleted += RoleDeleted;
}
private async Task RoleDeleted(SocketRole role)
{
var eventRole = _database.EventRoles.FirstOrDefault(r => r.RoleId == role.Id);
if(eventRole != null)
{
eventRole.ChannelId = 0;
await _database.SaveChangesAsync();
}
}
private async Task ChannelDeleted(SocketChannel channel)
{
var eventRole = _database.EventRoles.FirstOrDefault(r => r.ChannelId == channel.Id);
if (eventRole != null)
{
eventRole.ChannelId = 0;
await _database.SaveChangesAsync();
}
}
public async Task TryJoinEvent(IGuildUser user, EventRole er, string extra, bool extraChecks = true)
@@ -33,14 +55,16 @@ namespace EventBot.Services
throw new Exception("Cross server events are forbidden.");
if (extraChecks && er.ReamainingOpenings <= 0)
throw new Exception("No openings are left.");
if(er.Event.Participants.Where(p => p.UserId == user.Id).Count() > 0)
if(er.Event.ParticipantCount > 0 && er.Event.Participants.Where(p => p.UserId == user.Id).Count() > 0)
throw new Exception("You are already participating.");
if(extraChecks && !er.Event.Active)
throw new Exception("Event is closed.");
if (er.Event.Guild.ParticipantRoleId != 0)
await user.AddRoleAsync(user.Guild.GetRole(er.Event.Guild.ParticipantRoleId));
if (er.RoleId != 0)
await user.AddRoleAsync(user.Guild.GetRole(er.RoleId));
var ep = new EventParticipant()
{
UserId = user.Id,
@@ -63,6 +87,42 @@ namespace EventBot.Services
await (await user.Guild.GetTextChannelAsync(er.Event.Guild.EventRoleConfirmationChannelId)).SendMessageAsync(embed: embed.Build());
}
public async Task RemoveParticipant(IGuildUser user, Event @event, IGuildUser initiator)
{
var participant = @event.Participants.FirstOrDefault(p => p.UserId == user.Id);
_database.Remove(participant);
var embed = new EmbedBuilder()
.WithTitle($"{user} been removed from event `{@event.Title}`, by {initiator}.")
.WithDescription($"Their role was: `{participant.Role.Title}`")
.WithColor(Color.Red);
if (participant.UserData != null)
embed.AddField("Provided details", $"`{participant.UserData}`");
if (@event.Guild.EventRoleConfirmationChannelId != 0)
await (await user.Guild.GetTextChannelAsync(@event.Guild.EventRoleConfirmationChannelId)).SendMessageAsync(embed: embed.Build());
if (@event.Guild.ParticipantRoleId != 0)
await user.RemoveRoleAsync(user.Guild.GetRole(@event.Guild.ParticipantRoleId));
if (participant.Role.RoleId != 0)
await user.RemoveRoleAsync(user.Guild.GetRole(participant.Role.RoleId));
}
public async Task RemoveParticipant(EventParticipant participant, IGuildUser initiator)
{
IGuildUser user = await initiator.Guild.GetUserAsync(participant.UserId);
_database.Remove(participant);
var embed = new EmbedBuilder()
.WithTitle($"{user} been removed from event `{participant.Event.Title}`, by {initiator}.")
.WithDescription($"Their role was: `{participant.Role.Title}`")
.WithColor(Color.Red);
if (participant.UserData != null)
embed.AddField("Provided details", $"`{participant.UserData}`");
if (participant.Event.Guild.EventRoleConfirmationChannelId != 0)
await (await user.Guild.GetTextChannelAsync(participant.Event.Guild.EventRoleConfirmationChannelId)).SendMessageAsync(embed: embed.Build());
if (participant.Event.Guild.ParticipantRoleId != 0)
await user.RemoveRoleAsync(user.Guild.GetRole(participant.Event.Guild.ParticipantRoleId));
}
public async Task RemoveParticipant(IGuildUser user, EventRole eventRole, IGuildUser initiator) => await RemoveParticipant(user, eventRole.Event, initiator);
public Event FindEventBy(IGuild guild, bool bypassActive = false)
{
return _database.Events.OrderByDescending(e => e.Opened).FirstOrDefault(e => e.GuildId == guild.Id && (e.Active || bypassActive));