Skip to content

Commit

Permalink
feat!: new DisCatSharp.ApplicationCommands Attributes
Browse files Browse the repository at this point in the history
- Added ApplicationCommandRequireTeamMember
- Added ApplicationCommandRequireTeamReadOnly
- Added ApplicationCommandRequireTeamDeveloper
- Added ApplicationCommandRequireTeamAdmin
- Added ApplicationCommandRequireTeamOwner
- Added ApplicationCommandRequireGuildOwner

- Deprecated ApplicationCommandRequireOwner
- Deprecated ApplicationCommandRequireOwnerOrId
  • Loading branch information
Lulalaby committed Sep 16, 2023
1 parent 67a521f commit c52ba11
Show file tree
Hide file tree
Showing 8 changed files with 330 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is only usable within a guild by its owner.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireGuildOwnerAttributeAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this command is only usable within a guild by its owner.
/// </summary>
public ApplicationCommandRequireGuildOwnerAttributeAttribute()
{ }

/// <summary>
/// Runs checks.
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
=> Task.FromResult(ctx.Guild is not null && ctx.Guild.OwnerId == ctx.User.Id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,20 @@
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;
using DisCatSharp.Attributes;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is restricted to the owner of the bot.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
[Deprecated("This is deprecated and will be remove in future in favor of RequireTeamXY"), AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireOwnerAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this application command is restricted to the owner of the bot.
/// </summary>
[Deprecated("This is deprecated and will be remove in future in favor of RequireTeamXY")]
public ApplicationCommandRequireOwnerAttribute()
{ }

Expand All @@ -45,9 +47,9 @@ public ApplicationCommandRequireOwnerAttribute()
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication;
var me = ctx.Client.CurrentUser;
var app = ctx.Client.CurrentApplication!;
var me = ctx.Client.CurrentUser!;

return app != null ? Task.FromResult(app.Owners.Any(x => x.Id == ctx.User.Id)) : Task.FromResult(ctx.User.Id == me.Id);
return app != null ? Task.FromResult(app.Members.Any(x => x.Id == ctx.User.Id)) : Task.FromResult(ctx.User.Id == me.Id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp;
using DisCatSharp.ApplicationCommands.Context;
using DisCatSharp.Attributes;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Requires ownership of the bot or a whitelisted id to execute this command.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
[Deprecated("This is deprecated and will be remove in future in favor of RequireTeamXY"), AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireOwnerOrIdAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
Expand All @@ -46,6 +46,7 @@ public sealed class ApplicationCommandRequireOwnerOrIdAttribute : ApplicationCom
/// Defines that usage of this command is restricted to the owner or whitelisted ids of the bot.
/// </summary>
/// <param name="userIds">List of allowed user ids</param>
[Deprecated("This is deprecated and will be remove in future in favor of RequireTeamXY")]
public ApplicationCommandRequireOwnerOrIdAttribute(params ulong[] userIds)
{
this.UserIds = new ReadOnlyCollection<ulong>(userIds);
Expand All @@ -57,10 +58,10 @@ public ApplicationCommandRequireOwnerOrIdAttribute(params ulong[] userIds)
/// <param name="ctx">The command context.</param>s
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication;
var me = ctx.Client.CurrentUser;
var app = ctx.Client.CurrentApplication!;
var me = ctx.Client.CurrentUser!;

var owner = app != null ? Task.FromResult(app.Owners.Any(x => x.Id == ctx.User.Id)) : Task.FromResult(ctx.User.Id == me.Id);
var owner = app != null ? Task.FromResult(app.Members.Any(x => x.Id == ctx.User.Id)) : Task.FromResult(ctx.User.Id == me.Id);

var allowed = this.UserIds.Contains(ctx.User.Id);

Expand Down
55 changes: 55 additions & 0 deletions DisCatSharp.ApplicationCommands/Attributes/RequireTeamAdmin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is restricted to team members of the bot with admin role or higher.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireTeamAdminAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this application command is restricted to team members of the bot with admin role or higher.
/// </summary>
public ApplicationCommandRequireTeamAdminAttribute()
{ }

/// <summary>
/// Runs checks.
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication!;
if (app.Team is null)
return Task.FromResult(app.Owner.Id == ctx.User.Id);

var teamMember = app.Team?.Members.FirstOrDefault(x => x.User.Id == ctx.User.Id);
return teamMember == null ? Task.FromResult(false) : Task.FromResult(teamMember.Role is "admin" or "owner");
}
}
55 changes: 55 additions & 0 deletions DisCatSharp.ApplicationCommands/Attributes/RequireTeamDeveloper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is restricted to team members of the bot with developer role or higher.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireTeamDeveloperAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this application command is restricted to team members of the bot with developer role or higher.
/// </summary>
public ApplicationCommandRequireTeamDeveloperAttribute()
{ }

/// <summary>
/// Runs checks.
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication!;
if (app.Team is null)
return Task.FromResult(app.Owner.Id == ctx.User.Id);

var teamMember = app.Team?.Members.FirstOrDefault(x => x.User.Id == ctx.User.Id);
return teamMember == null ? Task.FromResult(false) : Task.FromResult(teamMember.Role is "developer" or "admin" or "owner");
}
}
51 changes: 51 additions & 0 deletions DisCatSharp.ApplicationCommands/Attributes/RequireTeamMember.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is restricted to the team members of the bot.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireTeamMemberAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this application command is restricted to the team members of the bot.
/// </summary>
public ApplicationCommandRequireTeamMemberAttribute()
{ }

/// <summary>
/// Runs checks.
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication!;
return app.Team is null ? Task.FromResult(app.Owner.Id == ctx.User.Id) : Task.FromResult(app.Members.Any(x => x.Id == ctx.User.Id));
}
}
55 changes: 55 additions & 0 deletions DisCatSharp.ApplicationCommands/Attributes/RequireTeamOwner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is restricted to team members of the bot with owner role.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireTeamOwnerAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this application command is restricted to team members of the bot with owner role.
/// </summary>
public ApplicationCommandRequireTeamOwnerAttribute()
{ }

