Beispiel #1
0
        private async void ExecuteCommand(KeyValuePair <string, Tuple <BaseModule, ILog, MethodInfo> > Command, MessageEventArgs e, string @params)
        {
            try {
                log.Debug(string.Format(Resources.Culture, Resources.ResourceManager.GetString("ExecutingCommand", Resources.Culture), Command.Key, string.IsNullOrEmpty(@params) ? Resources.ResourceManager.GetString("NoParams", Resources.Culture) : @params));
                Stopwatch st = new Stopwatch();
                st.Start();
                MethodInfo       method     = Command.Value.Item3;
                CommandAttribute attr       = method.GetCustomAttribute <CommandAttribute>();
                List <object>    parameters = new List <object>();
                foreach (ParameterInfo info in method.GetParameters())
                {
                    Type t = info.ParameterType;
                    if (parameters.Exists(p => p.GetType().Equals(t)))
                    {
                        Command.Value.Item2.Error(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingDuplicateParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        return;
                    }
                    DiscordVoiceConnection connection = null;
                    if (attr.NeedsVoice)
                    {
                        DiscordGuildTextChannel channel = shard.Cache.GetGuildTextChannel(e.Message.ChannelId);
                        if (channel == null)
                        {
                            channel = await client.GetChannel <DiscordGuildTextChannel>(e.Message.ChannelId).ConfigureAwait(true);

                            log.Debug(Resources.ResourceManager.GetString("HadToLoadGuild", Resources.Culture));
                        }
                        DiscordVoiceState voiceState = shard.Cache.GetVoiceState(channel.GuildId, e.Message.Author.Id);
                        if (voiceState.ChannelId.HasValue)
                        {
                            connection = shard.Voice.CreateOrGetConnection(channel.GuildId);
                            if (connection.IsValid && !connection.IsConnected && !connection.IsConnecting)
                            {
                                await connection.ConnectAsync(voiceState.ChannelId.Value, startDeaf : true).ConfigureAwait(true);

                                log.Debug(string.Format(Resources.Culture, Resources.ResourceManager.GetString("ConnectedToVoiceChannel", Resources.Culture), voiceState.ChannelId.Value, Guilds.FirstOrDefault(g => g.Id == channel.GuildId)));
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    switch (t.Name)
                    {
                    case "ICoreHandler": {
                        if (!attr.NeedsHandler)
                        {
                            Command.Value.Item2.Warn(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnflaggedParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        }
                        parameters.Add(this);
                        break;
                    }

                    case "Message": {
                        if (!attr.NeedsMessage)
                        {
                            Command.Value.Item2.Warn(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnflaggedParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        }
                        Message message = new Message(e.Message.Id, e.Message.ChannelId.Id, e.Message.Author.Id);
                        if (attr.NeedsVoice)
                        {
                            message.HasSound += Message_HasSound;
                        }
                        parameters.Add(message);
                        break;
                    }

                    case "String": {
                        if (!attr.HasParams)
                        {
                            Command.Value.Item2.Warn(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnflaggedParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        }
                        parameters.Add(@params);
                        break;
                    }

                    default: {
                        Command.Value.Item2.Error(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnknownParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        return;
                    }
                    }
                }
                await Task.Run(() => {
                    try {
                        if (Command.Value.Item3.IsStatic)
                        {
                            Command.Value.Item3.Invoke(null, parameters.ToArray());
                        }
                        else
                        {
                            Command.Value.Item3.Invoke(Command.Value.Item1, parameters.ToArray());
                        }
                    } catch (InvalidOperationException ex) {
                        Command.Value.Item2.Error(ex.InnerException.ToString());
                    }
                }).ConfigureAwait(true);

                st.Stop();
                log.Debug(string.Format(Resources.Culture, Resources.ResourceManager.GetString("RanCommand", Resources.Culture), Command.Key, st.Elapsed.ToString("s\\.f", Resources.Culture)));
            } catch (Exception ex) {
                log.Error(ex);
                throw;
            }
        }