Esempio n. 1
0
        public async Task CommandsCommand()
        {
            try
            {
                var fields = new List <EmbedFieldBuilder>();

                foreach (ModuleInfo module in _commands.Modules)
                {
                    var names = new List <string>();
                    foreach (CommandInfo cmd in module.Commands)
                    {
                        if (cmd.Summary == null)
                        {
                            continue;
                        }
                        if (!names.Contains(cmd.Name))
                        {
                            names.Add(cmd.Name);
                        }
                    }
                    fields.Add(new EmbedFieldBuilder().WithIsInline(true).WithName(module.Name).WithValue($"**{string.Join(", ", names)}**"));
                }

                var embed = new EmbedBuilder()
                            .WithColor(_misc.RandomColor())
                            .WithTitle("Commands")
                            .WithFields(fields)
                            .WithCurrentTimestamp()
                            .WithThumbnailUrl(_client.CurrentUser.GetAvatarUrl(size: 512));

                await ReplyAsync(embed : embed.Build());
            }
            catch (Exception e)
            {
                await ReplyAsync(embed : (await _misc.GenerateErrorMessage(e)).Build());
            }
        }
Esempio n. 2
0
        public async Task GetModAction(SocketGuildUser user)
        {
            try
            {
                var sb = new StringBuilder();
                foreach (var action in (await _ms.GetModerationActionsAsync(Context.Guild.Id)))
                {
                    sb.Append($"{action.Type.ToString()}\n");
                }

                var embed = new EmbedBuilder()
                            .WithTitle($"Current moderation action for {user}:")
                            .WithColor(_misc.RandomColor())
                            .WithThumbnailUrl(user.GetAvatarUrl(size: 512))
                            .WithCurrentTimestamp()
                            .WithDescription(sb.ToString());

                await ReplyAsync(embed : embed.Build());
            }
            catch (Exception e)
            {
                await ReplyAsync(embed : (await _misc.GenerateErrorMessage(e)).Build());
            }
        }
Esempio n. 3
0
 public async Task AvatarCmd([Summary("The user whose avatar should be fetched.")] SocketUser user = null)
 {
     if (user == null)
     {
         user = Context.User;
     }
     if (Context.Guild.Users.Where(x => x.ToString() == user.ToString()).Count() == 0)
     {
         return;
     }
     await ReplyAsync(embed : new EmbedBuilder().WithImageUrl(user.GetAvatarUrl(ImageFormat.Auto, 512)).WithColor(_misc.RandomColor()).Build());
 }
