我正在尝试创建一个命令,返回当前正在玩指定游戏的公会成员数。
示例(!是我的前缀) :.打英雄联盟。
如果有5名成员在玩英雄联盟,请输出:
v.Game.toString()
我设置了以下内容,从调试我能够接收[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 + ".");
}
}
返回正确的字符串但由于某种原因if语句不会触发。它还会捕获一个异常,当成员不玩游戏时会抛出异常(我认为它是空的?),是否有解决方法?为什么这不计算有多少成员正在玩某种游戏?
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 ---
这是一个例外:
public TestDatabase (Context context) {
super(context,DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(<YOUR QUERIES TO EXECUTE WHEN APPLICATION IS FIRST INSTALLED>);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(<YOUR QUERY WHEN DB VERSION CHANGES>);
}
if if语句应该触发的图片(因隐私原因而被阻止的用户名):
答案 0 :(得分:1)
游戏参数无需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);
}