我只发现了一个有类似问题的帖子,但是他们的问题是他们忘记了将服务传递给AddModulesAsync()
方法。当我启动机器人时,它会引发InvalidOperationException
System.InvalidOperationException
HResult=0x80131509
Message=Failed to create "CraigTheBot.Bot.Objects.Item", dependency "String" was not found.
(其余错误仅涉及模块无法加载的主类)
无法加载的类是这样的:
public class Item : ModuleBase
{
public string ID { get; set; }
public string Name { get; set; }
public long Price { get; set; }
public string Command { get; set; }
public string ServerID { get; set; }
public string Description { get; set; }
public string MinRank { get; set; }
public static List<Item> GetItemsFromServer(string ServerID)
{
var database = DBConnector.Instance;
DataTable table = database.GetDBObjects($"SELECT * FROM Item WHERE ServerID = {ServerID}");
var itemList = table.AsEnumerable().Select(row =>
new Item
{
ID = row.Field<string>("ItemID"),
Name = row.Field<string>("ItemName"),
Price = row.Field<long>("Price"),
Command = row.Field<string>("CommandOnUse"),
ServerID = row.Field<string>("ServerID"),
MinRank = row.Field<string>("RankRequired")
}).ToList();
return itemList;
}
private static Random random = new Random();
public static string RandomString(int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
return new string(Enumerable.Repeat(chars, length)
.Select(s => s[random.Next(s.Length)]).ToArray());
}
[Command("Item create")]
public async Task CreateItem(params string[] args)
{
var db = DBConnector.Instance;
Item item = new Item();
foreach (var str in args)
{
var prop = str.Split(':');
var propName = prop[0].ToLower();
var propValue = prop[1].ToLower();
while (true)
{
string ID = RandomString(10);
if (db.GetDBData($"SELECT ItemID FROM Item WHERE ItemID = {ID}").Count == 0)
{
item.ID = ID;
break;
}
}
switch (propName)
{
case "name":
item.Name = propValue;
break;
case "description":
item.Description = propValue;
break;
case "command":
item.Command = propValue;
break;
case "price":
try
{
item.Price = Convert.ToInt64(propValue);
}
catch (Exception)
{
item = null;
Craig.Instance.Say("I'm sorry, this is not a whole number", Context.Channel);
}
break;
case "minrank":
item.MinRank = propValue;
break;
}
}
db.ExecuteCommand($"INSERT INTO Item (ItemID, ItemName, Price, CommandOnUse, RankRequired, ServerID, Description) VALUES ({item.ID}, {item.Name}, {item.Price}, {item.Command}, {item.MinRank}, {Context.Guild.Id.ToString()}, {item.Description})");
Craig.Instance.Say($"Item {item.Name} successfully added.", Context.Channel);
}
}
但是我不知道错误消息指的是什么“字符串”。
编辑:我仍然没有发现错误,但是我将类分为Item和ItemModule,至少现在可以使用