示例#1
0
 public ScrobblerService(ICredentialHelper credentialHelper)
 {
     _credentialHelper = credentialHelper;
     _auth = new LastAuth(ApiKeys.LastFmId, ApiKeys.LastFmSecret);
     _albumApi = new AlbumApi(_auth);
     _artistApi = new ArtistApi(_auth);
     _chartApi = new ChartApi(_auth);
     _trackApi = new TrackApi(_auth);
     _userApi = new UserApi(_auth);
     GetSessionTokenAsync();
 }
示例#2
0
        // Commands first to help with adding new commands
        static void GenerateCommands(CommandGroupBuilder group)
        {
            // User commands
            group.CreateCommand("servers")
            .Description("I'll send you statistics about the servers, channels and users (can be spammy, goes to private).")
            .MinPermissions(4)
            .Do(async e =>
            {
                var output = "";
                foreach (var server in client.Guilds)
                {
                    output += $"{server.Name}: {server.TextChannels.Count()} text & {server.VoiceChannels.Count()} voice channels, {server.Users.Count()} users. ID: {server.Id}";
                    if (output.Length >= 2000)
                    {
                        var index = output.Length == 2000 ? 0 : output.LastIndexOf('\n');
                        await e.User.SendMessageAsync(Format.Code(index == 0 ? output : output.Substring(0, index)));
                        output = index == 0 ? "" : output.Substring(index + 1);
                    }
                    else
                    {
                        output += '\n';
                    }
                }
                if (output.Any())
                {
                    await e.User.SendMessageAsync(Format.Code(output));
                }
            });

            group.CreateCommand("status")
            .Description("I'll tell you some useful stats about myself.")
            .Do(async e => await e.Channel.SendMessageAsync($"I'm connected to {client.Guilds.Count()} servers, which have a total of {client.Guilds.SelectMany(x => x.TextChannels).Count()} text and {client.Guilds.SelectMany(x => x.VoiceChannels).Count()} voice channels, and see a total of {client.Guilds.SelectMany(x => x.Users).Distinct().Count()} different users.\n{Format.Code($"Uptime: {Helpers.Uptime()}\n{Console.Title}")}"));

            group.CreateCommand("version")
            .Description("I'll tell you the current version and check if a newer version is available.")
            .Do(async e => await e.Channel.SendMessageAsync(VersionCheck()));

            Common.AddCommands(group);

            group.CreateCommand("playeravatar")
            .Parameter("username1", Commands.ParameterType.Required)
            .Parameter("username2", Commands.ParameterType.Optional)
            .Parameter("username3", Commands.ParameterType.Multiple)
            .Description("I'll get you the avatar of each Player.me username provided.")
            .Do(async e =>
            {
                var rclient = Helpers.GetRestClient("https://player.me/api/v1/auth");
                var request = new RestRequest("pre-login", Method.POST);
                foreach (string s in e.Args)
                {
                    request.AddQueryParameter("login", s);
                    JObject result = JObject.Parse(rclient.Execute(request).Content);
                    await e.Channel.SendMessageAsync(s + (result["success"].ToObject <bool>() == false
                            ? " was not found." : $"'s avatar: https:{result["results"]["avatar"]["original"]}"));
                }
            });

            if (config["LastFM"].HasValues)
            {
                lfclient = new LastFM.LastfmClient(config["LastFM"]["apikey"].ToString(), config["LastFM"]["apisecret"].ToString());
                group.CreateCommand("lastfm")
                .Parameter("username(s)", Commands.ParameterType.Unparsed)
                .Description("I'll tell you the last thing you, a lastfm user, or users on this server (if I know their lastfm) listened to.")
                .Do(async e =>
                {
                    var api      = new LastFM.UserApi(lfclient.Auth, lfclient.HttpClient);
                    var users    = e.Args[0].Any() ? e.Message.MentionedUserIds.Any() ? e.Message.Tags.Where(x => x.Type == TagType.UserMention).Select(x => x.Value as IUser) : null : new[] { e.User };
                    var response = "";
                    if (users == null)
                    {
                        response = await GetLastScrobble(api, Tuple.Create(e.Args[0], e.Args[0], false));
                    }
                    else
                    {
                        foreach (var user in (from u in users select Tuple.Create(SQL.ReadUser(u.Id, "lastfm"), u.Username, u == e.User)))
                        {
                            response += (user.Item1 != null ? await GetLastScrobble(api, user)
                                    : $"I don't know {(user.Item3 ? "your" : $"{user.Item2}'s")} lastfm yet{(user.Item3 ? ", please use the `setlastfm <username>` command" : "")}"
                                         ) + ".\n";
                        }
                    }
示例#3
0
        // Commands first to help with adding new commands
        static void GenerateCommands(CommandGroupBuilder group)
        {
            // User commands
            group.CreateCommand("servers")
                .Description("I'll send you statistics about the servers, channels and users (can be spammy, goes to private).")
                .MinPermissions(4)
                .Do(async e =>
                {
                    var output = "";
                    foreach (var server in client.Servers)
                    {
                        output += $"{server.Name}: {server.TextChannels.Count()} text & {server.VoiceChannels.Count()} voice channels, {server.Users.Count()} users. ID: {server.Id}";
                        if (output.Length >= 2000)
                        {
                            var index = output.Length == 2000 ? 0 : output.LastIndexOf('\n');
                            await e.User.SendMessage(Format.Code(index == 0 ? output : output.Substring(0, index)));
                            output = index == 0 ? "" : output.Substring(index+1);
                        }
                        else output += '\n';
                    }
                    if (output.Any()) await e.User.SendMessage(Format.Code(output));
                });

            group.CreateCommand("status")
                .Description("I'll tell you some useful stats about myself.")
                .Do(async e => await e.Channel.SendMessage($"I'm connected to {client.Servers.Count()} servers, which have a total of {client.Servers.SelectMany(x => x.TextChannels).Count()} text and {client.Servers.SelectMany(x => x.VoiceChannels).Count()} voice channels, and see a total of {client.Servers.SelectMany(x => x.Users).Distinct().Count()} different users.\n{Format.Code($"Uptime: {Helpers.Uptime()}\n{Console.Title}")}"));

            group.CreateCommand("version")
                .Description("I'll tell you the current version and check if a newer version is available.")
                .Do(async e =>
                {
                    var version = Config.AppVersion;
                    string[] versions = version.Split('.');
                    string remoteversion = JObject.Parse(Helpers.GetRestClient("https://raw.githubusercontent.com").Execute<JObject>(new RestRequest("Kusoneko/Nekobot/master/version.json", Method.GET)).Content)["version"].ToString();
                    string[] remoteversions = remoteversion.Split('.');
                    int diff;
                    string section =
                        (diff = int.Parse(versions[0]) - int.Parse(remoteversions[0])) != 0 ? $"major version{(Math.Abs(diff) == 1 ? "" : "s")}" :
                        (diff = int.Parse(versions[1]) - int.Parse(remoteversions[1])) != 0 ? $"minor version{(Math.Abs(diff) == 1 ? "" : "s")}" :
                        (diff = int.Parse(versions[2]) - int.Parse(remoteversions[2])) != 0 ? $"patch{(Math.Abs(diff) == 1 ? "" : "es")}" : null;
                    await e.Channel.SendMessage($"I'm {(section == null ? $"up to date! (Current version: {version})" : $"currently {Math.Abs(diff)} {section} {(diff > 0 ? "ahead" : "behind")}. (Current version: {version}, latest {("released ")}version: {remoteversion})")}");
                });

            Common.AddCommands(group);

            group.CreateCommand("playeravatar")
                .Parameter("username1", Commands.ParameterType.Required)
                .Parameter("username2", Commands.ParameterType.Optional)
                .Parameter("username3", Commands.ParameterType.Multiple)
                .Description("I'll get you the avatar of each Player.me username provided.")
                .Do(async e =>
                {
                    var rclient = Helpers.GetRestClient("https://player.me/api/v1/auth");
                    var request = new RestRequest("pre-login", Method.POST);
                    foreach (string s in e.Args)
                    {
                        request.AddQueryParameter("login", s);
                        JObject result = JObject.Parse(rclient.Execute(request).Content);
                        await e.Channel.SendMessage(s + (result["success"].ToObject<bool>() == false
                            ? " was not found." : $"'s avatar: https:{result["results"]["avatar"]["original"]}"));
                    }
                });

            if (config["LastFM"].HasValues)
            {
                lfclient = new LastFM.LastfmClient(config["LastFM"]["apikey"].ToString(), config["LastFM"]["apisecret"].ToString());
                group.CreateCommand("lastfm")
                    .Parameter("username(s)", Commands.ParameterType.Unparsed)
                    .Description("I'll tell you the last thing you, a lastfm user, or users on this server (if I know their lastfm) listened to.")
                    .Do(async e =>
                    {
                        var api = new LastFM.UserApi(lfclient.Auth, lfclient.HttpClient);
                        var users = e.Args[0].Any() ? e.Message.MentionedUsers.Any() ? e.Message.MentionedUsers : null : new[]{e.User};
                        var response = "";
                        if (users == null)
                            response = await GetLastScrobble(api, Tuple.Create(e.Args[0], e.Args[0], false));
                        else foreach (var user in (from u in users select Tuple.Create(SQL.ReadUser(u.Id, "lastfm"), u.Name, u == e.User)))
                            response += (user.Item1 != null ? await GetLastScrobble(api, user)
                                    : $"I don't know {(user.Item3 ? "your" : $"{user.Item2}'s")} lastfm yet{(user.Item3 ? ", please use the `setlastfm <username>` command" : "")}"
                                ) + ".\n";
                        await e.Channel.SendMessage(response);
                    });

                group.CreateCommand("setlastfm")
                    .Parameter("username", Commands.ParameterType.Unparsed)
                    .Description("I'll remember your lastfm username.")
                    .Do(async e =>
                    {
                        var lastfm = e.Args[0];
                        if (lastfm.Any() && lastfm.Length < 16)
                        {
                            lastfm = $"'{lastfm}'";
                            await SQL.AddOrUpdateUserAsync(e.User.Id, "lastfm", lastfm);
                            await e.Channel.SendMessage($"I'll remember your lastfm is {lastfm} now, {e.User.Name}.");
                        }
                        else await e.Channel.SendMessage($"'{lastfm}' is not a valid lastfm username.");
                    });
            }