Beispiel #1
0
        /// <summary>
        /// Initialization of the package; this method is called right after the package is sited, so this is the place
        /// where you can put all the initialization code that rely on services provided by VisualStudio.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token to monitor for initialization cancellation, which can occur when VS is shutting down.</param>
        /// <param name="progress">A provider for progress updates.</param>
        /// <returns>A task representing the async work of package initialization, or an already completed task if there is none. Do not return null from this method.</returns>
        protected override async Task InitializeAsync(CancellationToken cancellationToken, IProgress <ServiceProgressData> progress)
        {
            AssemblyResolver.Redirect();
            await this.JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken);

            InversionContainer.Instance.Register <ISampleInjector>(new SampleInjector.SampleInjector());
            IMenuCommandService commandService = await this.GetServiceAsync(typeof(IMenuCommandService)) as IMenuCommandService;

            await CommandsProvider.InitializeCommandsAsync(commandService, this);
        }
Beispiel #2
0
        private static void Main()
        {
            var generator = SetUpGenerator();
            var logger    = SetUpLogger();

            VersionedDeserializers.RegisterFromAssembly(typeof(Program).Assembly);

            Application.Init();

            var commandProvider = new CommandsProvider(AppController.Instance(), logger);
            var menu            = new MenuWindow(commandProvider);

            AppController.Instance().StartWindow(menu, generator, logger);
        }
Beispiel #3
0
 public Exit(CommandsProvider commandsProvider) : base("exit")
     => this.commandsProvider = commandsProvider;
Beispiel #4
0
#pragma warning restore CS4014

        private async Task HandleDispatchEventAsync(DispatchType code, string payload)
        {
            switch (code)
            {
            case DispatchType.Ready:
            {
                var ready = JsonConvert.DeserializeObject <ReadyPayload>(payload);
                _sessionId        = ready.SessionId;
                _guildsToDownload = ready.Guilds.Count();
                this.Ready?.Invoke().ConfigureAwait(false);
                break;
            }

            case DispatchType.GuildMemberUpdate:
            {
                var member = JsonConvert.DeserializeObject <DiscordGuildMember>(payload);
                member = this.Guilds.First(x => x.Id == member.GuildId).Members.FirstOrDefault(x => x.UserData.Id == member.UserData.Id);
                this.MemberUpdated?.Invoke(member).ConfigureAwait(false);
                break;
            }

            case DispatchType.TypingStart:
            {
                var    typing = JsonConvert.DeserializeObject <TypingStartPayload>(payload);
                var    guild  = this.Guilds.FirstOrDefault(x => x.Id == (typing.GuildId ?? 0));
                object channel;
                if (guild != null)
                {
                    channel = guild.Channels.FirstOrDefault(x => x.Id == typing.ChannelId);
                }
                else
                {
                    channel = this.PrivateChannels.FirstOrDefault(x => x.Id == typing.ChannelId);
                }
                this.TypingStarted?.Invoke(typing.UserId, channel).ConfigureAwait(false);
                break;
            }

            case DispatchType.PresenceUpdate:
            {
                var presence = JsonConvert.DeserializeObject <PresencePayload>(payload);
                var guild    = this.Guilds.FirstOrDefault(x => x.Id == presence.GuildId);
                if (guild != null)
                {
                    var member = guild.Members.FirstOrDefault(x => x.UserData.Id == presence.User.Id);
                    member.RoleIds           = presence.RoleIds;
                    member.Nickname          = presence.Nickname;
                    member.UserData.Status   = presence.Status;
                    member.UserData.Activity = presence.Activity;
                    this.PresenceUpdated?.Invoke(guild, member).ConfigureAwait(false);
                }
                break;
            }

            case DispatchType.GuildCreate:
            {
                var guild = JsonConvert.DeserializeObject <DiscordGuild>(payload);
                this._internalGuilds.Add(guild);
                this._downloadedGuilds++;
                foreach (var member in guild.Members)
                {
                    member.Guild = guild;
                    member.Roles = guild.Roles.Where(x => member.RoleIds.Any(a => a == x?.Id)) as IReadOnlyList <DiscordGuildRole?>;
                }
                this.GuildCreated?.Invoke(guild).ConfigureAwait(false);
                if (this._guildsToDownload == this._downloadedGuilds)
                {
                    this.Guilds          = this._internalGuilds;
                    this.PrivateChannels = await GetPrivateChannelsAsync().ConfigureAwait(false);

                    this.GuildsDownloaded?.Invoke(this.Guilds).ConfigureAwait(false);
                }
                break;
            }

            case DispatchType.MessageCreate:
            {
                var msg = JsonConvert.DeserializeObject <DiscordMessage>(payload);
                this.MessageReceived?.Invoke(msg).ConfigureAwait(false);
                if (msg.Content.StartsWith(CommandsProvider.Prefix))
                {
                    var args = await CommandsProvider.ParseArguementsAsync(msg).ConfigureAwait(false);

                    Command?cmd;
                    bool    sub = false;
                    cmd = CommandsProvider.CommandsList.FirstOrDefault(x => (x?.CommandName == args[0]) || (x?.Aliases?.FirstOrDefault(a => a == args[0]) != null));
                    if (cmd == null)
                    {
                        var group = CommandsProvider.GroupCommandsList.FirstOrDefault(x => (x?.GroupName == args[0]) || (x?.Aliases?.FirstOrDefault(a => a == args[0]) != null));
                        cmd = group?.SubCommands.FirstOrDefault(x => (x.CommandName == args[1]) || (x.Aliases?.FirstOrDefault(a => a == args[1]) != null));
                        if (cmd == null && group?.ExecuteMethod != null)
                        {
                            cmd = new Command()
                            {
                                Method       = group?.ExecuteMethod,
                                MethodParams = group?.ExecuteMethod.GetParameters()
                            }
                        }
                        ;
                        else if (cmd != null)
                        {
                            sub = true;
                        }
                        else
                        {
                            break;
                        }
                    }
                    await CommandsProvider.ExecuteCommandAsync(args, (Command)cmd, sub, this, msg);
                }
                break;
            }

            case DispatchType.GuildMemberAdd:
            {
                var usr = JsonConvert.DeserializeObject <DiscordGuildMember>(payload);
                this.MemberAdded?.Invoke(this.Guilds.FirstOrDefault(x => x.Id == usr.GuildId), usr).ConfigureAwait(false);
                break;
            }

            case DispatchType.GuildMemberRemove:
            {
                var data = JsonConvert.DeserializeObject <MemberRemovedPayload>(payload);
                this.MemberRemoved?.Invoke(this.Guilds.FirstOrDefault(x => x.Id == data.GuildId), data.User).ConfigureAwait(false);
                break;
            }

            case DispatchType.Resumed:
            {
                this.GatewayResumed?.Invoke().ConfigureAwait(false);
                break;
            }

            default:
            {
                this.UnknownEventReceived?.Invoke($"Unknown Dispatch Type: {(int)code}", $"\n{payload}").ConfigureAwait(false);
                break;
            }
            }
        }
Beispiel #5
0
 public SendAll(CommandsProvider provider)
     : base(nameof(SendAll)) => this.provider = provider;
 public Help(CommandsProvider provider)
     : base("help") => Provider = provider ?? throw new ArgumentNullException();