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

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations.Schema;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
namespace EventBot.Entities namespace EventBot.Entities
{ {
@@ -15,6 +16,8 @@ namespace EventBot.Entities
public string Description { get; set; } public string Description { get; set; }
public string Emote { get; set; } public string Emote { get; set; }
public int MaxParticipants { get; set; } public int MaxParticipants { get; set; }
public ulong ChannelId { get; set; }
public ulong RoleId { get; set; }
public int EventId { get; set; } public int EventId { get; set; }
[ForeignKey("EventId")] [ForeignKey("EventId")]
public virtual Event Event { get; set; } public virtual Event Event { get; set; }
@@ -22,6 +25,8 @@ namespace EventBot.Entities
public int ParticipantCount => Participants == null ? 0 : Participants.Count; public int ParticipantCount => Participants == null ? 0 : Participants.Count;
public int ReamainingOpenings => MaxParticipants < 0 ? 1 : MaxParticipants - ParticipantCount; public int ReamainingOpenings => MaxParticipants < 0 ? 1 : MaxParticipants - ParticipantCount;
public string ChannelName => Regex.Replace(Title.Replace(' ', '-'), "(?!\\w)", "");
public int SortNumber public int SortNumber
{ {
get get

View File

@@ -12,6 +12,7 @@ namespace EventBot.Entities
public string Prefix { get; set; } public string Prefix { get; set; }
public ulong EventRoleConfirmationChannelId { get; set; } public ulong EventRoleConfirmationChannelId { get; set; }
public ulong ParticipantRoleId { get; set; } public ulong ParticipantRoleId { get; set; }
public ulong AutoRoleChannelCategoryId { get; set; }
public virtual ICollection<Event> Events { get; set; } public virtual ICollection<Event> Events { get; set; }
} }

View File

@@ -0,0 +1,147 @@
// <auto-generated />
using System;
using EventBot.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace EventBot.Migrations.MySql
{
[DbContext(typeof(MySqlDatabaseService))]
[Migration("20190620075923_AutoRolesAndChannels")]
partial class AutoRolesAndChannels
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("EventBot.Entities.Event", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<bool>("Active");
b.Property<string>("Description");
b.Property<ulong>("GuildId");
b.Property<ulong>("MessageChannelId");
b.Property<ulong>("MessageId");
b.Property<DateTime>("Opened");
b.Property<string>("Title");
b.Property<int>("Type");
b.HasKey("Id");
b.HasIndex("GuildId");
b.ToTable("Events");
});
modelBuilder.Entity("EventBot.Entities.EventParticipant", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("EventId");
b.Property<int>("EventRoleId");
b.Property<string>("UserData");
b.Property<ulong>("UserId");
b.HasKey("Id");
b.HasIndex("EventId");
b.HasIndex("EventRoleId");
b.ToTable("EventParticipants");
});
modelBuilder.Entity("EventBot.Entities.EventRole", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<string>("Description");
b.Property<string>("Emote");
b.Property<int>("EventId");
b.Property<int>("MaxParticipants");
b.Property<ulong>("RoleId");
b.Property<string>("Title");
b.HasKey("Id");
b.HasIndex("EventId");
b.ToTable("EventRoles");
});
modelBuilder.Entity("EventBot.Entities.GuildConfig", b =>
{
b.Property<ulong>("GuildId")
.ValueGeneratedOnAdd();
b.Property<ulong>("AutoRoleChannelCategoryId");
b.Property<ulong>("EventRoleConfirmationChannelId");
b.Property<ulong>("ParticipantRoleId");
b.Property<string>("Prefix");
b.HasKey("GuildId");
b.ToTable("GuildConfigs");
});
modelBuilder.Entity("EventBot.Entities.Event", b =>
{
b.HasOne("EventBot.Entities.GuildConfig", "Guild")
.WithMany("Events")
.HasForeignKey("GuildId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("EventBot.Entities.EventParticipant", b =>
{
b.HasOne("EventBot.Entities.Event", "Event")
.WithMany("Participants")
.HasForeignKey("EventId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("EventBot.Entities.EventRole", "Role")
.WithMany("Participants")
.HasForeignKey("EventRoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("EventBot.Entities.EventRole", b =>
{
b.HasOne("EventBot.Entities.Event", "Event")
.WithMany("Roles")
.HasForeignKey("EventId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace EventBot.Migrations.MySql
{
public partial class AutoRolesAndChannels : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<ulong>(
name: "AutoRoleChannelCategoryId",
table: "GuildConfigs",
nullable: false,
defaultValue: 0ul);
migrationBuilder.AddColumn<ulong>(
name: "ChannelId",
table: "EventRoles",
nullable: false,
defaultValue: 0ul);
migrationBuilder.AddColumn<ulong>(
name: "RoleId",
table: "EventRoles",
nullable: false,
defaultValue: 0ul);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AutoRoleChannelCategoryId",
table: "GuildConfigs");
migrationBuilder.DropColumn(
name: "ChannelId",
table: "EventRoles");
migrationBuilder.DropColumn(
name: "RoleId",
table: "EventRoles");
}
}
}

View File

@@ -72,6 +72,8 @@ namespace EventBot.Migrations.MySql
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd(); .ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<string>("Description"); b.Property<string>("Description");
b.Property<string>("Emote"); b.Property<string>("Emote");
@@ -80,6 +82,8 @@ namespace EventBot.Migrations.MySql
b.Property<int>("MaxParticipants"); b.Property<int>("MaxParticipants");
b.Property<ulong>("RoleId");
b.Property<string>("Title"); b.Property<string>("Title");
b.HasKey("Id"); b.HasKey("Id");
@@ -94,6 +98,8 @@ namespace EventBot.Migrations.MySql
b.Property<ulong>("GuildId") b.Property<ulong>("GuildId")
.ValueGeneratedOnAdd(); .ValueGeneratedOnAdd();
b.Property<ulong>("AutoRoleChannelCategoryId");
b.Property<ulong>("EventRoleConfirmationChannelId"); b.Property<ulong>("EventRoleConfirmationChannelId");
b.Property<ulong>("ParticipantRoleId"); b.Property<ulong>("ParticipantRoleId");

View File

@@ -0,0 +1,146 @@
// <auto-generated />
using System;
using EventBot.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace EventBot.Migrations.Sqlite
{
[DbContext(typeof(SqliteDatabaseService))]
[Migration("20190620080021_AutoRolesAndChannels")]
partial class AutoRolesAndChannels
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "2.2.4-servicing-10062");
modelBuilder.Entity("EventBot.Entities.Event", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<bool>("Active");
b.Property<string>("Description");
b.Property<ulong>("GuildId");
b.Property<ulong>("MessageChannelId");
b.Property<ulong>("MessageId");
b.Property<DateTime>("Opened");
b.Property<string>("Title");
b.Property<int>("Type");
b.HasKey("Id");
b.HasIndex("GuildId");
b.ToTable("Events");
});
modelBuilder.Entity("EventBot.Entities.EventParticipant", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<int>("EventId");
b.Property<int>("EventRoleId");
b.Property<string>("UserData");
b.Property<ulong>("UserId");
b.HasKey("Id");
b.HasIndex("EventId");
b.HasIndex("EventRoleId");
b.ToTable("EventParticipants");
});
modelBuilder.Entity("EventBot.Entities.EventRole", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<string>("Description");
b.Property<string>("Emote");
b.Property<int>("EventId");
b.Property<int>("MaxParticipants");
b.Property<ulong>("RoleId");
b.Property<string>("Title");
b.HasKey("Id");
b.HasIndex("EventId");
b.ToTable("EventRoles");
});
modelBuilder.Entity("EventBot.Entities.GuildConfig", b =>
{
b.Property<ulong>("GuildId")
.ValueGeneratedOnAdd();
b.Property<ulong>("AutoRoleChannelCategoryId");
b.Property<ulong>("EventRoleConfirmationChannelId");
b.Property<ulong>("ParticipantRoleId");
b.Property<string>("Prefix");
b.HasKey("GuildId");
b.ToTable("GuildConfigs");
});
modelBuilder.Entity("EventBot.Entities.Event", b =>
{
b.HasOne("EventBot.Entities.GuildConfig", "Guild")
.WithMany("Events")
.HasForeignKey("GuildId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("EventBot.Entities.EventParticipant", b =>
{
b.HasOne("EventBot.Entities.Event", "Event")
.WithMany("Participants")
.HasForeignKey("EventId")
.OnDelete(DeleteBehavior.Cascade);
b.HasOne("EventBot.Entities.EventRole", "Role")
.WithMany("Participants")
.HasForeignKey("EventRoleId")
.OnDelete(DeleteBehavior.Cascade);
});
modelBuilder.Entity("EventBot.Entities.EventRole", b =>
{
b.HasOne("EventBot.Entities.Event", "Event")
.WithMany("Roles")
.HasForeignKey("EventId")
.OnDelete(DeleteBehavior.Cascade);
});
#pragma warning restore 612, 618
}
}
}

View File

@@ -0,0 +1,43 @@
using Microsoft.EntityFrameworkCore.Migrations;
namespace EventBot.Migrations.Sqlite
{
public partial class AutoRolesAndChannels : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<ulong>(
name: "AutoRoleChannelCategoryId",
table: "GuildConfigs",
nullable: false,
defaultValue: 0ul);
migrationBuilder.AddColumn<ulong>(
name: "ChannelId",
table: "EventRoles",
nullable: false,
defaultValue: 0ul);
migrationBuilder.AddColumn<ulong>(
name: "RoleId",
table: "EventRoles",
nullable: false,
defaultValue: 0ul);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "AutoRoleChannelCategoryId",
table: "GuildConfigs");
migrationBuilder.DropColumn(
name: "ChannelId",
table: "EventRoles");
migrationBuilder.DropColumn(
name: "RoleId",
table: "EventRoles");
}
}
}

