Auto assign roles

This commit is contained in:
Kwoth 2016-08-25 00:39:02 +02:00
parent 279e189f08
commit 4b080590b2
10 changed files with 97 additions and 79 deletions

View File

@ -8,7 +8,7 @@ using NadekoBot.Services.Database.Impl;
namespace NadekoBot.Migrations
{
[DbContext(typeof(NadekoSqliteContext))]
[Migration("20160824184118_FirstMigration")]
[Migration("20160824223801_FirstMigration")]
partial class FirstMigration
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -21,6 +21,8 @@ namespace NadekoBot.Migrations
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("AutoAssignRoleId");
b.Property<bool>("DeleteMessageOnCommand");
b.Property<ulong>("GuildId");
@ -30,7 +32,7 @@ namespace NadekoBot.Migrations
b.HasIndex("GuildId")
.IsUnique();
b.ToTable("Configs");
b.ToTable("GuildConfigs");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.Donator", b =>

View File

@ -9,17 +9,18 @@ namespace NadekoBot.Migrations
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Configs",
name: "GuildConfigs",
columns: table => new
{
Id = table.Column<int>(nullable: false)
.Annotation("Autoincrement", true),
AutoAssignRoleId = table.Column<ulong>(nullable: false),
DeleteMessageOnCommand = table.Column<bool>(nullable: false),
GuildId = table.Column<ulong>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Configs", x => x.Id);
table.PrimaryKey("PK_GuildConfigs", x => x.Id);
});
migrationBuilder.CreateTable(
@ -55,8 +56,8 @@ namespace NadekoBot.Migrations
});
migrationBuilder.CreateIndex(
name: "IX_Configs_GuildId",
table: "Configs",
name: "IX_GuildConfigs_GuildId",
table: "GuildConfigs",
column: "GuildId",
unique: true);
@ -70,7 +71,7 @@ namespace NadekoBot.Migrations
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Configs");
name: "GuildConfigs");
migrationBuilder.DropTable(
name: "Donators");

View File

