Exemple #1
0
        public static string Prepare(this string Trivia, Dictionary <string, string> Changes, ITextChannel Channel = null, Discord.IGuild Guild = null, Discord.IUser User = null)
        {
            var changes = new Dictionary <string, string>(Changes)
            {
                { "%OWNER%", Global.Settings.OwnerName }
            };

            GuildSetting Settings = null;

            if (Channel != null && Settings == null)
            {
                Settings = Channel.GetSettings();
            }
            else if (Guild != null && Settings == null)
            {
                Settings = Guild.GetSettings();
            }
            else if (User != null && Settings == null)
            {
                Settings = Global.GuildSettings.GetSettings(User.Id);
            }

            if (User != null)
            {
                changes.Add("%MENTION%", User.Mention);
            }

            if (Settings != null)
            {
                changes.Add("%P%", Settings.Prefix);
                changes.Add("%PREFIX%", Settings.Prefix);
            }

            var Text = Trivia;

            if (Text.Contains("%LANGUAGES%") || Text.Contains("%LANGS%"))
            {
                var LangNames = string.Join(", ", Global.LanguageHandler.Languages.Select(langPair => langPair.Key + " - " + langPair.Value.Name));
                Text = Text.Replace("%LANGUAGES%", LangNames);
                Text = Text.Replace("%LANGS%", LangNames);
            }

            foreach (var Pair in changes)
            {
                string Key = Pair.Key;

                if (!Key.EndsWith("%"))
                {
                    Key += "%";
                }
                if (!Key.StartsWith("%"))
                {
                    Key = "%" + Key;
                }

                Text = Text.Replace(Key.ToUpper(), Pair.Value);
            }

            return(Text);
        }
Exemple #2
0
        public async Task ListAsync(ICommandContext Context = null, ITextChannel Channel = null)
        {
            if (Channel == null)
            {
                Channel = Context.Channel as ITextChannel;
            }

            LanguageEntry Language = Channel.GetSettings().GetLanguage();

            var Builder = new EmbedBuilder()
            {
                Color = Color,
                Title = Language.GetEntry("MusicHandler:Queue"),
            };

            if (Queue.Count == 0)
            {
                Builder.Description = Language.GetEntry("MusicHandler:EmptyQueue");
            }
            else
            {
                int Count = Math.Min(5, Queue.Count);
                for (int i = 0; i < Count; i++)
                {
                    var    CurrentItem = Queue[i];
                    string fval        = $"[URL]({ CurrentItem.PublicUrl })\n" +
                                         $"{ Language.GetEntry("MusicHandler:UploadedBy") }: { CurrentItem.Author }\n" +
                                         $"{ Language.GetEntry("MusicHandler:Length") }: " + CurrentItem.Duration.ToString(@"hh\:mm\:ss");

                    Builder.AddField(new EmbedFieldBuilder()
                    {
                        IsInline = false,
                        Name     = "#" + (i + 1) + ": " + (CurrentItem.IsListenMoe ? "LISTEN.moe" : (CurrentItem.Title + " (" + (CurrentItem.IsYouTube ? "YouTube" : "SoundCloud") + ")")),
                        Value    = CurrentItem.IsListenMoe ? "[LISTEN.moe](https://listen.moe/)" : fval
                    });
                    if (i != Count - 1)
                    {
                        Builder.Fields[Builder.Fields.Count - 1].Value += "\n-----------";
                    }
                }

                if (Queue.Count > 5)
                {
                    Builder.WithFooter(Language.GetEntry("MusicHandler:MoreItems", "X", "" + (Queue.Count - 5)));
                }
            }
            await Channel.SendMessageAsync("", embed : Builder.Build());
        }
Exemple #3
0
        public async Task SendNowPlayingAsync(ICommandContext Context = null, ITextChannel Channel = null)
        {
            if (Channel == null)
            {
                Channel = Context.Channel as ITextChannel;
            }

            LanguageEntry Language = Channel.GetSettings().GetLanguage();

            Embed Final;

            if (Current == null)
            {
                Final = CreateMusicEmbed(Language.GetEntry("MusicHandler:NotPlaying"), Language);
            }
            else
            {
                Final = CreateMusicEmbed(Language.GetEntry("MusicHandler:NowPlaying"), Language, Current.Title,
                                         Current.Duration, Current.IsListenMoe ? Current.PublicUrl.Replace("fallback", "") : Current.PublicUrl, Current.Author, Current.IsListenMoe ? ListenMoeCurrentTime : CurrentTime, Current.Thumbnail);
            }

            await Channel.SendMessageAsync("", embed : Final);
        }
