问题很简单:我试图制造一个不和谐的机器人,它所需要的只是在初始化时加载有关该机器人所在的所有公会的所有信息。
由于一切似乎都很好,我没有尝试任何事情。我不想留下这个硬编码并创建一个不友好的库,这就是为什么我仍在尝试这样做。
这是我到目前为止的内容: [Json文件]
{
"Guilds": [
{
"ID": 561524447828246535,
"GuildName": "TestGuild",
"Filter": [ "words", "words2" ]
}
]
}
[提取类]
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using J = Newtonsoft.Json.JsonPropertyAttribute;
using System;
using System.Collections.Generic;
using System.Globalization;
namespace Cuvue.JSONManagment
{
public static class JSONManager<Generic>
{
public static List<Generic> ListFromJson(string directory) => JsonConvert.DeserializeObject<List<Generic>>(directory, Converter.Settings);
public static Generic TypeFromJson(string directory) => JsonConvert.DeserializeObject<Generic>(directory, Converter.Settings);
public static string IntoJson(Generic type) => JsonConvert.SerializeObject(type, Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
}
[GuildData类]
using J = Newtonsoft.Json.JsonPropertyAttribute;
namespace Cuvue.JSONManagment
{
public partial class GuildData
{
[J("Guilds")] public Guild[] Guilds { get; set; }
}
public partial class Guild
{
[J("ID")] public long Id { get; set; }
[J("GuildName")] public string GuildName { get; set; }
[J("Filter")] public string[] Filter { get; set; }
}
}
[实例化点]
private async Task Async()
{
Client = new DiscordSocketClient(new DiscordSocketConfig
{
AlwaysDownloadUsers = true,
ConnectionTimeout = 2000,
LogLevel = LogSeverity.Debug
});
Commands = new CommandService(new CommandServiceConfig
{
SeparatorChar = ';',
ThrowOnError = false,
DefaultRunMode = RunMode.Async,
CaseSensitiveCommands = true,
LogLevel = LogSeverity.Debug
});
string Token = JSONManager<BotData>.TypeFromJson(BotDataJSON).Token;
Client.MessageReceived += ClientMessageReceived;
await Commands.AddModulesAsync(assembly: Assembly.GetEntryAssembly(), services: null);
Client.Ready += ClientIsReady;
Client.Log += Logging;
await Client.LoginAsync(TokenType.Bot, Token);
await Client.StartAsync();
#region Populating Lists
//Guilds = JSONManager<GuildData>.ListFromJson(GuildDataJSON);
#endregion
await Task.Delay(-1);
}
[错误消息]
'Unexpected character encountered while parsing value: G. Path '', line 0, position 0.'
嗯...呃...我希望JSON反序列化到GuildData类中并应用于GuildList列表。在调试应用程序时崩溃。更不用说接收机器人的令牌时也会发生同样的事情。