【问题标题】:Discord Bot c# XP systemDiscord Bot c# XP 系统
【发布时间】:2018-03-12 16:38:31
【问题描述】:

我正在尝试为 c# discord bot 制作 xp 系统,但我的 int 不会保存在命令之外,例如检查余额或购买其他需要我检查整数值的项目。我是否需要将此数据存储在 sql 数据库中,或者是否可以将其作为 int 保留在 Visual Studio 中。谢谢。

public class Buy : ModuleBase<SocketCommandContext>
{

    int coins = 24;

    int coins2buymeme = 23;


    [Command("buyrole")]
    public async Task Ishoping (IGuildUser user, IRole role)
    {

        string invite_code = "you have sucesfully purchased the " + role + " role for " + coins2buymeme + " coins !!!";
        await Context.Channel.SendMessageAsync(invite_code);
        await user.AddRoleAsync(role);
        Console.WriteLine("User bought" + role + "for" + coins2buymeme + "if you are not happy with this decision please change there roles on server manually");
        this.coins -= coins2buymeme;
        Console.WriteLine("your new balance is " + this.coins + " if you are not happy with this decision please change there roles on server manually");

        const int delay = 90000;

        string bought = "your new balance is " + this.coins + " if you are not happy with this decision please change there roles on server manually";
        await Context.Channel.SendMessageAsync(bought);
    }



    [Command("refund")]
    public async Task Ishopidang(IGuildUser user, IRole roles)
    {

        string invite_code = "you have sucesfully refunded the " + roles + " role for " + coins2buymeme + " coins !!!";
        await Context.Channel.SendMessageAsync(invite_code);
        await user.RemoveRoleAsync(roles);
        Console.WriteLine("User refunded" + roles + "for" + coins2buymeme + "if you are not happy with this decision please change there roles on server manually");
    }
    [Command("balance")]
    public async Task Ishopiddang(IGuildUser user)
    {
        await Context.Channel.SendMessageAsync("you have " + this.coins + " to spend use !shop to find out more");
        Console.WriteLine("User has " + this.coins + "if you are not happy with this decision please change there roles on server manually");
    }



}

}

【问题讨论】:

  • 你指的是什么int
  • 硬币 int,这样当用户购买 meme 角色时,硬币数量会减少,他们将能够在余额中看到它。

标签: c# discord


【解决方案1】:

您的coins2buymeme 很好,最好将其设为静态和只读。
(除非你想让程序在逻辑上改变一半的值)

现在,您的coins,在用户使用buyrole 命令时进行更改,但在命令完成后,Buy 对象的当前实例被“销毁”。
当您再次调用命令时,将创建一个新的实例。
这意味着无论何时调用命令,coins 的值始终从 24 开始。

您可以将其更改为全局静态变量,但这会导致所有用户拥有相同数量的硬币。

我建议将其存储为 SQL 数据库。只需确保根据每个用户的 ID 来识别他们。
SQL 数据库而不是将其存储为对象的好处是,当您的机器人关闭时,coins 的值将保留给每个用户。

有一个关于如何在 Visual Studio here 上设置 SQL 数据库的 youtube 视频教程。它还教授如何将 .NET SQLClient 与 C# 一起使用。

(注意:我建议您了解有关对象和类属性的更多信息)

【讨论】:

    【解决方案2】:

    鉴于问题的措辞,我假设您以前没有做过太多编程。如果我假设错了,我深表歉意。

    当您运行机器人时,您正在运行一个可执行文件。每当运行可执行文件时,它都可以创建变量(如整数)并将数据存储在其中。这将在“内存中”完成,因为可执行文件会向计算机询问它需要的一些内存 (RAM)。但是,一旦可执行文件关闭/退出,它会将其占用的内存返回给计算机,从而丢失所有数据。 如果您希望在机器人关闭时保留您的数据,您需要通过将其保存到文件、数据库等来将其保存到硬盘驱动器。您可以选择适合您的方式保存它。大多数人会推荐数据库,因为它可以保存您的数据并使其与其他数据相关,从而轻松存储和读取复杂数据。

    对于您想要的,没有通用的解决方案。如果您想保存数据,您将需要查看您需要保存的内容,然后研究不同的方式并查看适合您需要的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-13
      • 2021-06-11
      • 2020-07-31
      • 2021-03-25
      • 2019-04-30
      • 2019-11-10
      • 1970-01-01
      • 2021-06-15
      相关资源
      最近更新 更多