View File

@@ -71,6 +71,8 @@ namespace EventBot.Migrations.Sqlite
b.Property<int>("Id") b.Property<int>("Id")
.ValueGeneratedOnAdd(); .ValueGeneratedOnAdd();
b.Property<ulong>("ChannelId");
b.Property<string>("Description"); b.Property<string>("Description");
b.Property<string>("Emote"); b.Property<string>("Emote");
@@ -79,6 +81,8 @@ namespace EventBot.Migrations.Sqlite
b.Property<int>("MaxParticipants"); b.Property<int>("MaxParticipants");
b.Property<ulong>("RoleId");
b.Property<string>("Title"); b.Property<string>("Title");
b.HasKey("Id"); b.HasKey("Id");
@@ -93,6 +97,8 @@ namespace EventBot.Migrations.Sqlite
b.Property<ulong>("GuildId") b.Property<ulong>("GuildId")
.ValueGeneratedOnAdd(); .ValueGeneratedOnAdd();
b.Property<ulong>("AutoRoleChannelCategoryId");
b.Property<ulong>("EventRoleConfirmationChannelId"); b.Property<ulong>("EventRoleConfirmationChannelId");
b.Property<ulong>("ParticipantRoleId"); b.Property<ulong>("ParticipantRoleId");

View File