Esempio n. 4
0
        public async Task AddCmd([Summary("The URL to add from YouTube, or search term."), Remainder] string urlOrTerm)
        {
            try
            {
                Song s;
                var  key = _db.GetApiKey("youtube");
                urlOrTerm = urlOrTerm.Trim('<', '>');

                if (Uri.TryCreate(urlOrTerm, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps))
                {
                    using (WebClient client = new WebClient())
                    {
                        Regex regex = new Regex("youtu(?:.be|be.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)");
                        var   id    = regex.Match(urlOrTerm).Groups[1].Value;

                        var json = await client.DownloadStringTaskAsync($"https://www.googleapis.com/youtube/v3/videos?id={id}&key={key}&part=snippet,contentDetails,statistics,status");

                        JObject obj = JsonConvert.DeserializeObject <JObject>(json);

                        if (obj.SelectToken("pageInfo").SelectToken("totalResults").Value <int>() == 0)
                        {
                            return;
                        }
                        var songUrl = $"https://www.youtube.com/watch?v={id}";

                        var pleasewait = await ReplyAsync($"Searching for your track... <a:search:588920003374350356>");

                        var streaminfo = await _audio.GetStreamInfo(id);

                        s = new Song
                        {
                            URL       = songUrl,
                            AudioURL  = streaminfo.Url,
                            Bitrate   = streaminfo.Bitrate,
                            QueuerId  = Context.User.Id,
                            ChannelId = Context.Channel.Id,
                            GuildId   = Context.Guild.Id,
                            Author    = obj.SelectToken("items").First.SelectToken("snippet.channelTitle").Value <string>(),
                            Name      = obj.SelectToken("items").First.SelectToken("snippet.title").Value <string>(),
                            Length    = System.Xml.XmlConvert.ToTimeSpan(obj.SelectToken("items").First.SelectToken("contentDetails.duration").Value <string>()),
                            Thumbnail = obj.SelectToken("items").First.SelectToken("snippet.thumbnails.high.url").Value <string>()
                        };

                        await pleasewait.DeleteAsync();

                        await _ms.AddSongAsync(Context, s);
                    }
                }
                else
                {
                    YouTubeService y = new YouTubeService(new BaseClientService.Initializer()
                    {
                        ApiKey = key
                    });

                    var request = y.Search.List("snippet");
                    request.Q          = urlOrTerm;
                    request.Type       = "video";
                    request.MaxResults = 10;

                    var response = await request.ExecuteAsync();

                    List <string> titles  = new List <string>();
                    int           counter = 1;

                    foreach (var result in response.Items)
                    {
                        titles.Add($"{counter}. {result.Snippet.Title} *by {result.Snippet.ChannelTitle}*");
                        counter++;
                    }

                    EmbedBuilder embed = new EmbedBuilder()
                                         .WithColor(_misc.RandomColor())
                                         .WithTitle("Respond with the number that you want to add.")
                                         .WithCurrentTimestamp()
                                         .WithDescription($"**{string.Join("**\n**", titles)}**");

                    var message = await ReplyAsync(embed : embed.Build());

                    var nextMsg = await NextMessageAsync(timeout : TimeSpan.FromSeconds(30));

                    if (!int.TryParse(nextMsg.Content, out int number))
                    {
                        await message.DeleteAsync();

                        return;
                    }
                    if (number > 10 || number < 1)
                    {
                        await message.DeleteAsync();

                        return;
                    }
                    var id = response.Items[number - 1].Id.VideoId;

                    using (WebClient client = new WebClient())
                    {
                        var json = await client.DownloadStringTaskAsync($"https://www.googleapis.com/youtube/v3/videos?id={id}&key={key}&part=snippet,contentDetails,statistics,status");

                        JObject obj     = JsonConvert.DeserializeObject <JObject>(json);
                        var     songUrl = $"https://www.youtube.com/watch?v={id}";

                        var pleasewait = await ReplyAsync($"Searching for your track... <a:search:588920003374350356>");

                        var streaminfo = await _audio.GetStreamInfo(id);

                        s = new Song
                        {
                            URL       = songUrl,
                            AudioURL  = streaminfo.Url,
                            Bitrate   = streaminfo.Bitrate,
                            QueuerId  = Context.User.Id,
                            ChannelId = Context.Channel.Id,
                            GuildId   = Context.Guild.Id,
                            Author    = obj.SelectToken("items").First.SelectToken("snippet.channelTitle").Value <string>(),
                            Name      = obj.SelectToken("items").First.SelectToken("snippet.title").Value <string>(),
                            Length    = System.Xml.XmlConvert.ToTimeSpan(obj.SelectToken("items").First.SelectToken("contentDetails.duration").Value <string>()),
                            Thumbnail = obj.SelectToken("items").First.SelectToken("snippet.thumbnails.high.url").Value <string>()
                        };
                        if (obj.SelectToken("items").First.SelectToken("snippet.title").Value <string>() == null)
                        {
                            return;
                        }

                        await pleasewait.DeleteAsync();

                        await message.DeleteAsync();

                        await _ms.AddSongAsync(Context, s);
                    }
                }

                await _ms.PlayAsync(Context, s);
            }
            catch (System.Net.Http.HttpRequestException)
            {
                await ReplyAsync($"The bot has not connected to Lavalink yet. Please wait a few moments...");
            }
            catch (Exception e)
            {
                await ReplyAsync(embed : (await _misc.GenerateErrorMessage(e)).Build());
            }
        }