英文:
Program ends in one exact spot for unknown reason
问题
这是您提供的代码部分的翻译:
我知道这是因为断点在这里'这是代码:
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using Newtonsoft.Json;
using System.Text;
interface IPermsLevel
{
PermissionsLevel Suspended { get; set; }
PermissionsLevel Unverified { get; set; }
PermissionsLevel Member { get; set; }
PermissionsLevel VIP { get; set; }
PermissionsLevel Administrator { get; set; }
}
internal struct PermissionsLevel
{
internal enum Permslvl : sbyte
{
Awaiting = -127,
Suspended = -2,
Unverified = -1,
Member = 0,
VIP = 1,
Administrator = 2,
}
internal async Task<bool> UserAccessControl(Permslvl AccessLvl, Permslvl MinimumAccessLvl, Permslvl[]? BlockedAccessLvls = null)
{
PermissionsLevel.Permslvl permslvl = AccessLvl;
PermissionsLevel.Permslvl RequestedPermLvl = MinimumAccessLvl;
PermissionsLevel.Permslvl[] Blocked = BlockedAccessLvls ?? new PermissionsLevel.Permslvl[] { PermissionsLevel.Permslvl.Suspended };
List<PermissionsLevel.Permslvl> Bypass = new() { PermissionsLevel.Permslvl.Administrator };
if (Blocked.Length == 0)
{
Blocked = new PermissionsLevel.Permslvl[] { PermissionsLevel.Permslvl.Suspended };
}
if (Bypass.Contains(permslvl) || permslvl == RequestedPermLvl)
{
if (!Blocked.Contains(permslvl) || Bypass.Contains(permslvl))
{
await Console.Out.WriteLineAsync("Access Granted");
return true;
}
else
{
await Console.Out.WriteLineAsync("Access Denied");
return false;
}
}
else
{
await Console.Out.WriteLineAsync("Access Denied");
return false;
}
}
}
internal class AccessException : Exception
{
AccessException(string message, Exception? innerException) : base()
{
if (User.AccessExceptionFlagged == true && User.user.AccessLvl != PermissionsLevel.Permslvl.Suspended)
{
throw new AccessException("用户没有足够高的权限角色来访问此方法!", null);
}
else
{
throw an AccessException("你被暂停了!", null);
}
}
}
internal class User
{
internal string Name { get; set; } = "Deciding";
internal PermissionsLevel.Permslvl AccessLvl { get; set; } = PermissionsLevel.Permslvl.Awaiting;
internal static bool AccessExceptionFlagged = false;
internal static User user = new();
internal async static void UserGetInfo()
{
Console.Write("输入名称:");
string Name = Console.ReadLine() ?? "Unknown";
Console.WriteLine("通过discord验证");
Console.ReadKey();
Discord discord = new();
PermissionsLevel permissions = new();
discord.RunAsync().GetAwaiter();
bool Continue = true;
while (user.AccessLvl == PermissionsLevel.Permslvl.Awaiting && Continue == true)
{
await Task.Delay(1000);
if (user.AccessLvl != PermissionsLevel.Permslvl.Awaiting)
{
Continue = false;
}
}
try
{
if (Continue == false)
{
bool V = await permissions.UserAccessControl(user.AccessLvl, PermissionsLevel.Permslvl.Unverified);
AccessExceptionFlagged = V;
await Console.Out.WriteLineAsync("你好");
}
}
catch (AccessException ex)
{
await Console.Out.WriteLineAsync($"错误:你没有满足访问用户方法的要求,抱歉 错误: {ex}");
await Console.Out.WriteLineAsync("作弊者");
}
finally
{
UserGetInfo();
}
}
}
internal class Discord
{
internal DiscordClient Client { get; private set; }
internal InteractivityExtension Interactivity { get; private set; }
internal CommandsNextExtension Commands { get; private set; }
internal async Task RunAsync()
{
var json = string.Empty;
using (var fs = File.OpenRead(@"C:\Program Files (x86)\AFLLiveCoLTD\config.json"))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync();
var ConfigJson = JsonConvert.DeserializeObject<ConfigJSON>(json);
var config = new DiscordConfiguration()
{
Intents = DiscordIntents.All,
Token = ConfigJson.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
};
Client = new DiscordClient(config);
Client.UseInteractivity(new InteractivityConfiguration()
{
Timeout = TimeSpan.FromMinutes(1)
});
var commandsConfig = new CommandsNextConfiguration()
{
StringPrefixes = new string[] { ConfigJson.Prefix },
EnableMentionPrefix = true,
EnableDms = true,
};
Commands = Client.UseCommandsNext(commandsConfig);
Commands.RegisterCommands<PermCommands>();
await Client.ConnectAsync();
await Task.Delay(-1);
}
private Task OnClientReady(ReadyEventArgs e)
{
return Task.CompletedTask;
}
internal struct ConfigJSON
{
[JsonProperty("token")]
internal string Token { get; private set; }
[JsonProperty("prefix")]
internal string Prefix { get; private set; }
}
}
internal class PermCommands : BaseCommandModule
{
[Command("verifyplvl")]
internal async Task Permlvl(CommandContext ctx)
{
var oermissions = new User();
IEnumerable<DiscordRole>? role = ctx.Member.Roles;
if (role.Any() == false)
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Unverified;
}
else
{
if (role.Any(r => r.Id == 894527137250422795))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Administrator;
}
else if (role.Any(r => r.Id == 894526951119802368))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Administrator;
}
else if (role.Any(r => r.Id == 894525017780551691))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.VIP;
}
else if (role.Any(r => r.Id == 894522335640580196))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Member;
}
else if (role.Any(r => r.Id == 895258073239785472))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Suspended;
}
}
await Console.Out.WriteLineAsync(User.user.AccessLvl
<details>
<summary>英文:</summary>
I know this because of breakpoints here's code:
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using Newtonsoft.Json;
using System.Text;
interface IPermsLevel
{
PermissionsLevel Suspended { get; set; }
PermissionsLevel Unverified { get; set; }
PermissionsLevel Member { get; set; }
PermissionsLevel VIP { get; set; }
PermissionsLevel Administrator { get; set; }
}
internal struct PermissionsLevel
{
internal enum Permslvl : sbyte
{
Awaiting = -127,
Suspended = -2,
Unverified = -1,
Member = 0,
VIP = 1,
Administrator = 2,
}
internal async Task<bool> UserAccessControl(Permslvl AccessLvl, Permslvl MinimumAccessLvl, Permslvl[]? BlockedAccessLvls = null)
{
PermissionsLevel.Permslvl permslvl = AccessLvl;
PermissionsLevel.Permslvl RequestedPermLvl = MinimumAccessLvl;
PermissionsLevel.Permslvl[] Blocked = BlockedAccessLvls ?? new PermissionsLevel.Permslvl[] { PermissionsLevel.Permslvl.Suspended };
List<PermissionsLevel.Permslvl> Bypass = new() { PermissionsLevel.Permslvl.Administrator };
if (Blocked.Length == 0)
{
Blocked = new PermissionsLevel.Permslvl[] { PermissionsLevel.Permslvl.Suspended };
}
if (Bypass.Contains(permslvl) || permslvl == RequestedPermLvl)
{
if (!Blocked.Contains(permslvl) || Bypass.Contains(permslvl))
{
await Console.Out.WriteLineAsync("Access Granted");
return true;
}
else
{
await Console.Out.WriteLineAsync("Access Denied");
return false;
}
}
else
{
await Console.Out.WriteLineAsync("Acess Denied");
return false;
}
}
}
internal class AccessException : Exception
{
AccessException(string message, Exception? innerException) : base()
{
if (User.AccessExceptionFlagged == true && User.user.AccessLvl != PermissionsLevel.Permslvl.Suspended)
{
throw new AccessException("User did not have a role with high enough permissions to access this method!", null);
}
else
{
throw new AccessException("YOU ARE SUSPENDED", null);
}
}
}
internal class User
{
internal string Name { get; set; } = "Deciding";
internal PermissionsLevel.Permslvl AccessLvl { get; set; } = PermissionsLevel.Permslvl.Awaiting;
internal static bool AccessExceptionFlagged = false;
internal static User user = new();
internal async static void UserGetInfo()
{
Console.Write("ENTER NAME:");
string Name = Console.ReadLine() ?? "Unknown";
Console.WriteLine("Verify with discord");
Console.ReadKey();
Discord discord = new();
PermissionsLevel permissions = new();
discord.RunAsync().GetAwaiter();
bool Continue = true;
while (user.AccessLvl == PermissionsLevel.Permslvl.Awaiting && Continue == true)
{
await Task.Delay(1000);
if (user.AccessLvl != PermissionsLevel.Permslvl.Awaiting)
{
Continue = false;
}
}
try
{
if (Continue == false)
{
bool V = await permissions.UserAccessControl(user.AccessLvl, PermissionsLevel.Permslvl.Unverified);
AccessExceptionFlagged = V;
await Console.Out.WriteLineAsync("Hello");
}
}
catch (AccessException ex)
{
await Console.Out.WriteLineAsync($"Error You didn't meet requirements to access User method sorry ERROR: {ex}");
await Console.Out.WriteLineAsync("Cheater");
}
finally
{
UserGetInfo();
}
}
}
internal class Discord
{
internal DiscordClient Client { get; private set; }
internal InteractivityExtension Interactivity { get; private set; }
internal CommandsNextExtension Commands { get; private set; }
internal async Task RunAsync()
{
var json = string.Empty;
using (var fs = File.OpenRead(@"C:\Program Files (x86)\AFLLiveCoLTD\config.json"))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync();
var ConfigJson = JsonConvert.DeserializeObject<ConfigJSON>(json);
var config = new DiscordConfiguration()
{
Intents = DiscordIntents.All,
Token = ConfigJson.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
};
Client = new DiscordClient(config);
Client.UseInteractivity(new InteractivityConfiguration()
{
Timeout = TimeSpan.FromMinutes(1)
});
var commandsConfig = new CommandsNextConfiguration()
{
StringPrefixes = new string[] { ConfigJson.Prefix },
EnableMentionPrefix = true,
EnableDms = true,
};
Commands = Client.UseCommandsNext(commandsConfig);
Commands.RegisterCommands<PermCommands>();
await Client.ConnectAsync();
await Task.Delay(-1);
}
private Task OnClientReady(ReadyEventArgs e)
{
return Task.CompletedTask;
}
internal struct ConfigJSON
{
[JsonProperty("token")]
internal string Token { get; private set; }
[JsonProperty("prefix")]
internal string Prefix { get; private set; }
}
}
internal class PermCommands : BaseCommandModule
{
[Command("verifyplvl")]
internal async Task Permlvl(CommandContext ctx)
{
var oermissions = new User();
IEnumerable<DiscordRole>? role = ctx.Member.Roles;
if (role.Any() == false)
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Unverified;
}
else
{
if (role.Any(r => r.Id == 894527137250422795))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Administrator;
}
else if (role.Any(r => r.Id == 894526951119802368))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Administrator;
}
else if (role.Any(r => r.Id == 894525017780551691))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.VIP;
}
else if (role.Any(r => r.Id == 894522335640580196))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Member;
}
else if (role.Any(r => r.Id == 895258073239785472))
{
User.user.AccessLvl = PermissionsLevel.Permslvl.Suspended;
}
}
await Console.Out.WriteLineAsync(User.user.AccessLvl.ToString());
await ctx.Channel.SendMessageAsync($"Your Permission Level is {User.user.AccessLvl}");
}
}
class Program
{
public static void Main()
{
User.UserGetInfo();
}
}
I've been trying to find a way to make it wait for the discord bot to finish the command because it changes a value that's required for a method to spit out the information a method would want but now I have a strange issue there are no errors but it exists right when it does what I said earlier. it's supposed to keep running it's a discord bot after all and also it's supposed to execute the method `UserAccessConstrol();` after the command is requested in the discord server. Sorry for the messy code, this was a test to see how things work. It ends at `json = await sr.ReadToEndAsync();` there isn't a null exception happening when I run the program but I'll debug it in the future for exception proofing but this is not the issue the issue is that the program exits in `json = await sr.ReadToEndAsync();` and want the rest of the `discord.RunAsync();` to run also how it got this bad and went from working to not working is that I wanted to wait until value: `internal PermissionsLevel.Permslvl AccessLvl { get; set; } = PermissionsLevel.Permslvl.Awaiting;` has changed form awaiting to any value so method: `internal async Task<bool> UserAccessControl(Permslvl AccessLvl, Permslvl MinimumAccessLvl, Permslvl[]? BlockedAccessLvls = null)` has a value that the user actually has because if it runs early it will work but it will not match to what the user's role/access level actually is.
</details>
# 答案1
**得分**: 1
1) 将UserGetInfo的返回类型更改为Task而不是void
```csharp
internal async static Task UserGetInfo()
- 更改您的主方法如下:
static void Main(string[] args)
{
Task.Run(async () =>
{
await User.UserGetInfo();
}).GetAwaiter().GetResult();
}
最后,在finally块中添加await:
finally
{
await UserGetInfo();
}
编辑:
您还可以使用async main来使代码更加清晰:
public static async Task Main()
{
await User.UserGetInfo();
}
英文:
- Make UserGetInfo returning Task instead of void
internal async static Task UserGetInfo()
- Change your main method to be like this :
static void Main(string[] args)
{
Task.Run(async () =>
{
await User.UserGetInfo();
}).GetAwaiter().GetResult();
}
Lastly, add await in the finally block :
finally
{
await UserGetInfo();
}
Edit :
You can also use async main to make the code much cleaner :
public static async Task Main()
{
await User.UserGetInfo();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论