Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否可以使用异步任务永久更改Bool值?_C#_Boolean_Discord_Discord.net - Fatal编程技术网

C# 是否可以使用异步任务永久更改Bool值?

C# 是否可以使用异步任务永久更改Bool值?,c#,boolean,discord,discord.net,C#,Boolean,Discord,Discord.net,我正在开发一个Discord机器人,它允许版主撤销一个功能的可用性(掷骰子)。我的目标是让它位于ifboola=true的位置;掷骰子布尔a=假;拒绝。版主将使用一个单独的函数来更改bool,在这个函数中,他们可以更改a的布尔值,并使其保持这种状态 我曾尝试将布尔值分配给一个单独的类,并使用get和set,但该值要么不更改,要么立即更改回去 [Command("diceRoll")] [Summary("Turns on/off the ability to use the Dice Roll

我正在开发一个Discord机器人,它允许版主撤销一个功能的可用性(掷骰子)。我的目标是让它位于if
boola=true的位置;掷骰子
<代码>布尔a=假;拒绝。版主将使用一个单独的函数来更改bool,在这个函数中,他们可以更改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;
            }
        }        
    }
}

我希望在调用“骰子滚动开/关”命令时,“滚动”命令将停止工作或再次工作。当前,已调用该命令,但布尔值未更改或未保持更改。

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

这实际上解释了很多,谢谢!我会进一步调查的。