【问题标题】:Is it possible to permanently change Bool Value with Async Task?是否可以使用异步任务永久更改布尔值?
【发布时间】:2019-09-19 18:34:52
【问题描述】:

我正在开发一个 Discord Bot,它允许版主撤销功能的可用性(掷骰子)。我的目标是在bool a = true; roll the dice 的地方拥有它。 bool a = false; deny。版主将使用单独的函数更改布尔值,他们可以在其中更改 a 的布尔值并使其保持不变。

我尝试将布尔值分配给单独的类并使用 get 和 set,但值要么没有改变,要么立即变回。

[Command("diceRoll")]
[Summary("Turns on/off the ability to use the Dice Roll function")]
public async Task DiceRoll(string toggle)
{
    switch (toggle)
    {
        case "on":
        case "On":
        case "ON":
            diceToggle.DiceBool = true;
            await Context.Channel.SendMessageAsync("Dice Roll Function: ON");
            break;

        case "off":
        case "Off":
        case "OFF":
            diceToggle.DiceBool = false;
            await Context.Channel.SendMessageAsync("Dice Roll Function: OFF");
            break;

        default:
            await Context.Channel.SendMessageAsync("Dice Roll Function: ERROR");
            break;
    }
}

[Command("roll")]
[Summary("Dice Roll")]
public async Task Dice(int number)
{
    if (diceToggle.DiceBool == true)
    {
        int randNumber = rand.Next(1, number);
        if (randNumber == 8 || randNumber == 11 || randNumber == 18)
        { await Context.Channel.SendMessageAsync("You rolled an " + randNumber + "."); }
        else 
        { await Context.Channel.SendMessageAsync("You rolled a " + randNumber + "."); }
    }
    else if (diceToggle.DiceBool == false)
    {
        await Context.Channel.SendMessageAsync("This feature has been disabled.");
    }
    else
    {
        await Context.Channel.SendMessageAsync("Something broke, please fix.");
        }
    }
}

public class Game
{
    private bool diceBool;
    public bool DiceBool
    {
        get { return diceBool; }
        set
        {
            if (diceBool != value)
            {
                diceBool = value;
            }
        }        
    }
}

我希望当调用“diceRoll on/off”命令时,“roll”命令将停止工作或再次工作。目前,该命令被调用,但布尔值没有变化或保持变化。

【问题讨论】:

    标签: c# boolean discord discord.net


    【解决方案1】:

    您无法“保存”该值的原因是模块在 Discord.Net 中的工作方式。与 ASP.NET 类似,模块是瞬态的,这意味着它们在执行后会从内存中销毁。详情请查看here

    【讨论】:

    • 这其实说明了很多,谢谢!我会对此进行更多研究。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    相关资源
    最近更新 更多