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

@@ -56,6 +56,7 @@ namespace EventBot.Modules
[RequireUserPermission(GuildPermission.Administrator, Group = "Permission")]
[RequireOwner(Group = "Permission")]
[Group("event")]
//[Alias("e")]
[Name("Event management")]
public class EventManagementModule : InteractiveBase<SocketCommandContext>
{
@@ -67,6 +68,7 @@ namespace EventBot.Modules
_database = database;
}
[Priority(2)]
[Command("config logchannel")]
[Name("Configure logging channel")]
[Summary("Sets logging channel for role changes.")]
@@ -83,6 +85,7 @@ namespace EventBot.Modules
await s;
}
[Priority(2)]
[Command("config partrole")]
[Name("Configure participant role")]
[Summary("Sets discord role to assign when the user selects a role.")]
@@ -99,6 +102,29 @@ namespace EventBot.Modules
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")]
[Alias("add", "create")]
[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}`.");
}
[Priority(2)]
[Command("update title")]
[Name("Update 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 _events.UpdateEventMessage(@event);
}
[Priority(2)]
[Command("update description")]
[Alias("update desc")]
[Name("Update event's description")]
@@ -162,6 +190,7 @@ namespace EventBot.Modules
await _events.UpdateEventMessage(@event);
}
[Priority(2)]
[Command("update type")]
[Name("Update event's type")]
[Summary("Updates the event type.")]
@@ -186,6 +215,7 @@ namespace EventBot.Modules
}
[Priority(2)]
[Command("role new")]
[Alias("role add", "role create")]
[Name("Add role")]
@@ -219,11 +249,21 @@ namespace EventBot.Modules
Emote = parsedEmote.ToString(),
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);
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}");
}
[Priority(2)]
[Command("role update title")]
[Name("Update role's title")]
[Summary("Updates the role's title.")]
@@ -242,6 +282,7 @@ namespace EventBot.Modules
await _events.UpdateEventMessage(eventRole.Event);
}
[Priority(2)]
[Command("role update description")]
[Alias("role update desc")]
[Name("Update role's description")]
@@ -261,6 +302,7 @@ namespace EventBot.Modules
await _events.UpdateEventMessage(eventRole.Event);
}
[Priority(2)]
[Command("role update slots")]
[Name("Update role's slots")]
[Summary("Updates a role's maximum participants count.")]
@@ -280,6 +322,7 @@ namespace EventBot.Modules
}
[Priority(2)]
[Command("role update emote")]
[Name("Update role's role")]
[Summary("Updates a role's emote.")]
@@ -335,6 +378,33 @@ namespace EventBot.Modules
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)]
[Command("role")]
[Alias("role info")]
@@ -361,6 +431,7 @@ namespace EventBot.Modules
}
}
[Priority(1)]
[Command("open")]
[Alias("start", "begin")]
[Name("Open event")]
@@ -394,7 +465,9 @@ namespace EventBot.Modules
throw new Exception("Event type in not implemented.");
}
}
[Priority(1)]
[Command("close")]
[Alias("stop", "end")]
[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.");
}
[Priority(1)]
[Command("finalize")]
[Alias("archive")]
[Name("Archive event")]
@@ -441,9 +516,18 @@ namespace EventBot.Modules
var user = Context.Guild.GetUser(participant.UserId);
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!");
}
[Priority(1)]
[Command("list")]
[Alias("all")]
[Name("Lists events")]
@@ -478,6 +562,8 @@ namespace EventBot.Modules
await PagedReplyAsync(pager);
}
[Priority(2)]
[Command("participant add")]
[Name("Add participant")]
[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.
}
[Priority(2)]
[Command("participant remove")]
[Alias("participant delete")]
[Name("Remove participant")]
@@ -521,25 +608,15 @@ namespace EventBot.Modules
throw new Exception("No events were found for this discord server.");
if (!@event.Active)
throw new Exception("This event is finalized. Please make a new event.");
if (!(user is IGuildUser guildUser))
if (!(user is IGuildUser guildUser) || !(Context.User is IGuildUser initiator))
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 ReplyAsync($"{guildUser} has been removed from 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));
}
}
}