我目前正在尝试这样做,如果在不和谐频道中发布了新订单,则第一个写!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);
}
}
}
答案 0 :(得分:0)
我个人完成此操作的方式类似于以下内容。
下面的一些代码...。我从上面的示例中提取了很多内容,但是应该可以使您明白。需要确保您用来存储这些订单的任何容器都是并发的。我建议您使用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);
}
}