【问题标题】:How to keep data for different guilds/servers for Discord Bots如何为 Discord Bots 保存不同公会/服务器的数据
【发布时间】:2019-03-15 10:02:56
【问题描述】:

我目前正在进行一个项目,我正在尝试为不和谐机器人制作角色扮演游戏。我目前正在努力解决如何实现一种将不同服务器的数据分开的方法。例如,我正在尝试为每个服务器存储聚会的位置。我尝试过从“城镇”到“森林”的测试。它适用于使用该命令的服务器,但该机器人所在的所有其他服务器也将其位置更新为“森林”。由于我也是 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" );
    }
}
}

【问题讨论】:

    标签: c# discord discord.net


    【解决方案1】:

    我认为你可以尝试将它与 serverip 一起存储,这样你就可以在你的 WHERE 部分 sql 中请求 CharOwner='" + charowner + "'"ServerIp='" + serverip + "'"

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

    【讨论】:

    • Discord 服务器并没有真正通过 IP 来区分,而是通过 ulong 标识符来区分。但这听起来确实是最好的解决方案。也许您可以详细说明如何正确实现这一点,以及为什么它会是一个不错的选择
    • 等等,我的意思是服务器 ID。那是我的错,对不起!
    猜你喜欢
    • 2020-09-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-21
    • 2021-04-20
    • 2021-12-30
    • 2019-03-30
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多