@@ -88,7 +88,7 @@ namespace EventBot.Modules
.Aggregate(true, (a, r) => a && r)) .Aggregate(true, (a, r) => a && r))
.SelectMany(c => c.Aliases.Select(a => new { CI = c, MA = (c.Aliases[0] == a), A = a }).Reverse()) .SelectMany(c => c.Aliases.Select(a => new { CI = c, MA = (c.Aliases[0] == a), A = a }).Reverse())
.Select((e, i) => new { Command = e, Index = i }) .Select((e, i) => new { Command = e, Index = i })
.GroupBy(o => o.Index / 20) .GroupBy(o => o.Index / 30)
.Select(g => g.Select(o => o.Command)); .Select(g => g.Select(o => o.Command));
var pager = new PaginatedMessage() var pager = new PaginatedMessage()

View File

@@ -56,6 +56,7 @@ namespace EventBot.Modules
[RequireUserPermission(GuildPermission.Administrator, Group = "Permission")] [RequireUserPermission(GuildPermission.Administrator, Group = "Permission")]
[RequireOwner(Group = "Permission")] [RequireOwner(Group = "Permission")]
[Group("event")] [Group("event")]
//[Alias("e")]
[Name("Event management")] [Name("Event management")]
public class EventManagementModule : InteractiveBase<SocketCommandContext> public class EventManagementModule : InteractiveBase<SocketCommandContext>
{ {
@@ -67,6 +68,7 @@ namespace EventBot.Modules
_database = database; _database = database;
} }
[Priority(2)]
[Command("config logchannel")] [Command("config logchannel")]
[Name("Configure logging channel")] [Name("Configure logging channel")]
[Summary("Sets logging channel for role changes.")] [Summary("Sets logging channel for role changes.")]
@@ -83,6 +85,7 @@ namespace EventBot.Modules
await s; await s;
} }
[Priority(2)]
[Command("config partrole")] [Command("config partrole")]
[Name("Configure participant role")] [Name("Configure participant role")]
[Summary("Sets discord role to assign when the user selects a role.")] [Summary("Sets discord role to assign when the user selects a role.")]
@@ -99,6 +102,29 @@ namespace EventBot.Modules
await s; await s;
} }
[Priority(2)]
[Command("config category")]
[Name("Configure auto channel category")]
[Summary("Configures auto channel category, where new channels will be created for each role.")]
public async Task ConfigureParticipantCategory(
[Summary("Category to use when making channels for roles.")] ICategoryChannel category = null)
{
var guild = _database.GuildConfigs.FirstOrDefault(g => g.GuildId == Context.Guild.Id);
if (guild == null)
throw new Exception("This command must be executed inside a discord server.");
if (category == null)
{
guild.AutoRoleChannelCategoryId = 0;
await ReplyAsync("No channels and discord roles will be created for event roles.");
} else
{
guild.AutoRoleChannelCategoryId = category.Id;
await ReplyAsync($"Bot will create discord roles and channels for each new role inside `{category.Name}` category.");
}
await _database.SaveChangesAsync();
}
[Priority(1)]
[Command("new")] [Command("new")]
[Alias("add", "create")] [Alias("add", "create")]
[Name("Create event")] [Name("Create event")]
@@ -124,6 +150,7 @@ namespace EventBot.Modules
await ReplyAsync($"Created new {@event.Type} event `{title}`, with description: `{description}`. Its ID is `{@event.Id}`."); await ReplyAsync($"Created new {@event.Type} event `{title}`, with description: `{description}`. Its ID is `{@event.Id}`.");
} }
[Priority(2)]
[Command("update title")] [Command("update title")]
[Name("Update event's title")] [Name("Update event's title")]
[Summary("Updates the event's title.")] [Summary("Updates the event's title.")]
@@ -142,6 +169,7 @@ namespace EventBot.Modules
await ReplyAsync($"Updated event's (`{@event.Id}`) title to `{@event.Title}`"); await ReplyAsync($"Updated event's (`{@event.Id}`) title to `{@event.Title}`");
await _events.UpdateEventMessage(@event); await _events.UpdateEventMessage(@event);
} }
[Priority(2)]
[Command("update description")] [Command("update description")]
[Alias("update desc")] [Alias("update desc")]
[Name("Update event's description")] [Name("Update event's description")]
@@ -162,6 +190,7 @@ namespace EventBot.Modules
await _events.UpdateEventMessage(@event); await _events.UpdateEventMessage(@event);
} }
[Priority(2)]
[Command("update type")] [Command("update type")]
[Name("Update event's type")] [Name("Update event's type")]
[Summary("Updates the event type.")] [Summary("Updates the event type.")]
@@ -186,6 +215,7 @@ namespace EventBot.Modules
} }
[Priority(2)]
[Command("role new")] [Command("role new")]
[Alias("role add", "role create")] [Alias("role add", "role create")]
[Name("Add role")] [Name("Add role")]
@@ -219,11 +249,21 @@ namespace EventBot.Modules
Emote = parsedEmote.ToString(), Emote = parsedEmote.ToString(),
Event = @event Event = @event
}; };
if(@event.Guild.AutoRoleChannelCategoryId != 0)
{
var channel = await Context.Guild.CreateTextChannelAsync(er.ChannelName, o => o.CategoryId = @event.Guild.AutoRoleChannelCategoryId);
var role = await Context.Guild.CreateRoleAsync(er.Title);
await channel.AddPermissionOverwriteAsync(role, new OverwritePermissions(viewChannel: PermValue.Allow, sendMessages: PermValue.Allow));
er.RoleId = role.Id;
er.ChannelId = channel.Id;
}
_database.Add(er); _database.Add(er);
await _database.SaveChangesAsync(); await _database.SaveChangesAsync();
await ReplyAsync($"Added event role `{er.Id}` for event `{er.Event.Id}`, title: `{er.Title}`, description: `{er.Description}`, slots: `{er.MaxParticipants}`, emote: {er.Emote}"); await ReplyAsync($"Added event role `{er.Id}` for event `{er.Event.Id}`, title: `{er.Title}`, description: `{er.Description}`, slots: `{er.MaxParticipants}`, emote: {er.Emote}");
} }
[Priority(2)]
[Command("role update title")] [Command("role update title")]
[Name("Update role's title")] [Name("Update role's title")]
[Summary("Updates the role's title.")] [Summary("Updates the role's title.")]
@@ -242,6 +282,7 @@ namespace EventBot.Modules
await _events.UpdateEventMessage(eventRole.Event); await _events.UpdateEventMessage(eventRole.Event);
} }
[Priority(2)]
[Command("role update description")] [Command("role update description")]
[Alias("role update desc")] [Alias("role update desc")]
[Name("Update role's description")] [Name("Update role's description")]
@@ -261,6 +302,7 @@ namespace EventBot.Modules
await _events.UpdateEventMessage(eventRole.Event); await _events.UpdateEventMessage(eventRole.Event);
} }
[Priority(2)]
[Command("role update slots")] [Command("role update slots")]
[Name("Update role's slots")] [Name("Update role's slots")]
[Summary("Updates a role's maximum participants count.")] [Summary("Updates a role's maximum participants count.")]
@@ -280,6 +322,7 @@ namespace EventBot.Modules
} }
[Priority(2)]
[Command("role update emote")] [Command("role update emote")]
[Name("Update role's role")] [Name("Update role's role")]
[Summary("Updates a role's emote.")] [Summary("Updates a role's emote.")]
@@ -335,6 +378,33 @@ namespace EventBot.Modules
await ReplyAsync(embed: embed.Build()); await ReplyAsync(embed: embed.Build());
} }
[Priority(2)]
[Command("role delete")]
[Alias("role remove")]
[Name("Delete role")]
[Summary("Deletes role and all information about it.")]
public async Task EventRoleDelete(
[Summary("Role you wish to delete.")] EventRole eventRole)
{
if (!(Context.User is SocketGuildUser guildUser))
throw new Exception("This command must be executed inside a discord server.");
if (eventRole == null)
throw new Exception("Please provide the correct role, this one does not exist.");
if (eventRole.Event.MessageId != 0)
throw new Exception("Can't remove role from open event.");
foreach (var p in eventRole.Participants)
await _events.RemoveParticipant(p, guildUser);
if(eventRole.ChannelId != 0)
await Context.Guild.GetTextChannel(eventRole.ChannelId)?.DeleteAsync();
if (eventRole.RoleId != 0)
await Context.Guild.GetRole(eventRole.RoleId)?.DeleteAsync();
_database.Remove(eventRole);
await _database.SaveChangesAsync();
await ReplyAsync($"Role {eventRole.Title} has been deleted, and it's participants removed.");
}
[Priority(1)] [Priority(1)]
[Command("role")] [Command("role")]
[Alias("role info")] [Alias("role info")]
@@ -361,6 +431,7 @@ namespace EventBot.Modules
} }
} }
[Priority(1)]
[Command("open")] [Command("open")]
[Alias("start", "begin")] [Alias("start", "begin")]
[Name("Open event")] [Name("Open event")]
@@ -395,6 +466,8 @@ namespace EventBot.Modules
} }
} }
[Priority(1)]
[Command("close")] [Command("close")]
[Alias("stop", "end")] [Alias("stop", "end")]
[Name("Close event")] [Name("Close event")]
@@ -417,6 +490,8 @@ namespace EventBot.Modules
await ReplyAsync($"Event's `{@event.Id}` registration has been closed, its registration message will now be normal message."); await ReplyAsync($"Event's `{@event.Id}` registration has been closed, its registration message will now be normal message.");
} }
[Priority(1)]
[Command("finalize")] [Command("finalize")]
[Alias("archive")] [Alias("archive")]
[Name("Archive event")] [Name("Archive event")]
@@ -441,9 +516,18 @@ namespace EventBot.Modules
var user = Context.Guild.GetUser(participant.UserId); var user = Context.Guild.GetUser(participant.UserId);
await user.RemoveRoleAsync(Context.Guild.GetRole(@event.Guild.ParticipantRoleId)); await user.RemoveRoleAsync(Context.Guild.GetRole(@event.Guild.ParticipantRoleId));
} }
foreach (var role in @event.Roles)
{
if (role.ChannelId != 0)
await Context.Guild.GetTextChannel(role.ChannelId)?.DeleteAsync();
if (role.RoleId != 0)
await Context.Guild.GetRole(role.RoleId)?.DeleteAsync();
}
await ReplyAsync($"Everyone's roles have been removed. I hope it was fun!"); await ReplyAsync($"Everyone's roles have been removed. I hope it was fun!");
} }
[Priority(1)]
[Command("list")] [Command("list")]
[Alias("all")] [Alias("all")]
[Name("Lists events")] [Name("Lists events")]
@@ -478,6 +562,8 @@ namespace EventBot.Modules
await PagedReplyAsync(pager); await PagedReplyAsync(pager);
} }
[Priority(2)]
[Command("participant add")] [Command("participant add")]
[Name("Add participant")] [Name("Add participant")]
[Summary("Add user to event role. Acts like join command.")] [Summary("Add user to event role. Acts like join command.")]
@@ -507,6 +593,7 @@ namespace EventBot.Modules
await Context.Message.DeleteAsync(); // Protect somewhat sensitive data. await Context.Message.DeleteAsync(); // Protect somewhat sensitive data.
} }
[Priority(2)]
[Command("participant remove")] [Command("participant remove")]
[Alias("participant delete")] [Alias("participant delete")]
[Name("Remove participant")] [Name("Remove participant")]
@@ -521,25 +608,15 @@ namespace EventBot.Modules
throw new Exception("No events were found for this discord server."); throw new Exception("No events were found for this discord server.");
if (!@event.Active) if (!@event.Active)
throw new Exception("This event is finalized. Please make a new event."); throw new Exception("This event is finalized. Please make a new event.");
if (!(user is IGuildUser guildUser) || !(Context.User is IGuildUser initiator))
if (!(user is IGuildUser guildUser))
throw new Exception("This command must be executed inside a discord server."); throw new Exception("This command must be executed inside a discord server.");
var participant = @event.Participants.FirstOrDefault(p => p.UserId == guildUser.Id);
_database.Remove(participant);
var embed = new EmbedBuilder()
.WithTitle($"{user} been removed from event `{@event.Title}`, by {Context.User}.")
.WithDescription($"Their role was: `{participant.Role.Title}`")
.WithColor(Color.Red);
if (participant.UserData != null)
embed.AddField("Provided details", $"`{participant.UserData}`");
await _events.RemoveParticipant(guildUser, @event, initiator);
await _database.SaveChangesAsync(); await _database.SaveChangesAsync();
await ReplyAsync($"{guildUser} has been removed from event.");
await _events.UpdateEventMessage(@event); await _events.UpdateEventMessage(@event);
if (@event.Guild.EventRoleConfirmationChannelId != 0)
await (await ((IGuild)Context.Guild).GetTextChannelAsync(@event.Guild.EventRoleConfirmationChannelId)).SendMessageAsync(embed: embed.Build());
if (@event.Guild.ParticipantRoleId != 0)
await guildUser.RemoveRoleAsync(Context.Guild.GetRole(@event.Guild.ParticipantRoleId));
} }
} }
} }

