让命令只为1个用户工作一次,直到下一个机器人订单发布



我目前正在尝试这样做,如果在discord通道中发布了新订单,那么第一个写入的用户!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);
}
}
}

订单命令类:

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. 在订单到达时为其添加唯一标识符
  2. 将唯一标识符与订单的其余部分一起显示
  3. 需要!Accept命令接受一个唯一的标识符
  4. 当!Accept命令被称为对照存储的已接受命令的字典进行检查
  5. 如果它已经在那里,那么它已经被其他人接受了
  6. 否则,请将其交给该用户

下面的一些代码。。。。我从你上面的例子中学到了很多,但它应该能给你一个想法。需要确保用于存储这些订单的任何容器都是并发的。我建议使用ConcurrentDictionary,但这是一种非常典型的方法。注意:您的Dictionary需要位于命令类之外的其他位置。。。命令类是根据需要创建和销毁的。

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);
}
}

最新更新