Discord.NET 用户在玩同一个游戏
Discord.NET Users playing the same game
我正在尝试创建一个命令,returns 当前正在玩指定游戏的公会成员的数量。
示例(! 是我的前缀):!玩英雄联盟。
如果有5个成员在玩英雄联盟,输出:
There are 5 users currently playing League of Legends.
我设置了以下内容,从调试中我能够找到 v.Game.toString()
returns 正确的字符串,但由于某种原因,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 语句应在何处触发的图片(用户名因隐私原因被屏蔽):
游戏参数不需要params string[] s
。只需使用 Remainder
属性。
我还简化了这段代码 很多 :
[Command("playing")]
public async Task GetUsersPlaying([Remainder]string game)
{
await Context.Message.DeleteAsync();
var users = Context.Guild.Users.Where(x => x.Game.ToString() == game).Distinct().Select(x => x.Username);
var count = users.Count();
var SeparatedList = string.Join(", ", users);
string message;
if (count > 1)
message = $"There are {count} users playing {game}. [{SeparatedList}]";
else if (count == 1)
message = $"There is {count} user playing {game}. [{SeparatedList}]";
else
message = $"There is no one playing {game}.";
await Context.Channel.SendMessageAsync(message);
}
我正在尝试创建一个命令,returns 当前正在玩指定游戏的公会成员的数量。
示例(! 是我的前缀):!玩英雄联盟。
如果有5个成员在玩英雄联盟,输出:
There are 5 users currently playing League of Legends.
我设置了以下内容,从调试中我能够找到 v.Game.toString()
returns 正确的字符串,但由于某种原因,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 语句应在何处触发的图片(用户名因隐私原因被屏蔽):
游戏参数不需要params string[] s
。只需使用 Remainder
属性。
我还简化了这段代码 很多 :
[Command("playing")]
public async Task GetUsersPlaying([Remainder]string game)
{
await Context.Message.DeleteAsync();
var users = Context.Guild.Users.Where(x => x.Game.ToString() == game).Distinct().Select(x => x.Username);
var count = users.Count();
var SeparatedList = string.Join(", ", users);
string message;
if (count > 1)
message = $"There are {count} users playing {game}. [{SeparatedList}]";
else if (count == 1)
message = $"There is {count} user playing {game}. [{SeparatedList}]";
else
message = $"There is no one playing {game}.";
await Context.Channel.SendMessageAsync(message);
}