Exemple #4
0
        public static void Main(string[] args)
        {
            Console.Title           = "Chino-chan";
            InputStream             = Console.In;
            CancellationTokenSource = new CancellationTokenSource();
            CancellationToken       = CancellationTokenSource.Token;

            if (FirstRun)
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    SetErrorMode(ErrorModes.SEM_NOGPFAULTERRORBOX);
                }
                Args = args;

                if (Args.Length == 1)
                {
                    if (Args[0] == "1")
                    {
                        IsHandler = true;

                        DataSendTask = new Task(() =>
                        {
                            if (IsHandler)
                            {
                                while (!CancellationToken.IsCancellationRequested)
                                {
                                    int ServerCount           = 0;
                                    int ConnectedVoiceClients = 0;
                                    if (Global.MusicHandler != null && Global.MusicHandler.Ready)
                                    {
                                        ConnectedVoiceClients = Global.MusicHandler.ConnectedVoiceClients;
                                    }
                                    if (Global.ReadyFired)
                                    {
                                        ServerCount = Global.Client.Guilds.Count;
                                    }

                                    Console.WriteLine("Information:{0}|{1}|{2}|{3}", ServerCount, Global.CommandsUnderExecution, ConnectedVoiceClients, Global.Uptime.TotalMilliseconds);
                                    Thread.Sleep(100);
                                }
                            }
                        }, CancellationToken);

                        DataSendTask.Start();
                    }
                }

                if (!IsHandler)
                {
                    Logger.NewLog += Message =>
                    {
                        if (Message.Type == "Discord")
                        {
                            return;
                        }

                        lock (Console.Out)
                        {
                            if (Environment.UserInteractive && Console.WindowWidth > 0)
                            {
                                Console.Write("\r" + new string(' ', Console.WindowWidth - 1) + "\r");
                            }
                            Console.ForegroundColor = ConsoleColor.Blue;
                            Console.Write("[" + Message.Date.ToString("yyyy/MM/dd hh:mm:ss") + "] ");

                            Console.ForegroundColor = Message.Color;
                            Console.Write("[" + Message.Type.ToString() + "] ");


                            if (Message.Severity != null)
                            {
                                Console.Write("[" + Message.Severity + "] ");
                            }
                            Console.ResetColor();

                            Console.WriteLine(Message.Message);
                            //Console.Write("\n" + new string(' ', Console.WindowWidth) + "\n");
                        }
                    };
                }

                AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler((s, a) =>
                {
                    Exception e = a.ExceptionObject as Exception;
                    Logger.Log(LogType.Error, ConsoleColor.Red, "Exception", "Message: " + e.Message + "\r\nStack Trace: " + e.StackTrace + "\r\nSource: " + e.Source);
                });

                FirstRun = false;
            }


            if (CommandThread == null || !CommandThread.IsAlive)
            {
                CommandThread = new Thread(ManageConsoleCommands);
                CommandThread.Start();
            }
            Global.Setup();

            Global.Client.Ready += async() =>
            {
                await Global.Client.SetStatusAsync(UserStatus.Online);

                await Global.Client.SetGameAsync(Global.Settings.Game);

                await Global.Client.DownloadUsersAsync(Global.Client.Guilds);

                if (ReloadChannel != null)
                {
                    await ReloadChannel.SendMessageAsync(ReloadChannel?.GetSettings().GetLanguage().GetEntry("Global:Reloaded"));

                    ReloadChannel = null;
                }
            };

            Global.StartAsync().Wait();
        }
