More work on permission system, fixes
This commit is contained in:
@@ -96,7 +96,7 @@ namespace NadekoBot.Modules.Gambling
|
||||
{
|
||||
try
|
||||
{
|
||||
await raceChannel.SendMessageAsync($"🏁`Race is starting in 20 seconds or when the room is full. Type {NadekoBot.ModulePrefixes["Gambling"]}jr to join the race.`");
|
||||
await raceChannel.SendMessageAsync($"🏁`Race is starting in 20 seconds or when the room is full. Type {NadekoBot.ModulePrefixes[typeof(Gambling).Name]}jr to join the race.`");
|
||||
var t = await Task.WhenAny(Task.Delay(20000, token), fullgame);
|
||||
Started = true;
|
||||
cancelSource.Cancel();
|
||||
|
@@ -86,7 +86,7 @@ namespace NadekoBot.Modules.Games
|
||||
{
|
||||
var sent = await channel.SendFileAsync(
|
||||
GetRandomCurrencyImagePath(),
|
||||
$"❗ A random { Gambling.Gambling.CurrencyName } appeared! Pick it up by typing `{NadekoBot.ModulePrefixes["Gambling"]}pick`")
|
||||
$"❗ A random { Gambling.Gambling.CurrencyName } appeared! Pick it up by typing `{NadekoBot.ModulePrefixes[typeof(Games).Name]}pick`")
|
||||
.ConfigureAwait(false);
|
||||
plantedFlowers.AddOrUpdate(channel.Id, new List<IUserMessage>() { sent }, (id, old) => { old.Add(sent); return old; });
|
||||
}
|
||||
@@ -148,7 +148,7 @@ namespace NadekoBot.Modules.Games
|
||||
IUserMessage msg;
|
||||
var vowelFirst = new[] { 'a', 'e', 'i', 'o', 'u' }.Contains(Gambling.Gambling.CurrencyName[0]);
|
||||
|
||||
var msgToSend = $"Oh how Nice! **{imsg.Author.Username}** planted {(vowelFirst ? "an" : "a")} {Gambling.Gambling.CurrencyName}. Pick it using {NadekoBot.ModulePrefixes["Games"]}pick";
|
||||
var msgToSend = $"Oh how Nice! **{imsg.Author.Username}** planted {(vowelFirst ? "an" : "a")} {Gambling.Gambling.CurrencyName}. Pick it using {NadekoBot.ModulePrefixes[typeof(Gambling.Gambling).Name]}pick";
|
||||
if (file == null)
|
||||
{
|
||||
msg = await channel.SendMessageAsync(Gambling.Gambling.CurrencySign).ConfigureAwait(false);
|
||||
|
@@ -17,7 +17,7 @@ using NadekoBot.Services.Database;
|
||||
|
||||
namespace NadekoBot.Modules.Music
|
||||
{
|
||||
[NadekoModule("ClashOfClans", "!!")]
|
||||
[NadekoModule("Music", "!!")]
|
||||
public partial class Music : DiscordModule
|
||||
{
|
||||
public static ConcurrentDictionary<ulong, MusicPlayer> MusicPlayers = new ConcurrentDictionary<ulong, MusicPlayer>();
|
||||
|
121
src/NadekoBot/Modules/Permissions/PermissionExtensions.cs
Normal file
121
src/NadekoBot/Modules/Permissions/PermissionExtensions.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using Discord;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Services.Database.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace NadekoBot.Modules.Permissions
|
||||
{
|
||||
public static class PermissionExtensions
|
||||
{
|
||||
public static bool CheckPermissions(this IEnumerable<Permission> permsEnumerable, IUserMessage message, Command command)
|
||||
{
|
||||
var perms = permsEnumerable as List<Permission> ?? permsEnumerable.ToList();
|
||||
int throwaway;
|
||||
return perms.CheckPermissions(message, command, out throwaway);
|
||||
}
|
||||
|
||||
public static bool CheckPermissions(this IEnumerable<Permission> permsEnumerable, IUserMessage message, Command command, out int permIndex)
|
||||
{
|
||||
var perms = permsEnumerable as List<Permission> ?? permsEnumerable.ToList();
|
||||
|
||||
for (int i = 0; i < perms.Count; i++)
|
||||
{
|
||||
var perm = perms[i];
|
||||
|
||||
var result = perm.CheckPermission(message, command);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
permIndex = i + 1;
|
||||
return result.Value;
|
||||
}
|
||||
}
|
||||
permIndex = -1; //defaut behaviour
|
||||
return true;
|
||||
}
|
||||
|
||||
//null = not applicable
|
||||
//true = applicable, allowed
|
||||
//false = applicable, not allowed
|
||||
public static bool? CheckPermission(this Permission perm, IUserMessage message, Command command)
|
||||
{
|
||||
if (!((perm.SecondaryTarget == SecondaryPermissionType.Command &&
|
||||
perm.SecondaryTargetName == command.Text.ToLowerInvariant()) ||
|
||||
(perm.SecondaryTarget == SecondaryPermissionType.Module &&
|
||||
perm.SecondaryTargetName == command.Module.Name.ToLowerInvariant())))
|
||||
return null;
|
||||
|
||||
switch (perm.PrimaryTarget)
|
||||
{
|
||||
case PrimaryPermissionType.User:
|
||||
if (perm.PrimaryTargetId == message.Author.Id)
|
||||
return perm.State;
|
||||
break;
|
||||
case PrimaryPermissionType.Channel:
|
||||
if (perm.PrimaryTargetId == message.Channel.Id)
|
||||
return perm.State;
|
||||
break;
|
||||
case PrimaryPermissionType.Role:
|
||||
var guildUser = message.Author as IGuildUser;
|
||||
if (guildUser == null)
|
||||
break;
|
||||
if (guildUser.Roles.Any(r => r.Id == perm.PrimaryTargetId))
|
||||
return perm.State;
|
||||
break;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static string GetCommand(this Permission perm)
|
||||
{
|
||||
var com = NadekoBot.ModulePrefixes[typeof(Permissions).Name];
|
||||
switch (perm.PrimaryTarget)
|
||||
{
|
||||
case PrimaryPermissionType.User:
|
||||
com += "u";
|
||||
break;
|
||||
case PrimaryPermissionType.Channel:
|
||||
com += "c";
|
||||
break;
|
||||
case PrimaryPermissionType.Role:
|
||||
com += "r";
|
||||
break;
|
||||
}
|
||||
|
||||
switch (perm.SecondaryTarget)
|
||||
{
|
||||
case SecondaryPermissionType.Module:
|
||||
com += "m";
|
||||
break;
|
||||
case SecondaryPermissionType.Command:
|
||||
com += "c";
|
||||
break;
|
||||
}
|
||||
com += " " + perm.SecondaryTargetName + " " + (perm.State ? "enable" : "disable") + " ";
|
||||
|
||||
switch (perm.PrimaryTarget)
|
||||
{
|
||||
case PrimaryPermissionType.User:
|
||||
com += $"<@{perm.PrimaryTargetId}>";
|
||||
break;
|
||||
case PrimaryPermissionType.Channel:
|
||||
com += $"<#{perm.PrimaryTargetId}>";
|
||||
break;
|
||||
case PrimaryPermissionType.Role:
|
||||
com += $"<@&{perm.PrimaryTargetId}>";
|
||||
break;
|
||||
}
|
||||
|
||||
return com;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -20,6 +20,27 @@ namespace NadekoBot.Modules.Permissions
|
||||
{
|
||||
}
|
||||
|
||||
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
|
||||
[RequireContext(ContextType.Guild)]
|
||||
public async Task ListPerms(IUserMessage msg)
|
||||
{
|
||||
var channel = (ITextChannel)msg.Channel;
|
||||
|
||||
string toSend = "";
|
||||
using (var uow = DbHandler.UnitOfWork())
|
||||
{
|
||||
var perms = uow.GuildConfigs.For(channel.Guild.Id).Permissions;
|
||||
|
||||
var i = 1;
|
||||
toSend = String.Join("\n", perms.Select(p => $"`{(i++)}.` {p.GetCommand()}"));
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(toSend))
|
||||
await channel.SendMessageAsync("`No permissions set.`").ConfigureAwait(false);
|
||||
else
|
||||
await channel.SendMessageAsync(toSend).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
|
||||
[RequireContext(ContextType.Guild)]
|
||||
public async Task UsrCmd(IUserMessage imsg, Command command, PermissionAction action, IGuildUser user)
|
||||
@@ -30,14 +51,15 @@ namespace NadekoBot.Modules.Permissions
|
||||
{
|
||||
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
|
||||
{
|
||||
TargetType = PermissionType.User,
|
||||
Target = user.Id.ToString(),
|
||||
Command = command.Text.ToLowerInvariant(),
|
||||
PrimaryTarget = PrimaryPermissionType.User,
|
||||
PrimaryTargetId = user.Id,
|
||||
SecondaryTarget = SecondaryPermissionType.Command,
|
||||
SecondaryTargetName = command.Text.ToLowerInvariant(),
|
||||
State = action.Value,
|
||||
});
|
||||
await uow.CompleteAsync();
|
||||
await uow.CompleteAsync().ConfigureAwait(false);
|
||||
}
|
||||
await channel.SendMessageAsync($"{(action.Value ? "Allowed" : "Denied")} usage of `{command.Text}` command for `{user}` user.");
|
||||
await channel.SendMessageAsync($"{(action.Value ? "Allowed" : "Denied")} usage of `{command.Text}` command for `{user}` user.").ConfigureAwait(false);
|
||||
}
|
||||
|
||||
[LocalizedCommand, LocalizedDescription, LocalizedSummary, LocalizedAlias]
|
||||
@@ -50,14 +72,15 @@ namespace NadekoBot.Modules.Permissions
|
||||
{
|
||||
uow.GuildConfigs.For(channel.Guild.Id).Permissions.Add(new Permission
|
||||
{
|
||||
TargetType = PermissionType.User,
|
||||
Target = user.Id.ToString(),
|
||||
Module = module.Name.ToLowerInvariant(),
|
||||
PrimaryTarget = PrimaryPermissionType.User,
|
||||
PrimaryTargetId = user.Id,
|
||||
SecondaryTarget = SecondaryPermissionType.Module,
|
||||
SecondaryTargetName = module.Name.ToLowerInvariant(),
|
||||
State = action.Value,
|
||||
});
|
||||
await uow.CompleteAsync();
|
||||
await uow.CompleteAsync().ConfigureAwait(false);
|
||||
}
|
||||
await channel.SendMessageAsync($"{(action.Value ? "Allowed" : "Denied")} usage of `{module.Name}` module for `{user}` user.");
|
||||
await channel.SendMessageAsync($"{(action.Value ? "Allowed" : "Denied")} usage of `{module.Name}` module for `{user}` user.").ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user