/// <summary>
/// Runs checks.
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication!;
if (app.Team is null)
return Task.FromResult(app.Owner.Id == ctx.User.Id);

var teamMember = app.Team.Members.FirstOrDefault(x => x.User.Id == ctx.User.Id);
return teamMember == null ? Task.FromResult(false) : Task.FromResult(teamMember.Role is "owner");
}
}
55 changes: 55 additions & 0 deletions DisCatSharp.ApplicationCommands/Attributes/RequireTeamReadOnly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This file is part of the DisCatSharp project.
//
// Copyright (c) 2021-2023 AITSYS
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

using System;
using System.Linq;
using System.Threading.Tasks;

using DisCatSharp.ApplicationCommands.Context;

namespace DisCatSharp.ApplicationCommands.Attributes;

/// <summary>
/// Defines that this application command is restricted to team members of the bot with read-only role or higher.
/// </summary>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = false)]
public sealed class ApplicationCommandRequireTeamReadOnlyAttribute : ApplicationCommandCheckBaseAttribute
{
/// <summary>
/// Defines that this application command is restricted to team members of the bot with read-only role or higher.
/// </summary>
public ApplicationCommandRequireTeamReadOnlyAttribute()
{ }

/// <summary>
/// Runs checks.
/// </summary>
public override Task<bool> ExecuteChecksAsync(BaseContext ctx)
{
var app = ctx.Client.CurrentApplication!;
if (app.Team is null)
return Task.FromResult(app.Owner.Id == ctx.User.Id);

var teamMember = app.Team?.Members.FirstOrDefault(x => x.User.Id == ctx.User.Id);
return teamMember == null ? Task.FromResult(false) : Task.FromResult(teamMember.Role is "read-only" or "developer" or "admin" or "owner");
}
}

0 comments on commit c52ba11

Please sign in to comment.