【发布时间】:2017-10-21 05:07:17
【问题描述】:
我正在尝试创建一个返回当前正在玩指定游戏的公会成员数量的命令。
示例(! 是我的前缀):!玩英雄联盟。
如果有 5 个玩家在玩英雄联盟,输出:
There are 5 users currently playing League of Legends.
我设置了以下内容,从调试中我发现v.Game.toString() 返回了正确的字符串,但由于某种原因,if 语句没有触发。它还会捕获成员不玩游戏时引发的异常(我假设它为空?),是否有解决方法?为什么这不计算有多少成员在玩某个游戏?
[Command("playing")]
public async Task playingGame(params string[] s)
{
string gameName = "";
int count = 0;
for (int i = 0; i < s.Length; i++)
{
gameName += s[i] + " ";
}
await Context.Channel.SendMessageAsync("Looking for: " + gameName);
var u = Context.Guild.Users;
foreach (var v in u)
{
await Context.Channel.SendMessageAsync("v = " + v.ToString());
try
{
await Context.Channel.SendMessageAsync(v.Game.ToString());
if (v.Game.ToString().ToLower().Equals(gameName.ToLower()))
{
count++;
await Context.Channel.SendMessageAsync("Found match, count = " + count);
}
}
catch (Exception x)
{
await Context.Channel.SendMessageAsync("Exception throw caught");
}
}
if (count > 1) {
await Context.Channel.SendMessageAsync("There are " + count + " users currently playing " + gameName + ".");
}
else if (count == 1)
{
await Context.Channel.SendMessageAsync("There is " + count + " user currently playing " + gameName + ".");
}
else if (count == 0)
{
await Context.Channel.SendMessageAsync("No one is currently playing " + gameName + ".");
}
}
这是个例外:
System.ArgumentException: Argument cannot be blank
Parameter name: Content
at Discord.Preconditions.NotNullOrEmpty(String obj, String name, String msg)
at Discord.API.DiscordRestApiClient.<CreateMessageAsync>d__77.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
if 语句应该在哪里触发的图片(出于隐私原因屏蔽了用户名):
【问题讨论】:
-
你能分享一下这个例外吗?
-
@aloisdg 添加了异常消息和 if 语句应该触发的位置的图片。
-
我知道这个字符串是空的,因为他们不是在玩游戏,所以它没有被初始化。我主要担心的是 if 语句没有触发,
标签: c# discord.net