我正在编写一个机器人,它在使用 DSharp+ 加入的服务器中实现了罢工系统。在您被完全禁止使用服务器之前有 5 次罢工。
我的代码如下:
[Command("strike")]
public async Task StrikeSystem(CommandContext ctx, string setType, DiscordMember member)
{
if(setType.ToLower() == "add")
{
if (hasStrikes(member))
{
int strikeNumber = getStrikeNumber(member);
await member.RevokeRoleAsync(Test_Bot.StrikeRoles.roleArray[strikeNumber - 1]);
await member.GrantRoleAsync(Test_Bot.StrikeRoles.roleArray[strikeNumber]);
await ctx.RespondAsync($"{member.Mention} is now on Strike {strikeNumber + 1}.");
}
else
{
await member.GrantRoleAsync(Test_Bot.StrikeRoles.roleArray[0]);
await ctx.RespondAsync($"{member.Mention} is now on Strike 1.");
}
}
else if (setType.ToLower() == "remove")
{
if (hasStrikes(member))
{
int strikeNumber = getStrikeNumber(member);
if(strikeNumber != 1)
{
await member.RevokeRoleAsync(Test_Bot.StrikeRoles.roleArray[strikeNumber - 1]);
await member.GrantRoleAsync(Test_Bot.StrikeRoles.roleArray[strikeNumber - 2]);
await ctx.RespondAsync($"{member.Mention} is now on Strike {strikeNumber - 1}.");
}
else
{
await member.RevokeRoleAsync(Test_Bot.StrikeRoles.roleArray[0]);
await ctx.RespondAsync($"{member.Mention} now has no strikes.");
}
}
else
{
await ctx.RespondAsync($"{member.Mention} has no strikes!");
}
}
else if (setType.ToLower() == "clear")
{
if (hasStrikes(member))
{
foreach (DiscordRole role in member.Roles)
{
foreach (DiscordRole strikeRole in Test_Bot.StrikeRoles.roleArray)
{
if (role == strikeRole)
{
await member.RevokeRoleAsync(role);
await ctx.RespondAsync($"{member.Mention} now has no strikes.");
}
}
}
}
else
{
await ctx.RespondAsync($"{member.Mention} has no strikes!");
}
}
}
[Command("setstrikeroles")]
public async Task SetStrikeRoles(CommandContext ctx, DiscordRole strikeRole1, DiscordRole strikeRole2, DiscordRole strikeRole3, DiscordRole strikeRole4, DiscordRole strikeRole5)
{
Test_Bot.StrikeRoles.roleArray = new DiscordRole[] { strikeRole1, strikeRole2, strikeRole3, strikeRole4, strikeRole5 };
foreach(DiscordRole role in Test_Bot.StrikeRoles.roleArray)
{
await ctx.RespondAsync($"The role '{role.Name}' is now a role signifying a strike.");
}
}
bool hasStrikes(DiscordMember member)
{
foreach(DiscordRole role in member.Roles)
{
foreach(DiscordRole strikeRole in Test_Bot.StrikeRoles.roleArray)
{
if(role == strikeRole)
{
return true;
}
}
}
return false;
}
int getStrikeNumber(DiscordMember member)
{
foreach (DiscordRole role in member.Roles)
{
foreach (DiscordRole strikeRole in Test_Bot.StrikeRoles.roleArray)
{
if (role == strikeRole)
{
string roleName = role.Name;
string strikeString = roleName.Substring(roleName.Length - 1);
return int.Parse(strikeString);
}
}
}
return 10; //10 isn't a feasible number in this code, and you can't return nullified vaues as integers
}
但是,我的代码在角色更改时似乎没有更新,即使再次调用该方法也是如此。
如果收到警示的用户还没有任何警示,我得到的输出:
<块引用>用户:$strike add @user
BOT:@user 现在在 Strike 1
BOT:分配角色“Strike 1”
用户:$strike add @user
BOT:@user 现在在 Strike 1
BOT:分配角色“Strike 1”(不会改变任何东西)
什么时候应该:
<块引用>用户:$strike add @user
BOT:@user 现在在 Strike 1
BOT:分配角色“Strike 1”
用户:$strike add @user
机器人:@user 现在在 Strike 2
BOT:分配角色“Strike 2”
如何解决这个问题并获得第二个输出示例而不是第一个?