View File

@@ -50,7 +50,9 @@ namespace EventBot
{ {
return new ServiceCollection() return new ServiceCollection()
.AddSingleton(s => new DiscordSocketClient(new DiscordSocketConfig() { .AddSingleton(s => new DiscordSocketClient(new DiscordSocketConfig() {
#if DEBUG
LogLevel = LogSeverity.Debug, LogLevel = LogSeverity.Debug,
#endif
MessageCacheSize = 1500 MessageCacheSize = 1500
})) }))
.AddSingleton<CommandService>() .AddSingleton<CommandService>()

View File

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

View File

@@ -25,6 +25,28 @@ namespace EventBot.Services
_discord.ReactionAdded += ReactionAddedAsync; _discord.ReactionAdded += ReactionAddedAsync;
_discord.MessageDeleted += MessageDeletedAsync; _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) public async Task TryJoinEvent(IGuildUser user, EventRole er, string extra, bool extraChecks = true)
@@ -33,13 +55,15 @@ namespace EventBot.Services
throw new Exception("Cross server events are forbidden."); throw new Exception("Cross server events are forbidden.");
if (extraChecks && er.ReamainingOpenings <= 0) if (extraChecks && er.ReamainingOpenings <= 0)
throw new Exception("No openings are left."); 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."); throw new Exception("You are already participating.");
if(extraChecks && !er.Event.Active) if(extraChecks && !er.Event.Active)
throw new Exception("Event is closed."); throw new Exception("Event is closed.");
if (er.Event.Guild.ParticipantRoleId != 0) if (er.Event.Guild.ParticipantRoleId != 0)
await user.AddRoleAsync(user.Guild.GetRole(er.Event.Guild.ParticipantRoleId)); 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() var ep = new EventParticipant()
{ {
@@ -63,6 +87,42 @@ namespace EventBot.Services
await (await user.Guild.GetTextChannelAsync(er.Event.Guild.EventRoleConfirmationChannelId)).SendMessageAsync(embed: embed.Build()); 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) public Event FindEventBy(IGuild guild, bool bypassActive = false)
{ {
return _database.Events.OrderByDescending(e => e.Opened).FirstOrDefault(e => e.GuildId == guild.Id && (e.Active || bypassActive)); return _database.Events.OrderByDescending(e => e.Opened).FirstOrDefault(e => e.GuildId == guild.Id && (e.Active || bypassActive));