@ -20,6 +20,8 @@ namespace NadekoBot.Migrations
b.Property<int>("Id")
.ValueGeneratedOnAdd();
b.Property<ulong>("AutoAssignRoleId");
b.Property<bool>("DeleteMessageOnCommand");
b.Property<ulong>("GuildId");
@ -29,7 +31,7 @@ namespace NadekoBot.Migrations
b.HasIndex("GuildId")
.IsUnique();
b.ToTable("Configs");
b.ToTable("GuildConfigs");
});
modelBuilder.Entity("NadekoBot.Services.Database.Models.Donator", b =>

View File

@ -15,7 +15,6 @@ using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
//todo fix delmsgoncmd
//todo DB
namespace NadekoBot.Modules.Administration
{
[Module(".", AppendSpace = false)]
@ -47,9 +46,9 @@ namespace NadekoBot.Modules.Administration
Config conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.Configs.For(channel.Guild.Id);
conf = uow.GuildConfigs.For(channel.Guild.Id);
conf.DeleteMessageOnCommand = !conf.DeleteMessageOnCommand;
uow.Configs.Update(conf);
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (conf.DeleteMessageOnCommand)
@ -381,8 +380,8 @@ namespace NadekoBot.Modules.Administration
{
var channel = (ITextChannel)imsg.Channel;
//todo actually print info about created channel
await channel.Guild.CreateVoiceChannelAsync(channelName).ConfigureAwait(false);
await channel.SendMessageAsync($"Created voice channel **{channelName}**.").ConfigureAwait(false);
var ch = await channel.Guild.CreateVoiceChannelAsync(channelName).ConfigureAwait(false);
await channel.SendMessageAsync($"Created voice channel **{ch.Name}**, id `{ch.Id}`.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
@ -391,7 +390,7 @@ namespace NadekoBot.Modules.Administration
public async Task DelTxtChanl(IMessage imsg, [Remainder] ITextChannel channel)
{
await channel.DeleteAsync().ConfigureAwait(false);
await channel.SendMessageAsync($"Removed text channel **{channel.Name}**.").ConfigureAwait(false);
await channel.SendMessageAsync($"Removed text channel **{channel.Name}**, id `{channel.Id}`.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
@ -402,7 +401,7 @@ namespace NadekoBot.Modules.Administration
var channel = (ITextChannel)imsg.Channel;
//todo actually print info about created channel
var txtCh = await channel.Guild.CreateTextChannelAsync(channelName).ConfigureAwait(false);
await channel.SendMessageAsync($"Added text channel **{channelName}**.").ConfigureAwait(false);
await channel.SendMessageAsync($"Added text channel **{txtCh.Name}**, id `{txtCh.Id}`.").ConfigureAwait(false);
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
@ -413,7 +412,6 @@ namespace NadekoBot.Modules.Administration
var channel = (ITextChannel)imsg.Channel;
topic = topic ?? "";
await (channel as ITextChannel).ModifyAsync(c => c.Topic = topic);
//await (channel).ModifyAsync(c => c).ConfigureAwait(false);
await channel.SendMessageAsync(":ok: **New channel topic set.**").ConfigureAwait(false);
}

View File

@ -1,54 +0,0 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration
{
//todo DB
public partial class Administration
{
[Group]
public class AutoAssignRole
{
public AutoAssignRole()
{
var _client = NadekoBot.Client;
_client.UserJoined += (user) =>
{
//var config = SpecificConfigurations.Default.Of(e.Server.Id);
//var role = e.Server.Roles.Where(r => r.Id == config.AutoAssignedRole).FirstOrDefault();
//if (role == null)
// return;
//imsg.Author.AddRoles(role);
return Task.CompletedTask;
};
}
//[LocalizedCommand, LocalizedDescription, LocalizedSummary]
//[RequireContext(ContextType.Guild)]
//[RequirePermission(GuildPermission.ManageRoles)]
//public async Task AutoAssignRole(IMessage imsg, IRole role)
//{
// var channel = (ITextChannel)imsg.Channel;
// var config = SpecificConfigurations.Default.Of(e.Server.Id);
// if (string.IsNullOrWhiteSpace(r)) //if role is not specified, disable
// {
// config.AutoAssignedRole = 0;
// await channel.SendMessageAsync("`Auto assign role on user join is now disabled.`").ConfigureAwait(false);
// return;
// }
// config.AutoAssignedRole = role.Id;
// await channel.SendMessageAsync("`Auto assigned role is set.`").ConfigureAwait(false);
//}
}
}
}

View File

@ -0,0 +1,71 @@
using Discord;
using Discord.Commands;
using Discord.WebSocket;
using NadekoBot.Attributes;
using NadekoBot.Services;
using NadekoBot.Services.Database;
using NadekoBot.Services.Database.Models;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace NadekoBot.Modules.Administration
{
public partial class Administration
{
[Group]
public class AutoAssignRoleCommands
{
public AutoAssignRoleCommands()
{
var _client = NadekoBot.Client;
_client.UserJoined += async (user) =>
{
Config conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(user.Guild.Id);
}
var aarType = conf.AutoAssignRoleId.GetType();
if (conf.AutoAssignRoleId == 0)
return;
var role = user.Guild.Roles.Where(r => r.Id == conf.AutoAssignRoleId).FirstOrDefault();
if (role != null)
await user.AddRolesAsync(role);
};
}
[LocalizedCommand, LocalizedDescription, LocalizedSummary]
[RequireContext(ContextType.Guild)]
[RequirePermission(GuildPermission.ManageRoles)]
public async Task AutoAssignRole(IMessage imsg, [Remainder] IRole role = null)
{
var channel = (ITextChannel)imsg.Channel;
Config conf;
using (var uow = DbHandler.UnitOfWork())
{
conf = uow.GuildConfigs.For(channel.Guild.Id);
if (role == null)
conf.AutoAssignRoleId = 0;
else
conf.AutoAssignRoleId = role.Id;
uow.GuildConfigs.Update(conf);
await uow.CompleteAsync();
}
if (role == null)
{
await channel.SendMessageAsync("`Auto assign role on user join is now disabled.`").ConfigureAwait(false);
return;
}
await channel.SendMessageAsync("`Auto assigned role is set.`").ConfigureAwait(false);
}
}
}
}

View File

@ -10,7 +10,7 @@ namespace NadekoBot.Services.Database
public interface IUnitOfWork : IDisposable
{
IQuoteRepository Quotes { get; }
IConfigRepository Configs { get; }
IConfigRepository GuildConfigs { get; }
IDonatorsRepository Donators { get; }
int Complete();
Task<int> CompleteAsync();

View File

@ -9,6 +9,7 @@ namespace NadekoBot.Services.Database.Models
public class Config : DbEntity
{
public ulong GuildId { get; set; }
public bool DeleteMessageOnCommand { get; set; } = false;
public bool DeleteMessageOnCommand { get; set; }
public ulong AutoAssignRoleId { get; set; }
}
}

View File

@ -12,7 +12,7 @@ namespace NadekoBot.Services.Database
{
public DbSet<Quote> Quotes { get; set; }
public DbSet<Donator> Donators { get; set; }
public DbSet<Config> Configs { get; set; }
public DbSet<Config> GuildConfigs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
@ -41,9 +41,6 @@ namespace NadekoBot.Services.Database
#endregion
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite("Filename=./data/NadekoBot.sqlite");
}
protected abstract override void OnConfiguring(DbContextOptionsBuilder optionsBuilder);
}
}

View File

@ -15,8 +15,8 @@ namespace NadekoBot.Services.Database
private IQuoteRepository _quotes;
public IQuoteRepository Quotes => _quotes ?? (_quotes = new QuoteRepository(_context));
private IConfigRepository _configs;
public IConfigRepository Configs => _configs ?? (_configs = new ConfigRepository(_context));
private IConfigRepository _guildConfigs;
public IConfigRepository GuildConfigs => _guildConfigs ?? (_guildConfigs = new ConfigRepository(_context));
private IDonatorsRepository _donators;
public IDonatorsRepository Donators => _donators ?? (_donators = new DonatorsRepository(_context));