Exemple #5
0
        public static void HandleCommand(string Command, ITextChannel Channel = null)
        {
            var Lower     = Command.ToLower();
            var Trim      = Lower.Trim();
            var Parameter = "";
            var Index     = Command.IndexOf(" ");

            if (Index >= 0)
            {
                Lower     = Lower.Substring(0, Index);
                Trim      = Lower.Trim();
                Parameter = Command.Substring(Index).TrimEnd().TrimStart();
            }
            if (Trim == "gc")
            {
                Clean();
                if (Channel != null)
                {
                    Channel.SendMessageAsync(Channel?.GetSettings().GetLanguage().GetEntry("Global:GC_Complete"));
                }
            }
            else if (Trim == "quit" || Trim == "exit" || Trim == "shutdown")
            {
                if (Channel != null)
                {
                    Channel.SendMessageAsync(Channel?.GetSettings().GetLanguage().GetEntry("Global:Shutdown")).Wait();
                }
                Global.Stop();
                Environment.Exit(3);
            }
            else if (Trim == "reload")
            {
                if (Channel != null)
                {
                    Channel.SendMessageAsync(Channel?.GetSettings().GetLanguage().GetEntry("Global:Reload")).Wait();
                }
                Reload(Channel);
            }
            else if (Trim == "restart")
            {
                Global.Stop();
                Process.GetCurrentProcess().Kill();
            }
            else if (Trim == "update")
            {
                Update();
            }
            else if (Trim == "noupdatefound")
            {
                if (UpdateChannel != null)
                {
                    UpdateChannel.SendMessageAsync(UpdateChannel?.GetSettings().GetLanguage().GetEntry("Global:No_Update")).Wait();
                }
            }
            else if (Trim == "updatefound")
            {
                if (UpdateChannel != null)
                {
                    UpdateChannel.SendMessageAsync(UpdateChannel?.GetSettings().GetLanguage().GetEntry("Global:Update_Found")).Wait();
                }
                Global.Stop();
                Process.GetCurrentProcess().Kill();
            }
        }
Exemple #6
0
 public static Models.Language.Language GetLanguage(this ITextChannel Channel)
 {
     return(Global.LanguageHandler.GetLanguage(Channel.GetSettings().LanguageId));
 }
