-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate other commands and add checks
- Loading branch information
Showing
11 changed files
with
179 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
ModularAssistentForDiscordServer/CommandsChecks/EnsureDBEntitiesCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 Plerx2493 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License") | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using DSharpPlus.Commands; | ||
using DSharpPlus.Commands.ContextChecks; | ||
using DSharpPlus.Entities; | ||
using MADS.Entities; | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace MADS.CommandsChecks; | ||
|
||
public class EnsureDBEntitiesCheck : IContextCheck<UnconditionalCheckAttribute> | ||
{ | ||
private IDbContextFactory<MadsContext> _contextFactory; | ||
|
||
public EnsureDBEntitiesCheck(IDbContextFactory<MadsContext> dbContextFactory) | ||
{ | ||
_contextFactory = dbContextFactory; | ||
} | ||
|
||
public async ValueTask<string?> ExecuteCheckAsync(UnconditionalCheckAttribute _, CommandContext context) | ||
{ | ||
DiscordUser user = context.User; | ||
|
||
await using MadsContext dbContext = await _contextFactory.CreateDbContextAsync(); | ||
|
||
UserDbEntity userdbEntity = new() | ||
{ | ||
Id = user.Id, | ||
Username = user.Username, | ||
PreferedLanguage = "en-US" | ||
}; | ||
|
||
await dbContext.Users.Upsert(userdbEntity) | ||
.On(x => x.Id) | ||
.NoUpdate() | ||
.RunAsync(); | ||
|
||
if (context.Guild is null) | ||
{ | ||
return null; | ||
} | ||
|
||
GuildDbEntity guildDbEntity = new() | ||
{ | ||
DiscordId = context.Guild.Id | ||
}; | ||
|
||
await dbContext.Guilds.Upsert(guildDbEntity) | ||
.On(x => x.DiscordId) | ||
.NoUpdate() | ||
.RunAsync(); | ||
|
||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
ModularAssistentForDiscordServer/CommandsChecks/RequireOwnerAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright 2023 Plerx2493 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License") | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using DSharpPlus.Commands.ContextChecks; | ||
|
||
namespace MADS.CommandsChecks; | ||
|
||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class), ] | ||
public class RequireOwnerAttribute : ContextCheckAttribute | ||
{ | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
ModularAssistentForDiscordServer/CommandsChecks/RequireOwnerCheck.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2023 Plerx2493 | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License") | ||
// You may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using DSharpPlus.Commands; | ||
using DSharpPlus.Commands.ContextChecks; | ||
using DSharpPlus.Entities; | ||
|
||
namespace MADS.CommandsChecks; | ||
|
||
public class RequireOwnerCheck : IContextCheck<RequireOwnerAttribute> | ||
{ | ||
public ValueTask<string?> ExecuteCheckAsync(RequireOwnerAttribute attribute, CommandContext context) | ||
{ | ||
DiscordApplication app = context.Client.CurrentApplication; | ||
DiscordUser me = context.Client.CurrentUser; | ||
|
||
bool isOwner = app is not null ? app!.Owners!.Any(x => x.Id == context.User.Id) : context.User.Id == me.Id; | ||
|
||
if (!isOwner) | ||
{ | ||
return ValueTask.FromResult<string?>("User must be on of the application owner"); | ||
} | ||
|
||
return ValueTask.FromResult<string?>(null); | ||
} | ||
} |
Oops, something went wrong.