如何为Discord Bot的不同行会/服务器保留数据

时间:2018-10-10 13:14:55

标签: c# discord discord.net

我目前正在进行一个项目,在这个项目中,我试图为不和谐的机器人制作RPG。我目前正在努力实现一种将不同服务器的数据分开保存的方法。例如,我试图存储每个服务器的聚会位置。我已经尝试了从“城镇”到“森林”的测试。它可以在使用该命令的服务器上运行,但是该漫游器所在的所有其他服务器的位置也都更新为“林”。由于我也是c#的新手,因此我正在努力寻找一种方法来保持每台服务器上的位置更新。

可能的解决方案是将每个公会的对象存储在数组中,并在需要公会特定数据时引用该对象,但这似乎不是一个很好的解决方案。

实现行会之间数据分离的最佳方法是什么?

主要

    using Discord;
    using Discord.Commands;
    using Discord.WebSocket;
    using Microsoft.Extensions.DependencyInjection;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    using TestBot2.Modules;

    namespace TestBot2
    {
        class Program
        {
    static void Main(string[] args){
         new Program().RunBotAsync().GetAwaiter().GetResult();
    }

    private DiscordSocketClient _client;
    private CommandService _command;
    private IServiceProvider _service;

    public async Task RunBotAsync()
    {
        _client = new DiscordSocketClient();
        _command = new CommandService();
        _service = new ServiceCollection()
            .AddSingleton(_client)
            .AddSingleton(_command)
            .BuildServiceProvider();




        string botToken = *** BOT TOKEN ***;

        //event subscription
        _client.Log += Log;


        await RegisterCommandAsync();

        await _client.LoginAsync(TokenType.Bot, botToken);

        await _client.StartAsync();



        await Task.Delay(-1);
    }

    private Task Log(LogMessage arg)
    {
        Console.WriteLine(arg);

        return null;
    }

    public async Task RegisterCommandAsync()
    {
        _client.MessageReceived += HandleCommandAsync;

        await _command.AddModulesAsync(Assembly.GetEntryAssembly());

    }

    private async Task HandleCommandAsync(SocketMessage arg)
    {
        var message = arg as SocketUserMessage;




        if (!(message is SocketUserMessage) || message.Author.IsBot) {
            return;
        }
        int argPos = 1;

        if (message.HasStringPrefix("cf!", ref argPos) || message.HasMentionPrefix(_client.CurrentUser, ref argPos))
        {

            var context = new SocketCommandContext(_client, message);

            var result = await _command.ExecuteAsync(context, argPos+1, _service);

            if (!result.IsSuccess)
                Console.WriteLine(result.ErrorReason);


        }
    }
}

}

实用程序

   static string location = "town";            //curent loc
    static string[] locations =                 //array of vlaid loc
        {
            "town", "forest"
        };
    int[,] travelMtx = new int[,]               //distance matrix
   {
        {0,2 },
        {2,0 }
   };


    public string D6()
    {
        Random rnd = new Random();
        string reply;
        reply = Convert.ToString(rnd.Next(1, 7));
        return reply;
    }

    public string charName(string charowner = "")
    {
        string charname;

        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
        conn.ConnectionString = [FILE LOCATION]

        conn.Open();
        String my_query = "SELECT CharName FROM Chars WHERE CharOwner='" + charowner + "'";
        Console.WriteLine(my_query);
        OleDbCommand cmd = new OleDbCommand(my_query, conn);
        charname = (string)cmd.ExecuteScalar();
        Console.Write(charname);
        return charname;
    }

    public string usermention(string user = "")
    {
        return user;

    }

    public string getLoc()
    {
        return Utility.location;
    }

    public void setLoc(string location)
    {
        Utility.location = location;
    }

    public bool checkLoc(string dest)
    {
        for (int i = 0; i < locations.Length; i++)
        {
            if (dest.ToLower() == locations[i])
            {
                return true;

            }
        }
        return false;
    }

    public int travelTime(string location, string dest)
        {
        int x = 0;
        int y = 0;

        for (int i = 0; i < locations.Length; i++)
        {
            if (location.ToLower() == locations[i])
            {
                x = i;
            }
            if (dest.ToLower() == locations[i])
            {
                y= i;
            }
        }
           return travelMtx[x,y];
        }
    }
}

旅行

public class Travel : ModuleBase<SocketCommandContext>
{
    [Command("Travel")]
    public async Task PingAsync(string dest = "")
    {
        Utility Util = new Utility();

        string loc = Util.getLoc();
        int travelTime = 0;


        if (Util.checkLoc(dest) == true)
        {
            travelTime = Util.travelTime(loc, dest);
        }

        Util.setLoc(dest);
        await ReplyAsync("you are now in " + dest + " it took " + travelTime + " days" );
    }
}
}

1 个答案:

答案 0 :(得分:0)

我认为您可以尝试将其与serverip一起存储,以便可以在SQL的CharOwner='" + charowner + "'"部分中请求ServerIp='" + serverip + "'"WHERE

这只是一个猜测,但也许可以。 :)