程序由于未知原因在一个确切的位置结束



我知道这是因为断点这里的代码:

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)AFLLiveCoLTDconfig.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();
}
}

我一直在试图找到一种方法,使它等待不和谐机器人完成命令,因为它改变了一个方法吐出一个方法想要的信息所需的值,但现在我有一个奇怪的问题,没有错误,但它存在正确的时候,它做了我之前说的。它应该继续运行,毕竟它是一个不和谐机器人,而且它应该在不和谐服务器请求命令后执行方法UserAccessConstrol();。对不起,代码很乱,这是一个测试,看看事情是如何工作的。它以json = await sr.ReadToEndAsync();结束,当我运行程序时没有发生空异常,但我将在将来调试它以进行异常证明,但这不是问题,问题是程序在json = await sr.ReadToEndAsync();中退出,并希望discord.RunAsync();的其余部分也运行它是如何得到这个坏的,从工作到不工作是我想等到值:internal PermissionsLevel.Permslvl AccessLvl { get; set; } = PermissionsLevel.Permslvl.Awaiting;已经改变了等待任何值的形式,所以方法:internal async Task<bool> UserAccessControl(Permslvl AccessLvl, Permslvl MinimumAccessLvl, Permslvl[]? BlockedAccessLvls = null)有一个用户实际拥有的值,因为如果它早期运行,它将工作,但它与用户的角色/访问级别实际是不匹配的。

  1. 让UserGetInfo返回Task而不是void
internal async static Task UserGetInfo()
  1. 把你的主方法改成这样:
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();
}

相关内容

  • 没有找到相关文章

最新更新