Exemple #7
0
        public async Task PlayAsync(ICommandContext Context, ITextChannel Channel = null, bool KeepCurrent = false, TimeSpan?SeekTo = null, bool AvoidState = false)
        {
            if (Channel == null)
            {
                Channel = Context.Channel as ITextChannel;
            }

            string lId = Channel.GetSettings().Language;

            LanguageEntry Language = Global.Languages.GetLanguage(lId);

            if (!AvoidState)
            {
                if (State == PlayerState.Playing || State == PlayerState.Paused)
                {
                    await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:AlreadyPlaying"));

                    return;
                }
            }
            if (State == PlayerState.Disconnected)
            {
                bool Success = await ConnectAsync(Context);

                if (!Success)
                {
                    return;
                }
            }
            if (!KeepCurrent || Current == null)
            {
                Current = Queue[QueueIndex];
            }

            string Url = Current.UrlOrId;

            WebSocket ws = null;

            if (Current.IsYouTube)
            {
                YoutubeClient  Client = new YoutubeClient();
                StreamManifest sm     = null;
                try
                {
                    sm = await Client.Videos.Streams.GetManifestAsync(VideoId.TryParse(Current.UrlOrId).Value);
                }
                catch (Exception e)
                {
                    File.WriteAllText("tempexceptionfrommusicplayer.json", JsonConvert.SerializeObject(e, Formatting.Indented));

                    await Global.Client.GetGuild(Global.Settings.DevServer.Id)
                    .GetTextChannel(Global.Settings.DevServer.ErrorReportChannelId).SendFileAsync("tempexceptionfrommusicplayer.json", "");

                    File.Delete("tempexceptionfrommusicplayer.json");
                }
                if (sm != null)
                {
                    IEnumerable <IAudioStreamInfo> streamInfos = sm.GetAudioStreams();

                    if (streamInfos.Count() > 0)
                    {
                        IStreamInfo info = streamInfos.GetWithHighestBitrate();
                        Url = info.Url;
                    }
                }

                if (Url == Current.UrlOrId)
                {
                    Url = null;
                }
            }
            else
            {
                if (Current.IsListenMoe)
                {
                    Url = Current.PublicUrl;
                    ws  = new WebSocket(Current.PublicUrl.Contains("kpop") ? "wss://listen.moe/kpop/gateway_v2" : "wss://listen.moe/gateway_v2");

                    Timer t = new Timer((state) =>
                    {
                        try
                        {
                            ws.Send("{ \"op\": 9 }");
                        }
                        catch
                        {
                            Logger.Log(LogType.WebSocket, ConsoleColor.Red, "Error", "Couldn't send heartbeat to LISTEN.moe!");
                        }
                    }, null, -1, -1);

                    ws.OnOpen += (s, e) =>
                    {
                        ws.Send("{ \"op\": 0, \"d\": { \"auth\": \"\" } }"); // { op: 0, d: { auth: "" } }
                    };
                    ws.OnMessage += (s, e) =>
                    {
                        ListenMoe parsed = JsonConvert.DeserializeObject <ListenMoe>(e.Data);
                        if (parsed.OpCode == 0)
                        {
                            t.Change(0, parsed.Data.HeartBeat);
                        }
                        else if (parsed.OpCode == 1 && parsed.Type == "TRACK_UPDATE")
                        {
                            ListenMoeStartTime = parsed.Data.StartTime;
                            Current.Author     = string.Join(" && ", parsed.Data.Song.Artists.Select(i => i.NameRomaji ?? i.Name));
                            Current.Duration   = TimeSpan.FromSeconds(parsed.Data.Song.Duration);
                            Current.Title      = parsed.Data.Song.Title;
                            Current.Thumbnail  = null;
                            foreach (ListenMoeAlbum album in parsed.Data.Song.Albums)
                            {
                                if (album.Image != null)
                                {
                                    Current.Thumbnail = album.Image;
                                    break;
                                }
                            }
                            if (State > PlayerState.Connected && State != PlayerState.Paused)
                            {
                                SendNowPlayingAsync(Context, Channel).GetAwaiter().GetResult();
                            }
                        }
                    };
                    ws.OnClose += (s, e) =>
                    {
                        t.Change(Timeout.Infinite, Timeout.Infinite);
                        t.Dispose();
                    };

                    ws.Connect();
                }
                else if (Global.SoundCloud == null)
                {
                    if (!IterateIndex(true))
                    {
                        Current = null;
                        await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:SoundCloudNotAvailable"));
                    }
                    else
                    {
                        await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:OnlySoundCloudTracks"));

                        new Task(async() => await PlayAsync(Context, Channel, AvoidState: true));
                    }
                    return;
                }
                else
                {
                    Url += "?client_id=" + Global.SoundCloud.ClientId;
                }
            }

            if (Url == null)
            {
                if (!IterateIndex())
                {
                    Current = null;
                    await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:FinishedPlaying"));

                    State = PlayerState.Connected;
                }
                else
                {
                    new Task(async() => await PlayAsync(Context, Channel, AvoidState: true)).Start();
                    return;
                }
            }

            try
            {
                Reader = new FFmpegReader(Url);

                /*
                 * switch (encoding.Name)
                 * {
                 *  case "webm":
                 *  case "3gpp":
                 *      Stream s = await new HttpClient().GetStreamAsync(Url);
                 *      Reader = new NAudio.Vorbis.VorbisWaveReader(s);
                 *      break;
                 *
                 *  default:
                 *      Reader = new MediaFoundationReader(Url);
                 *      break;
                 * }
                 */
            }
            catch (Exception e)
            {
                System.Console.WriteLine(e);
                await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:SongInaccessible", "SONGNAME", Current.Title));

                if (!IterateIndex(Global.SoundCloud == null))
                {
                    Current = null;
                    await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:FinishedPlaying"));

                    State = PlayerState.Connected;
                }
                else
                {
                    new Task(async() => await PlayAsync(Context, Channel, AvoidState: true)).Start();
                }
                return;
            }

            if (PCMStream == null || !PCMStream.CanWrite)
            {
                PCMStream = Client.CreatePCMStream(AudioApplication.Music, 128 * 1024, 200, 0);
            }

            //WaveFormat OutFormat = new WaveFormat(48000, 16, 2);


            /*
             * MediaFoundationResampler Resampler = new MediaFoundationResampler(Reader, OutFormat)
             * {
             *  ResamplerQuality = 60
             * };
             */

            //if (SeekTo.HasValue && !Current.IsListenMoe) Reader.CurrentTime = SeekTo.Value;
            if (SeekTo.HasValue && SeekTo != TimeSpan.Zero && !Current.IsListenMoe)
            {
                Reader.ReadUntil(SeekTo.Value);
            }
            BackupTime = TimeSpan.Zero;

            //int Size = OutFormat.AverageBytesPerSecond / 50;
            int Size = Reader.BufferSize(1);

            byte[] Buffer = new byte[Size];
            int    Count  = 0;

            State         = PlayerState.Playing;
            TextChannelId = Channel.Id;
            if (!Current.IsListenMoe)
            {
                await SendNowPlayingAsync(Context, Channel);
            }

            /*while (Reader.CanRead && (Count = Resampler.Read(Buffer, 0, Size)) > 0
             *  && Request == PlayerRequest.Idle && State > PlayerState.Connected)*/
            while (Reader.CanRead && (Count = Reader.Read(Buffer, 0, Size)) > 0 &&
                   Request == PlayerRequest.Idle && State > PlayerState.Connected)
            {
                if (State == PlayerState.Paused)
                {
                    await Channel.SendMessageAsync("", embed : new EmbedBuilder()
                    {
                        Title = Language.GetEntry("MusicHandler:Paused"),
                        Color = Color
                    }.Build());


                    while (State == PlayerState.Paused && Request == PlayerRequest.Idle)
                    {
                        Thread.Sleep(100);
                    }

                    string cId = Channel.GetSettings().Language;
                    if (cId != lId)
                    {
                        Language = Global.Languages.GetLanguage(cId);
                    }
                    lId = cId;

                    if (State == PlayerState.Playing)
                    {
                        await Channel.SendMessageAsync("", embed : new EmbedBuilder()
                        {
                            Title = Language.GetEntry("MusicHandler:Resumed"),
                            Color = Color
                        }.Build());

                        if (Current.IsListenMoe)
                        {
                            await SendNowPlayingAsync(Context, Channel);
                        }
                    }
                }
                if (Request > 0)
                {
                    break;
                }

                if (CurrentTime.TotalSeconds % 10 == 0 && BackupTime.TotalSeconds != CurrentTime.TotalSeconds)
                {
                    PropertyChanged?.Invoke();
                    BackupTime = CurrentTime;
                }

                try
                {
                    if (State < PlayerState.Playing)
                    {
                        break;
                    }
                    ChangeVolume(Buffer, Volume / 100f);
                    await PCMStream.WriteAsync(Buffer, 0, Count);
                }
                catch (Exception e)
                {
                    if (State == PlayerState.Disconnected || Client.ConnectionState == ConnectionState.Disconnected || Client.ConnectionState == ConnectionState.Disconnecting)
                    {
                        break;
                    }

                    string cId = Channel.GetSettings().Language;
                    if (cId != lId)
                    {
                        Language = Global.Languages.GetLanguage(cId);
                    }
                    lId = cId;

                    await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:PlaybackErrorOccured"));

                    Request = PlayerRequest.Stop;
                    Logger.Log(LogType.Music, ConsoleColor.Red, "Error", e.ToString());
                    if (Global.Settings.DevServer.ErrorReportChannelId != 0)
                    {
                        ITextChannel Report = Global.Client.GetChannel(Global.Settings.DevServer.ErrorReportChannelId) as ITextChannel;
                        await Report.SendMessageAsync($"An error occured while playing on a server!\n```\n{ e }\n```\n" +
                                                      $"Client:\n { JsonConvert.SerializeObject(this) }");
                    }
                    break;
                }
            }
            if (ws != null && ws.IsAlive)
            {
                ws.Close();
            }

            if (Request == PlayerRequest.Idle && State > PlayerState.Connected)
            {
                Request = PlayerRequest.Next;
            }

            //Resampler.Dispose();
            Reader.Dispose();
            Reader = null;
            //Resampler = null;
            try { await PCMStream.FlushAsync(); } catch { } // It may be disposed

            if (Request == PlayerRequest.Next)
            {
                Request = PlayerRequest.Idle;
                if (!IterateIndex(Global.SoundCloud == null))
                {
                    if (State != PlayerState.Connected && State != PlayerState.Disconnected)
                    {
                        State = PlayerState.Connected;
                    }
                    Current = null;

                    string cId = Channel.GetSettings().Language;
                    if (cId != lId)
                    {
                        Language = Global.Languages.GetLanguage(cId);
                    }
                    lId = cId;

                    await Channel.SendMessageAsync(Language.GetEntry("MusicHandler:FinishedPlaying"));
                }
            }
            else if (Request == PlayerRequest.Stop)
            {
                Request = PlayerRequest.Idle;
                State   = PlayerState.Connected;
                Current = null;
            }

            if (State > PlayerState.Connected)
            {
                new Task(async() => await PlayAsync(Context, Channel, AvoidState: true)).Start();
            }
        }