Discord.net Dependency not found

0

I've only found one post that had a similar problem, but their problem was that they forgot to pass the service to the AddModulesAsync() method. When I start my bot, it throws an InvalidOperationException

System.InvalidOperationException
HResult=0x80131509
Message=Failed to create "CraigTheBot.Bot.Objects.Item", dependency "String" was not found.

(The rest of the error just refers to the main class where the module failed to get loaded)

The class that cannot get loaded is this one:

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);
    }
}

But I don't know what "string" the error message refers to.

Edit: I still haven't found the error, but I have split the class into Item, and ItemModule, at least it works now

c#
discord.net
asked on Stack Overflow Jan 23, 2020 by DudeWhoWantsToLearn • edited Jan 24, 2020 by DudeWhoWantsToLearn

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0