mirror of
https://github.com/tribufu/ServerManagers
synced 2026-05-06 15:17:34 +00:00
Bot cleanup
Language file updates
This commit is contained in:
parent
e72f5fb28f
commit
40b85340ae
21 changed files with 258 additions and 129 deletions
41
src/ServerManager.Discord/Attributes/RequireRoleAttribute.cs
Normal file
41
src/ServerManager.Discord/Attributes/RequireRoleAttribute.cs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Discord.WebSocket;
|
||||
|
||||
namespace ServerManagerTool.DiscordBot.Attributes
|
||||
{
|
||||
public class RequireRoleAttribute : PreconditionAttribute
|
||||
{
|
||||
private readonly string _name;
|
||||
|
||||
public RequireRoleAttribute(string name)
|
||||
{
|
||||
_name = name;
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
// Check if this user is a Guild User, which is the only context where roles exist
|
||||
if (context.User is SocketGuildUser guildUser)
|
||||
{
|
||||
// If this command was executed by a user with the appropriate role, return a success
|
||||
if (guildUser.Roles.Any(r => r.Name == _name))
|
||||
{
|
||||
// Since no async work is done, the result has to be wrapped with `Task.FromResult` to avoid compiler errors
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
}
|
||||
else
|
||||
{
|
||||
// Since it wasn't, fail
|
||||
return Task.FromResult(PreconditionResult.FromError($"You must have a role named '{_name}' to run this command."));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue