让命令一次只能对1个用户起作用,直到发布下一个机器人命令为止

时间:2018-08-06 23:55:55

标签: c# discord discord.net

我目前正在尝试这样做,如果在不和谐频道中发布了新订单,则第一个写!accept的用户会在聊天中向他们抛出一条消息,而如果第一个家伙之后的人试图写!accept会收到错误消息。我该怎么办?

AcceptCommand类:

public class AcceptCommand : ModuleBase<SocketCommandContext>
{
    [Command("Accept")]
    public async Task order()
    {
        var embed = new EmbedBuilder();

        var UserID = Context.User.Username;

        await Context.Channel.SendMessageAsync("<@&468001483099471883>");
        embed.AddField("Order has been taken by booster", "__" + UserID + "__", true);
        embed.AddField("The CEOS will provide you with account details \nfor the boost in PM as fast as possible.", "_ _ ", false);

        embed.WithColor(new Color(253, 000, 00));

        embed.WithCurrentTimestamp();

        embed.WithFooter("WaveBoosting Order Fetcher", "https://i.imgur.com/DNMWntW.png");

        await Context.Channel.SendMessageAsync("", false, embed);

    }
  }
}

OrderCommand类:

 public class OrderCommand : ModuleBase<SocketCommandContext>
{
    [Command("Order")]
    public async Task order()
    {
        var embed = new EmbedBuilder();

        await Context.Channel.SendMessageAsync("<@&468001483099471883>");
        embed.WithAuthor("Order Available");
        embed.AddField("Platform", "STEAM", true);
        embed.AddField("Gamemode", "2V2", true);
        embed.AddField("Rank", "DIAMOND1 → CHAMPION1", true);
        embed.AddField("Price", "69,33 €", false);
        embed.AddField("_ _ ", "_ _ ", false);
        embed.AddField("Accept this order by typing  **!ACCEPT**", "_ _ ", false);

        embed.WithColor(new Color(38, 244, 22));

        embed.WithCurrentTimestamp();

        embed.WithFooter("WaveBoosting Order Fetcher", "https://i.imgur.com/DNMWntW.png");

        await Context.Channel.SendMessageAsync("", false, embed);
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我个人完成此操作的方式类似于以下内容。

  1. 向订单中添加唯一标识符。
  2. 显示唯一标识符以及订单的其余部分。
  3. 要求!Accept命令使用唯一的标识符。
  4. 调用!Accept命令时,请检查已存储的接受命令字典
  5. 如果已经存在,则发送已被其他人接受。
  6. 否则将其提供给该用户。

下面的一些代码...。我从上面的示例中提取了很多内容,但是应该可以使您明白。需要确保您用来存储这些订单的任何容器都是并发的。我建议您使用ConcurrentDictionary,但这是执行此类操作的一种非常典型的方法。 注意:您的字典需要驻留在命令类之外的其他位置...命令类会根据需要创建和销毁。

public class AcceptCommand : ModuleBase<SocketCommandContext>
{
    [Command("Accept")]
    public async Task order([Remainder]string uniqueID)
    {
        if(SomeDictionaryOfAcceptedOrders.ContainsKey(uniqueID){
            //Someone already accepted
        } else {
            //Give it to the caller
            SomeDictionaryOfAcceptedOrders.Add(uniqueID, "Person accepting order"); 
        }
     }
}


public class OrderCommand : ModuleBase<SocketCommandContext>
{
    [Command("Order")]
    public async Task order()
    {
        var uniqueID = //UUID, SomeRandomHashValue, Whatever that will be unique to this order;
        var embed = new EmbedBuilder();
        await Context.Channel.SendMessageAsync("<@&468001483099471883>");
        embed.WithAuthor("Order Available");
        embed.AddField("Unique Order ID", uniqueID, true);
        await Context.Channel.SendMessageAsync("", false, embed);
    }
}